Gateway3常用Predicate,Filter

  • GateWay常用的Predicate:

Route Predicate Factories这个是什么

  1. Spring Cloud Gateway matches routes as part of the Spring WebFlux HandlerMapping infrastructure. Spring Cloud Gateway includes many built-in route predicate factories. All of these predicates match on different attributes of the HTTP request. You can combine multiple route predicate factories with logical and statements

pring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。
Spring Cloud Gateway包括许多内置的Route Predicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配。多个RoutePredicate工厂可以进行组合。
Spring Cloud Gateway创建Route 对象时,使用RoutePredicateFactory 创建 Predicate对象,Predicate 对象可以赋值给Route。Spring Cloud Gateway包含许多内置的Route Predicate Factories。
所有这些谓词都匹配HTTP请求的不同属性。多种谓词工厂可以组合,并通过逻辑and。
常用的Route Predicate Factory

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

实现几个Route Predicate Factory

  • The After Route Predicate Factory
  • The Between Route Predicate Factory
  1. yml
  1. server:
  2. port: 9527
  3. spring:
  4. application:
  5. name: zds-egg-gateway
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: 192.168.34.126:8848
  10. #############################新增网关配置###########################
  11. gateway:
  12. discovery:
  13. locator:
  14. enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
  15. routes:
  16. - id: biz_routh1 #路由的ID,没有固定规则但要求唯一,建议配合服务名
  17. # uri: http://localhost:8082 #匹配后提供服务的路由地址
  18. uri: lb://zds-egg-biz
  19. predicates:
  20. - Path=/timeout/** # 断言,路径相匹配的进行路由
  21. # 这个时间后才能起效
  22. - After=2021-12-01T14:19:22.070+08:00[Asia/Shanghai]
  23. # 两个时间点之间
  24. # - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
  25. - id: biz_routh2 #路由的ID,没有固定规则但要求唯一,建议配合服务名
  26. # uri: http://localhost:8082 #匹配后提供服务的路由地址
  27. uri: lb://zds-egg-biz
  28. predicates:
  29. - Path=/selectStuById # 断言,路径相匹配的进行路由

可以通过下述方法获得上述格式的时间戳字符串

  1. public class T2
  2. {
  3. public static void main(String[] args)
  4. {
  5. ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区
  6. System.out.println(zbj);
  7. //2021-12-01T16:19:22.070+08:00[Asia/Shanghai]
  8. }
  9. }
  • The Cookie Route Predicate Factory ``` server: port: 9527

spring: application: name: zds-egg-gateway cloud: nacos: discovery: server-addr: 192.168.34.126:8848

  1. #############################新增网关配置###########################
  2. gateway:
  3. discovery:
  4. locator:
  5. enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
  6. routes:
  7. - id: biz_routh1 #路由的ID,没有固定规则但要求唯一,建议配合服务名

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

  1. uri: lb://zds-egg-biz
  2. predicates:
  3. - Path=/timeout/** # 断言,路径相匹配的进行路由
  4. # 这个时间后才能起效
  5. - After=2021-12-01T14:19:22.070+08:00[Asia/Shanghai]
  6. # 两个时间点之间

- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

  1. - Cookie=username,zzyy
  2. - id: biz_routh2 #路由的ID,没有固定规则但要求唯一,建议配合服务名

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

  1. uri: lb://zds-egg-biz
  2. predicates:
  3. - Path=/selectStuById # 断言,路径相匹配的进行路由 http://localhost:8082/selectStuById?id=1
  1. 测试

该命令相当于发get请求,且没带cookie

curl http://localhost:9527/timeout/10

带cookie的

curl http://localhost:9527/timeout/10 —cookie “username=zzyy”

  1. - **The Header Route Predicate Factory**

server: port: 9527

spring: application: name: zds-egg-gateway cloud: nacos: discovery: server-addr: 192.168.34.126:8848

  1. #############################新增网关配置###########################
  2. gateway:
  3. discovery:
  4. locator:
  5. enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
  6. routes:
  7. - id: biz_routh1 #路由的ID,没有固定规则但要求唯一,建议配合服务名

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

  1. uri: lb://zds-egg-biz
  2. predicates:
  3. - Path=/timeout/** # 断言,路径相匹配的进行路由
  4. # 这个时间后才能起效

- After=2021-12-01T14:19:22.070+08:00[Asia/Shanghai]

  1. # 两个时间点之间

- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

- Cookie=username,zzyy

  1. - Header=X-Request-Id, \d+ # 请求头需要X-Request-Id属性并且值为整数的正则表达式
  2. - id: biz_routh2 #路由的ID,没有固定规则但要求唯一,建议配合服务名

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

  1. uri: lb://zds-egg-biz
  2. predicates:
  3. - Path=/selectStuById # 断言,路径相匹配的进行路由 http://localhost:8082/selectStuById?id=1
  1. 测试

curl http://localhost:9527/timeout/10 -H “X-Request-Id:123”

  1. ---
  2. ### Filter:
  3. [Spring Cloud Gateway](https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories)<br />路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。Spring Cloud Gateway内置了多种路由过滤器,他们都由GatewayFilter的工厂类来产生。<br />Spring Cloud Gateway的Filter:
  4. -
  5. 生命周期:
  6. - pre
  7. - post
  8. -
  9. 种类(具体看官方文档):
  10. - GatewayFilter - 31
  11. - GlobalFilter - 10
  12. 常用的GatewayFilterAddRequestParameter GatewayFilter<br />自定义全局GlobalFilter:<br />两个主要接口介绍:
  13. 1. GlobalFilter
  14. 1. Ordered
  15. 能干什么:
  16. 1. 全局日志记录
  17. 1. 统一网关鉴权
  18. 1. ...
  19. **代码案例:**<br />1.新建包filter,类MyLogGateWayFilter, 注释之前的断言

@Slf4j @Component public class MyLogGateWayFilter implements GlobalFilter, Ordered {

  1. @Override
  2. public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  3. log.info("***********come in MyLogGateWayFilter: "+new Date());
  4. String uname = exchange.getRequest().getQueryParams().getFirst("uname");
  5. if(uname == null)
  6. {
  7. log.info("*******用户名为null,非法用户,o(╥﹏╥)o");
  8. exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
  9. return exchange.getResponse().setComplete();
  10. }
  11. return chain.filter(exchange);
  12. }
  13. @Override
  14. public int getOrder() {
  15. return 0;
  16. }

}

``` 测试
http://localhost:9527/timeout/10 访问异常
http://localhost:9527/timeout/10?uname=1 访问正常