Java小强个人技术博客站点    手机版
当前位置: 首页 >> 框架 >> Spring Cloud Gateway 11种断言工厂

Spring Cloud Gateway 11种断言工厂

17810 框架 | 2022-2-7

Spring Cloud Gateway路由匹配是Spring WebFlux基础功能的一部分,在Spring Cloud Gateway中内置了很多路由断言工厂类。不同的断言工厂类针对HTTP请求的不同属性。多个断言工厂类可以使用逻辑“and”进行组合使用。

360截图20220207165501397.jpg

官方文档:

https://spring.io/projects/spring-cloud-gateway 

https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-after-route-predicate-factory 


POM引入:

<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

这里注册中心使用Nacos,配置Nacos地址

server:
  port: 10010
spring:
  application:
    name: gatewayservice
  cloud:
    nacos:
      server-addr: 192.168.2.22:8848 # Nacos地址

After Route Predicate Factory

这个Predicate工厂的实现类是AfterRoutePredicateFactory,使用一个时间参数,如果当前请求的时间在配置的赶时间之后,此断言才会返回true。在application.yml中的配置如下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: http://www.javacui.com  # 如果断言返回true,路由到的URI
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

注意这个类使用的时间类是ZonedDateTime,表示ISO-8601日历系统中具有时区的日期时间,所以在application.yml中的时间配置格式只能是:2017-01-20T17:42:47.789-07:00[America/Denver],字符串的时间格式转化为ZonedDateTime使用的是StringToZonedDateTimeConverter类。如果想使用其它的时间格式,得需要自己实现一个单独的转化类了。通过源码中的测试例子可以看到,如果请求在配置的时间之前,网关会返回404,如果在配置的时间之后,网关路由成功到http://www.javacui.com 网站。


Before Route Predicate Factory

这个Predicate工厂的实现类是BeforeRoutePredicateFactory,它和AfterRoutePredicateFactory的实现基本上是一致的,在application.yml中的配置如下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: http://www.javacui.com # 路由到的URI
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

如果当前请求的时间在配置的时间之前,此断言返回true。


Between Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: http://www.javacui.com
        predicates:
        - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

当请求网关的时间在datatime1之后,在datetim2之前时,这个断言返回true。这个断言对于维护一个时间窗口很有用。比如限制在基某段时间内开启的活动,非这个时间段不可以访问等。


Cookie Route Predicate Factory

这个Predicate工厂的实现类是CookieRoutePredicateFactory,它有两个参数,一个是name,另一个是正则表达式。在application.,yml中的配置如下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: http://www.javacui.com
        predicates:
			- Cookie=username, javacui

如果请求的Cookie中有name的值,并且根据name取出的值都匹配配置的正则表达式,这个断方就返回true。


Header Route Predicate Factory

这个Predicate工厂的实现类是HeaderRoutePredicateFactory,它有两个参数,一个是name,另一个是正则表达式。在application.yml中的配置如下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: http://www.javacui.com
        predicates:
        - Header=X-Request-Id, \d+

如果请求的Header里面有name的值,并且它的值与配置的正则表达式匹配,则断言返回true,如果没有配置正则表达式的值,断言也是返回true(断方只检测带正则表达式的配置)。上面的配置示例表示Header中必须有X-Request-Id,且它的值必须是数字。


Host Route Predicate Factory

这个Predicate的工厂的实现类是HostRoutePredicateFactory,这有一个参数,这个参数是一人List列表,它可以包含多个主机名字匹配的样式。它遵循Ant的样式风格,以点(.)分隔。在application.yml中的配置如下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://www.javacui.com
        predicates:
        - Host=**.javacui.org,**.javacui.org

如果请求的Header中的Host的值是www.javacui.com,www.javacui.org,这个断言就会返回true。

在断方工厂中会提取上面配置中Host对应的URI模板变量(比如上面的sub),把匹配的URI放到一个Map中,这个Map会被添加到ServerWebExchange.getAttributes(ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE)的属性集中。可以在后面的Gateway Filter工厂类使用。

注意,在配置Host的时候,如果Host不是80端口,在配置的时候也需要添加上端口。如:localhost:8080。


Method Route Predicate Factory

这个Predicate的实现类是MethodRoutePredicateFactory,它有一个参数:指定的Http方法名。在application.yml中的配置如下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://www.javacui.com
        predicates:
        - Method=GET

如果这个请求的方法名是GET,断言将返回true。


Path Route Predicate Factory

这个Predicate的实现类是PathRoutePredicateFactory,它有两个参数,一个是匹配样式列表,另一个是boolean值,表示是否匹配分隔线。在application.yml中的配置好下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: http://www.javacui.com
        predicates:
        - Path=/foo/{segment},/bar/{segment}

如果请求的URI中的路径是/foo/1,/foo/bar或/bar/baz,这个断言将返回true。这个断言工厂将会提取配置的URI格式中的变量(比如上面配置的segment),并将它转化为Map,替换掉ServerWebExchange.getAttributes(ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE)中的值。这个值可以在GatewayFilter工厂中使用。有一个方法可以方便的访问这些值,如下面代码所示:

Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);
String segment = uriVariables.get("segment");


Query Route Predicate Factory

这个是参数路由断言工厂,它的实现类是QueryRoutePredicateFactory,它有两个参数,一个是参数(param),这个是必须的,另一个是可选参数,是一个正则表达式。在application.yml中不带正则的配置如下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://www.javacui.com
        predicates:
        - Query=baz

如果请求的参数中包含baz参数,断言将返回true

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://www.javacui.com
        predicates:
        - Query=foo, ba.

如果请求的参数中包含foo,且它的值匹配ba.,断言将返回true。


RemoteAddr Route Predicate Factory

这个是远程地址路由断言工厂,它的实现类是RemoteAddrRoutePredicateFactory,它有一个List列表的参数,这些参数是CIDR-notation(IPv4和IPv6)的地址字符串,比如192.168.0.1/16(192.168.0.1是ip地址,16是一个子网掩码)。在application.yml中的配置如下所示:

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: http://www.javacui.com
        predicates:
        - RemoteAddr=192.168.1.1/24

如果请求的客户端的ip地址是192.168.1.1到192.168.1.24的范围,此断言返回true。


Weight Route Predicate Factory

Spring Cloud Gateway 提供了基于路由权重的断言工厂,配置时指定分组和权重值 即可。WeightRoutePredicateFactory 实现了路由权重的功能,按照路由权重选择 同一个分组中的路由。

The Weight route predicate factory takes two arguments: group and weight (an int). The weights are calculated per group. The following example configures a weight spring:

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
			- Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
			- Weight=group1, 2

This route would forward ~80% of traffic to weighthigh.org and ~20% of traffic to weightlow.org


推荐您阅读更多有关于“ spring Cloud Gateway Route Predicate Factories ”的文章

上一篇:Spring Cloud Gateway 全局过滤器 下一篇:zookeeper安装集群模式

猜你喜欢

发表评论: