Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。 Spring Cloud Gateway包括许多内置的Route Predicate工厂。 所有这些Predicate都与HTTP请求的不同属性匹配。 多个Route Predicate工厂可以进行组合。 Spring Cloud Gateway内置了许多Predict,这些Predict的源码在org.springframework.cloud.gateway.handler.predicate包中,如果读者有兴趣可以阅读一下。现在列举各种Predicate如下图:
具体UML类图如下图:
下面我们来介绍下一些常用的Route Predicate。
注意:Predicate中提到的配置都在application-predicate.yml文件中进行修改,并用该配置启动Spring Cloud Gateway服务。
在指定时间之后的请求会匹配该路由。
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 # 当且仅当请求时的时间After配置的时间时,才会转发到用户微服务 # 目前配置不会进该路由配置,所以返回404 # 将时间改成 < now的时间,则访问http://localhost:9100/** -> eureka-client-provider/** # eg. 访问http://localhost:9100/provider/demo/hello-> eureka-client-provider/demo/hello - After=2020-07-01T23:33:00.000+08:00[Asia/Shanghai] filters: - StripPrefix=1在指定时间之前的请求会匹配该路由。
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 # 当且仅当请求时的时间Before配置的时间时,才会转发到用户微服务 # 目前配置不会进该路由配置,所以返回404 # 将时间改成 > now的时间,则访问http://localhost:9100/** -> eureka-client-provider/** # 访问http://localhost:9100/provider/demo/hello -> eureka-client-provider/demo/hello - Before=2021-07-01T23:33:00.000+08:00[Asia/Shanghai] filters: - StripPrefix=1在指定时间区间内的请求会匹配该路由。
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 # 当且仅当请求时的时间Between配置的时间时,才会转发到用户微服务 # 因此,访问http://localhost:9100/** -> eureka-client-provider/** # 访问http://localhost:9100/provider/demo/hello -> eureka-client-provider/demo/hello - Between=2020-07-01T13:30:00+08:00[Asia/Shanghai], 2020-07-02T18:30:00+08:00[Asia/Shanghai] filters: - StripPrefix=1带有指定Cookie的请求会匹配该路由。
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 # 当且仅当带有名为somecookie,并且值符合正则ch.p的Cookie时,才会转发到用户微服务 # 如Cookie满足条件,则访问http://localhost:9100/** -> eureka-client-provider/** # 访问http://localhost:9100/provider/demo/hello -> eureka-client-provider/demo/hello - Cookie=username,tom filters: - StripPrefix=1使用curl工具测试如下,只有cookie为 username=tom 的请求可以匹配该路由。
带有指定请求头的请求会匹配该路由。
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 # 当且仅当带有名为X-Request-Id,并且值符合正则\d+的Header时,才会转发到用户微服务 # 如Header满足条件,则访问http://localhost:9100/** -> eureka-client-provider/** # 访问http://localhost:9100/provider/demo/hello -> eureka-client-provider/demo/hello - Header=X-Request-Id, \d+ filters: - StripPrefix=1使用curl工具测试如下,只有名为X-Request-Id,并且值符合正则\d+的Header时可以匹配该路由。
带有指定Host的请求会匹配该路由
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 # 当且仅当名为Host的Header符合**.hxmec.com或者**.heisea.cn时,才会转发用户微服务 # 如Host满足条件,则访问http://localhost:9100/** -> eureka-client-provider/** # http://localhost:9100/provider/demo/hello -> eureka-client-provider/demo/hello - Host=**.hxmec.com,**.heisea.cn filters: - StripPrefix=1 使用curl工具测试如下,只有带有请求头名为Host的Header符合**.hxmec.com或者**.heisea.cn时请求可以匹配该路由。发送指定方法的请求会匹配该路由。
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 # 当且仅当HTTP请求方法是GET时,才会转发用户微服务 # 如请求方法满足条件,则访问http://localhost:9100/** -> eureka-client-provider/** # 访问http://localhost:9100/provider/demo/hello -> eureka-client-provider/demo/hello - Method=GET filters: - StripPrefix=1使用curl测试如下,只有HTTP请求方法是GET时请求可以匹配该路由。
发送指定路径的请求会匹配该路由。
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则使用curl测试如下,只有访问路径为/provider/xxx 时请求可以匹配该路由。
带指定查询参数的请求可以匹配该路由
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 # 当且仅当请求带有name的参数,才会转发到用户微服务 # 访问http://localhost:9100/provider/demo/feign?name=xx -> eureka-client-provider的/demo/feign - Query=name filters: - StripPrefix=1使用curl测试,只有带 name=xxx 查询参数的请求可以匹配该路由。
从指定远程地址发起的请求可以匹配该路由。
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 # 当且仅当请求IP是192.168.29.1/24网段,例如192.168.29.188,才会转发到用户微服务 # 问http://localhost:9100/provider/demo/hello -> eureka-client-provider/demo/hello - RemoteAddr=192.168.29.1/24 filters: - StripPrefix=1使用curl测试,只有请求IP在192.168.29.1/24网段会转发到用户微服务
使用权重来路由相应请求
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这个路由规则表示有80%的请求会被路由到weighthigh.org,20%会被路由到weighlow.org。
基于请求体内容的断言工厂 将body中的内容读取到exchange对象中,使用exchange.getAttribute(“cachedRequestBodyObject”)获取
spring: cloud: gateway: routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 - name: ReadBodyPredicateFactory #读取body断言 args: inClass: "#{T(String)}" #body数据类型 predicate: "#{@testRequestBody}" #自定义断言处理器 filters: - StripPrefix=1 @Component public class TestRequestBody implements Predicate { /** * 根据内容判断是否匹配该路由 * @param o body的内容 * @return ture-匹配成功,false-匹配失败 */ @Override public boolean test(Object o) { //可以对body内容进行判断处理 //这里不做处理直接返回成功 return true; } }