Spring Cloud Gateway是什么?(官网地址:https://cloud.spring.io/spring-cloud-gateway/reference/html/

 Spring Cloud Gateway是建立在Spring 5, Spring Boot 2 and Project Reactor这几个项目上的API网关,它是由spring团队自己开发的,spring的亲儿子。Spring Cloud Gateway旨在提供一种简单而有效的方法来路由到api,并为它们提供跨领域的关注点,例如:安全性、监控/度量和弹性。

cloud全家桶在1.x版本中推荐用的网关是zuul,但是在2.x版本中,Zuul的升级一直跳票,spring团队最后自己研发了一个网关代替Zuul,就是Spring Cloud Gateway。Spring Cloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则是使用了高性能的Reactor模式通信框架Netty。

 

Spring Cloud Gateway有什么用?

  • 性能:API高可用,负载均衡,容错机制。
  • 安全:权限身份认证、脱敏,流量清洗,后端签名(保证全链路可信调用),黑名单(非法调用的限制)。
  • 日志:日志记录(spainid,traceid)一旦涉及分布式,全链路跟踪必不可少。
  • 缓存:数据缓存。
  • 监控:记录请求响应数据,api耗时分析,性能监控。
  • 限流:流量控制,错峰流控,可以定义多种限流规则。
  • 灰度:线上灰度部署,可以减小风险。
  • 路由:动态路由规则。

 

 

Spring Cloud Gateway的特性:

1、动态路由:能够匹配任何请求属性。

2、可以对路由指定Predicate(断言)和Filter(过滤),断言和过滤易于编写。

3、集成Hystrix断路器功能、Eureka服务发现功能。

4、请求限流功能,支持路径重写。

 

在Spring Cloud Finchley正式版之前推荐使用的网关是Netflex提供的Zuul,在此之后推荐使用的是Spring Cloud Gateway。

Spring Cloud Gateway和Zuul的区别:

1、Zuul 1.x是一个基于阻塞I/O的API网关。

2、Zuul 1.x是基于Servlet 2.5使用阻塞架构的,它不支持任何长连接(如WebSocket),Zuul的设计模式和Nginx较像,每次I/O操作都是从工作线程中选择一个执行,请求线程阻塞到工作线程完成,但是差别是Nginx是用C++实现的,Zuul是用Java实现的,而JVM本身会有第一次加载比较慢的情况,使得Zuul性能会相对较差。

3、Zuul 2.x理念是基于Netty非阻塞和支持长连接,性能方面比Zuul 1.x有很大的提升,但是因为Netflex跳票,还没有发布 。根据官方提供的基准测试,Spring Cloud Gateway的RPS(每秒请求数)是Zuul的1.6倍。

4、Spring Cloud Gateway是建立在Spring 5, Spring Boot 2 and Project Reactor之上,Spring Cloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则是使用了高性能的Reactor模式通信框架Netty,是一个非阻塞I/O的API网关。

5、Spring Cloud Gateway还支持WebSocket,并且与Spring紧密集成拥有更好的开发体验。

 

Spring Cloud Gateway三大核心概念:

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

2、Predicate(断言):参考的是Java8中的java.util.function.Predicate,开发人员匹配HTTP请求中的内容进行断言,如果请求与断言匹配则路由。

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

 

Spring Cloud Gateway工作流程如下官网提供的图片:

1、客户端向Spring Cloud Gateway发送请求,然后在Gateway Hangdler Mapping中找到与请求相匹配的路由,将其发送到Gateway Web Handler。

2、Handler再通过指定的过滤器链来将请求发送到我们实际服务执行业务逻辑,然后返回。

3、过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或者之后(“post”)执行业务逻辑。

4、Filter在“pre”类型的过滤器可以做参数校验、权限校验、流量监控、日志输出、协议转换等。在“post”类型的过滤器中可以做响应内容、响应头的修改,日志的输出,流量监控等。

5、核心逻辑就是路由转发和执行过滤链。

 

 

Spring Cloud Gateway实践

1、gateway在pom.xml加以下依赖包:

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.2.4.BUILD-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

 

2、gateway配置文件如下进行配置:

server:
  port: 8080
  tomcat:
    uri-encoding: UTF-8
    basedir: /tmp/tomcat
    connection-timeout: 60000

spring:
  application:
    name: demo-gateway
  cloud:
    gateway:
      discovery:
        locator:
          #开启从注册中心动态创建路由的功能,利用微服务名进行路由
          enabled: true
      routes:
        - id: demo-client
          #uri: http://localhost:8101
          uri: lb://demo-client
          predicates:
            - Path=/consumer/consumer/**
          #路由的id,没有固定规则,建议配合服务名使用
        - id: demo-user
          #提供服务的路由地址
          #uri: http://localhost:8100
          uri: lb://demo-user
          #断言,路径相匹配就进行路由
          predicates:
            - Path=/user/user/**


eureka:
  instance:
    hostname: gateway-service
  client:
    service-url:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://localhost:8761/eureka/

 

3、在GatewayApplication加上注解@EnableDiscoveryClient

 

4、访问http://localhost:8080/user/user/test?msg=1235

Spring Cloud Gateway服务网关github地址:https://github.com/yuanzipeng/spring-cloud-gateway

 

 

 

posted on 2020-06-16 10:40  袁子弹  阅读(1001)  评论(0编辑  收藏  举报