SpringCloud GateWay网关-学习笔记

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 SpringCloud GateWay网关-学习笔记,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1,概述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

一句话总结:SpringCloud Gateway使用的是Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架。

源码架构在这里插入图片描述

2,gateway能做什么

			反向代理
			鉴权
			流量控制
			熔断
			日志监控
			...

3,微服务中的网关位置

在这里插入图片描述


非阻塞异步!非阻塞异步!非阻塞异步!


4,三大核心概念

路由(route)

路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如断言为true则匹配该路由

断言(predicate)

参考的是Java8的java.util.function.Predicate
开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由

过滤(filter)

指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改.

总体:
在这里插入图片描述

5,gateway工作流程

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

核心逻辑 : 路由转发+执行过滤器链

6,Gateway网关路由有两种配置方式:

在配置文件yaml中配置:

server:
  port: 9527
spring:
  application:
    name: cloud-gateway-service
  cloud:
    gateway:
      routes:                                     #多个路由
        - id: payment_routh                       #路由的ID,没有固定规则但要求唯一,建议配合服务名
          #          uri: http://localhost:8001   #匹配后提供服务的路由地址
          uri: lb://cloud-payment-service         #路由的ID,没有固定规则但要求唯一,建议配合服务名
          predicates:                             #断言
            - Path=/payment/getById/**                #断言 路径相匹配的进行路由
        - id: payment_routh2
          #          uri: http://localhost:8001
          uri: lb://cloud-payment-service
          predicates:
            - Path=/payment/lb/**
      discovery:
        locator:
          enabled: true  #开启从注册中心动态生成路由的功能,用微服务名进行路由

eureka:
  instance:
    hostname: cloud_gateway_service
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

代码中注入RouteLocator的Bean

package com.tigerhhzz.springcloud.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author tigerhhzz
 * @date 2022/6/16 11:29
 */
@Configuration
public class GateWayConfig {

    /*
    * 配置了一个id为route-name的路由规则
    * 当访问地址http://localhost:9527/guonei时自动转发到地址:http://news.baidu.com/guonei
    * */
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route("path_route",r->r.path("/guonei").uri("http://news.baidu.com/guonei"));
        routes.route("path_route1",r->r.path("/guoji").uri("http://news.baidu.com/guoji"));
        return routes.build();
    }
}


7,开启注册中心动态创建路由,通过服务名实现动态路由

9527服务中的yml配置
在这里插入图片描述

8,常用的8种gateway路由断言(Route Predicate)

在这里插入图片描述
curl测试请求(携带cookie的例子)

C:\Users\Administrator>curl http://localhost:9527/payment/lb
{"timestamp":"2022-06-16T12:37:14.285+0000","path":"/payment/lb","status":404,"error":"Not Found","message":null,"requestId":"ee95c33b"}
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8001
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8002
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8001
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8002
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8001
C:\Users\Administrator>

断言yml配置参考
在这里插入图片描述

总结:Predicate就是为了实现一组匹配规则, 让请求过来找到对应的Route进行处理!!

9,Filter的使用

实现两个接口 implments GlobalFilter,OrderId

filter的作用是 全局日志记录和统一网关鉴权 等等等

自定义过滤器

package com.tigerhhzz.springcloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;

/**
 * @author tigerhhzz
 * @date 2022/6/16 21:05
 */
@Component
public class MyGateWayFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("**************come in MylogGateWayGilter:  " + new Date());
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
        if (uname == null) {
            System.out.println("********用户名为空,非法用户。");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

测试地址:http://localhost:9527/payment/lb?uname=z3

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135763.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!