Spring Cloud Gateway功能模块

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/it_lihongmin/article/details/90774723

目录

一、Spring Cloud Gateway概览和特性

二、Spring Cloud Gateway集成

三、Spring Cloud Gateway专业术语和工作原理

1、专业术语

2、工作原理

四、路由Predicate工厂

五、过滤器工厂

六、全局filter

七、ssl支持

八、RouteDefinitionLocator编码方式配置路由

九、CORS配置方式实现跨域


一、Spring Cloud Gateway概览和特性

    之前有写过Spring Cloud Netflix Zuul(Spring Cloud Zuul的Api网关实现Spring Cloud Zuul详细说明)的文章,Zuul分为1.x和2.x版本。但是首先肯定的是Spring Cloud Gateway的性能高于Zuul,但是Gateway基于Spring 5Spring Boot 2Project Reactor实现(对版本的要求比较高)。Spring Cloud Gateway旨在提供一种简单而有效的方法来路由到api,并为它们提供交叉关注点,例如:安全性、监视/度量和弹性。

    特性:

构建在Spring 5,Spring Boot 2,Project Reactor之上

能够为请求进行路由(Api网关)

Predicates和Filter为每个路由进行特殊请求处理

Hystrix断路器的集成

Spring Cloud注册中心支持

请求频率限制

重写请求(与Nginx的类似)

二、Spring Cloud Gateway集成

 在start.spring.io中添加gateway模块,或者在pom中添加如下依赖:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

引入了gateway相关maven依赖后默认开启Sping Cloud Gateway,但是也可以在bootstrap.properties(或者yml)添加配置进行关闭:

spring.cloud.gateway.enabled=false

三、Spring Cloud Gateway专业术语和工作原理

1、专业术语

Route:路由是网关的基本模块,由id,目标uri、Predicate集合和Filter集合组成

Predicate:Predicate是一个Java8的函数,输入类型是Spring的ServerWebExchange,允许匹配来自Http的任何内容,如请求头或者参数。

Filter:使用特定工厂构建的Spring GatewayFilter实例,可以在发生下游请求之前修改请求信息或者响应请求之后修改返回内容(这一点与Zuul的过滤器一致)。

2、工作原理

    客户端发送请求到Spring Cloud Gateway服务,如果请求能够进行映射并且满足路由则发送到Gateway Web Handler,再根据配置的Filter调用链在请求前或者请求后进行业务逻辑处理。

四、路由Predicate工厂

  1. After Route Predicate Factory
  2. Before Route Predicate Factory
  3. Between Route Predicate Factory
  4. Cookie Route Predicate Factory
  5. Header Route Predicate Factory
  6. Host Route Predicate Factory
  7. Method Route Predicate Factory
  8. Path Route Predicate Factory
  9. Query Route Predicate Factory
  10. RemoteAddr Route Predicate Factory

五、过滤器工厂

  1. AddRequestHeader GatewayFilter Factory
  2. AddRequestParameter GatewayFilter Factory
  3. AddResponseHeader GatewayFilter Factory
  4. DedupeResponseHeader GatewayFilter Factory
  5. Hystrix GatewayFilter Factory
  6. FallbackHeaders GatewayFilter Factory
  7. PrefixPath GatewayFilter Factory
  8. PreserveHostHeader GatewayFilter Factory
  9. RequestRateLimiter GatewayFilter Factory
  10. RedirectTo GatewayFilter Factory
  11. RemoveHopByHopHeadersFilter GatewayFilter Factory
  12. RemoveRequestHeader GatewayFilter Factory
  13. RemoveResponseHeader GatewayFilter Factory
  14. RewritePath GatewayFilter Factory
  15. RewriteResponseHeader GatewayFilter Factory
  16. SaveSession GatewayFilter Factory
  17. SecureHeaders GatewayFilter Factory
  18. SetPath GatewayFilter Factory
  19. SetResponseHeader GatewayFilter Factory
  20. SetStatus GatewayFilter Factory
  21. StripPrefix GatewayFilter Factory
  22. Retry GatewayFilter Factory
  23. RequestSize GatewayFilter Factory
  24. Modify Request Body GatewayFilter Factory
  25. Modify Response Body GatewayFilter Factory
  26. Default Filters

六、全局filter

  1. Combined Global Filter and GatewayFilter Ordering
  2. Forward Routing Filter
  3. LoadBalancerClient Filter
  4. Netty Routing Filter
  5. Netty Write Response Filter
  6. RouteToRequestUrl Filter
  7. Websocket Routing Filter
  8. Gateway Metrics Filter
  9. Marking An Exchange As Routed

七、ssl支持

需要支持是再添加

八、RouteDefinitionLocator编码方式配置路由

    使用RouteDefinitionLocator进行路由、Predicate、Filter的配置,即可以有两种实现方式,配置文件和编码的方式,如下:

@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
    return builder.routes()
        .route(r ->
            r.path("/kevin/**")
            .filters(
                f -> f.stripPrefix(1)
                )
                .uri("http://127.0.0.1:8090/helloWorld")
			)
			.build();
}

九、CORS配置方式实现跨域

spring:
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "https://www.kevin.com"
            allowedMethods:
            - GET

关于也是类似Nginx等网关服务常用的解决手段之一,Spring Cloud Gateway这样配置后,Get请求 www.kevin.com的服务就可以进行跨域访问了,至于跨域相关的问题和解决方案可以参见我之间写的跨域分享问题。 

猜你喜欢

转载自blog.csdn.net/it_lihongmin/article/details/90774723