详解SpringCloud新一代网关Gateway

时间:2022-08-27 10:37:44

一、概述简介

1.1、简介

springcloud gateway作为spring cloud生态系统中的网关,目标是替代zuul,在spring cloud 2.0以上版本中,没有对新版本的zuul 2.0以上最新高性能版本进行集成,仍然还是使用的zuul 1.x非reactor模式的老版本。而为了提升网关的性能,springcloud gateway是基于webflux框架实现的,而webflux框架底层则使用了高性能的reactor模式通信框架netty。

spring cloud gateway的目标提供统- -的路由方式且基于 filter 链的方式提供了网关基本的功能,例如:安全,监控/指标, 和限流。

一句话:springcloud geteway使用的webflux中的reactor-netty响应式变成组建,底层使用了netty通讯框架。 

1.2、作用

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

二、三大核心概念

2.1、route 路由

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

2.2、predicate 断言

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

2.3、filter 过滤

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

2.4、总体

  • web请求,通过一些匹配条件,定位到真正的服务节点。并在这个转发过程的前后,进行一些精细化控制。
  • predicate就是我们的匹配条件;
  • 而filter,就可以理解为一个无所不能的拦截器。有了这两个元素,再加上目标uri,就可以实现一个具体的路由了

三、getway工作流程

  • 客户端向spring cloud gatqway发出请求。然后在gateway handler mapping中找到与请求相匹配的路由,将其发送到gateway
  • web handler
  • handler再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。
  • 过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前( “pre” )或之后( “post” )执行业务逻辑。
  • filter在 “pre” 类型的过滤器可以做参数校验、权限校验、流量监控、日志输出、协议转换等,
  • 在"post" 类型的过滤器中可以做响应内容、响应头的修改,日志的输出,流量监控等有着非常重要的作用。
  • 核心逻辑:路由转发+执行过滤链

四、入门配置

4.1、pom

?
1
2
3
4
<!--新增gateway-->
<dependency>
   <groupid>org.springframework.cloud</groupid>
   <artifactid>spring-cloud-starter-gateway</artifactid> </dependency>

4.2、路由配置

yml:

server:

  port: 9527

spring:

  application:

    name: cloud-gateway

  cloud:

    gateway:

      routes:

        - id: payment_routh #路由的id,没有固定规则但要求唯一,建议配合服务名

          uri: http://localhost:8001   #匹配后提供服务的路由地址

          predicates:

            - path=/payment/get/**   #断言,路径相匹配的进行路由

 

        - id: payment_routh2

          uri: http://localhost:8001

          predicates:

            - path=/payment/lb/**   #断言,路径相匹配的进行路由

 

eureka:

  instance:

    hostname: cloud-gateway-service

  client:

    service-url:

      register-with-eureka: true

      fetch-registry: true

      defaultzone: http://eureka7001.com:7001/eureka

bean:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.rw.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;
 
@configuration
public class gatewayconfig {
    @bean
    public routelocator customroutelocator(routelocatorbuilder routelocatorbuilder){
        routelocatorbuilder.builder routes=routelocatorbuilder.routes();
        routes.route("path_rout_rw1",
                r->r.path("/guonei")
                        .uri("http://news.baidu.com/guonei"))
                .build();
        return routes.build();
    }
}

五、通过微服务名实现动态路由

默认情况下gateway会根据注册中心注册的服务列表以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能

yml:

server:

  port: 9527

spring:

  application:

    name: cloud-gateway

  cloud:

    gateway:

      discovery:

        locator:

          enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由

      routes:

        - id: payment_routh #路由的id,没有固定规则但要求唯一,建议配合服务名

#          uri: http://localhost:8001   #匹配后提供服务的路由地址

          uri: lb://cloud-payment-service

          predicates:

            - path=/payment/get/**   #断言,路径相匹配的进行路由

 

        - id: payment_routh2

          #uri: http://localhost:8001

          uri: lb://cloud-payment-service

          predicates:

            - path=/payment/lb/**   #断言,路径相匹配的进行路由

 

 

eureka:

  instance:

    hostname: cloud-gateway-service

  client:

    service-url:

      register-with-eureka: true

      fetch-registry: true

      defaultzone: http://eureka7001.com:7001/eureka

六、predicate的使用

详解SpringCloud新一代网关Gateway

七、filter的使用

7.1、作用

  • 路由过滤器可用于修改进入的http请求和返回的http响应,路由过滤器只能指定路由进行使用。
  • spring cloud gateway内置y多种路由过滤器,他们都由gatewayfilter的工厂 类来产生

7.2、spring cloud gateway的filter

生命周期,only two

  • pre 请求之前
  • post 请求之后

种类,only two

  • gatewayfilter 单一的
  • globalfilter 全局的

7.3、自定义过滤器

两个接口介绍:globalfilter,ordered

功能:

  • 全局日志记录
  • 统一网关鉴权

案例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.rw.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;
 
@component
public class myloggatewayfilter implements globalfilter, ordered {
    @override
    public mono<void> filter(serverwebexchange exchange, gatewayfilterchain chain) {
        system.out.println("*********com in myloggatewayfilter"+new date());
        string name=exchange.getrequest().getqueryparams().getfirst("uname");
        if(name==null){
            system.out.println("******用户名为null,非法用户");
            exchange.getresponse().setstatuscode(httpstatus.not_acceptable);
            return exchange.getresponse().setcomplete();
        }
        return chain.filter(exchange);
    }
 
    @override
    public int getorder() {
        return 0;
    }
}

效果:请求地址中带由uname才让访问http://localhost:9527/payment/lb?uname=z3

以上就是详解springcloud新一代网关gateway的详细内容,更多关于springcloud gateway的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/weixin_45498245/article/details/109514463