失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【Hoxton.SR1版本】Spring Cloud Gateway之Predicate详解

【Hoxton.SR1版本】Spring Cloud Gateway之Predicate详解

时间:2021-12-25 21:36:22

相关推荐

【Hoxton.SR1版本】Spring Cloud Gateway之Predicate详解

目录

一、简介

二、Predicate断言详解

三、总结

一、简介

前面一篇文章,我们介绍有关Gateway新一代网关的概念以及与旧网关Zuul的对比,并通过一些简单的示例说明了gateway中路由如何配置、实现动态路由等,本篇文章我们主要介绍Gateway中强大的Predicate断言并结合示例详细说明其用法。

二、Predicate断言详解

Spring Cloud官网提供了11中断言的使用方法,如下图所示:

通过Gatewayt9527的启动日志,我们也可以看到:

-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [After]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Before]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Between]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Cookie]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Header]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Host]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Method]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Path]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Query]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [ReadBodyPredicateFactory]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [RemoteAddr]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Weight]-08-19 16:12:54.275 INFO 16404 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [CloudFoundryRouteService]

前面一篇文章我们只使用到了The Path Route Predicate Factory路径路由断言方式,接下来依次对其他几种断言结合示例介绍其用法。

(一)、After断言

After 路由断言工厂接受一个参数datetime(Java里面的ZonedDateTime类格式的时间)。此断言匹配发生在指定日期时间之后的请求。下面的示例配置了一个after断言:

【a】服务提供者添加如下测试方法:

/*** 测试gateway网关After断言*/@GetMapping("/gatewayAfterRoutePredicate/{name}")public String gatewayAfterRoutePredicate(@PathVariable("name") String name) {return "hello, [gatewayAfterRoutePredicate] the name is :" + name;}

【b】application.yml中加入如下配置:

server:port: 9527spring:application:name: springcloud-gatewaycloud:gateway:routes:#如下配置表示,当浏览器URL访问localhost:9527/gatewaySimple/**的时候,gateway网关会帮忙转发到http://localhost:8001/gatewaySimple/**去.- id: payment8001_gatewaySimple #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewaySimple/** # 断言,路径相匹配的进行路由#如下配置表示,当浏览器URL访问localhost:9527/gatewayLoadBalance/**的时候,gateway网关负载均衡地将请求分发到springcloud-payment-service服务中去.- id: payment_service_loadbalance #路由IDuri: lb://springcloud-payment-service #springcloud-payment-service为服务提供者注册到Eureka的应用名称(application name)predicates:- Path=/gatewayLoadBalance/** # 断言,路径相匹配的进行路由########################################【After Route Predicate】#######################################################如下配置表示,在配置的时间之后访问localhost:9527/gatewayAfterRoutePredicate/**的时候,,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayAfterRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayAfterRoutePredicate/**# - After=-08-19T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由eureka:instance:hostname: springcloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://springcloud-:7001/eureka/,http://springcloud-:7002/eureka/ #集群版Eureka注册中心

注意:ZonedDateTime格式的时间可以通过下面的方法生成:

public class Test {public static void main(String[] args) {ZonedDateTime zonedDateTime = ZonedDateTime.now();System.out.println(zonedDateTime);}}

【c】测试

重启网关,浏览器访问:http://localhost:9527/gatewayAfterRoutePredicate/weishihuai

如果当前时间小于配置的时间(将时间配置修改为

- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]

),那么访问将会报错:

总之,只有在指定的时间之后发出的请求gateway才会帮忙路由。

(二)、Before断言

Before路由断言工厂接受一个参数datetime(Java里面的ZonedDateTime类格式的时间)。此断言匹配发生在指定日期时间之前的请求。下面的示例配置了一个Before断言:

【a】服务提供者添加如下测试方法:

/*** 测试gateway网关Before断言*/@GetMapping("/gatewayBeforeRoutePredicate/{name}")public String gatewayBeforeRoutePredicate(@PathVariable("name") String name) {return "hello, [gatewayBeforeRoutePredicate] the name is :" + name;}

【b】application.yml中加入如下配置:

server:port: 9527spring:application:name: springcloud-gatewaycloud:gateway:routes:#如下配置表示,当浏览器URL访问localhost:9527/gatewaySimple/**的时候,gateway网关会帮忙转发到http://localhost:8001/gatewaySimple/**去.- id: payment8001_gatewaySimple #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewaySimple/** # 断言,路径相匹配的进行路由#如下配置表示,当浏览器URL访问localhost:9527/gatewayLoadBalance/**的时候,gateway网关负载均衡地将请求分发到springcloud-payment-service服务中去.- id: payment_service_loadbalance #路由IDuri: lb://springcloud-payment-service #springcloud-payment-service为服务提供者注册到Eureka的应用名称(application name)predicates:- Path=/gatewayLoadBalance/** # 断言,路径相匹配的进行路由########################################【After Route Predicate】#######################################################如下配置表示,在配置的时间之后访问localhost:9527/gatewayAfterRoutePredicate/**的时候,,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayAfterRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayAfterRoutePredicate/**# - After=-08-19T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Before Route Predicate】#######################################################如下配置表示,在配置的时间之前访问localhost:9527/gatewayBeforeRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBeforeRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBeforeRoutePredicate/**# - After=-08-10T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由eureka:instance:hostname: springcloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://springcloud-:7001/eureka/,http://springcloud-:7002/eureka/ #集群版Eureka注册中心

【c】重启网关,浏览器访问:http://localhost:9527/gatewayBeforeRoutePredicate/weishihuai

同上,只有访问时间在配置的时间之前时,断言才为true,网关才会进行跳转。

(三)、Between断言

Between断言:路由之间的断言工厂接受两个参数,datetime1和datetime2,它们是java ZonedDateTime对象。此断言匹配发生在datetime1之后和datetime2之前的请求。datetime2参数必须位于datetime1之后。下面的示例配置了一个between route断言:

【a】服务提供者添加如下测试方法:

/*** 测试gateway网关Between断言*/@GetMapping("/gatewayBetweenRoutePredicate/{name}")public String gatewayBetweenRoutePredicate(@PathVariable("name") String name) {return "hello, [gatewayBetweenRoutePredicate] the name is :" + name;}

【b】application.yml中加入如下配置:

server:port: 9527spring:application:name: springcloud-gatewaycloud:gateway:routes:#如下配置表示,当浏览器URL访问localhost:9527/gatewaySimple/**的时候,gateway网关会帮忙转发到http://localhost:8001/gatewaySimple/**去.- id: payment8001_gatewaySimple #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewaySimple/** # 断言,路径相匹配的进行路由#如下配置表示,当浏览器URL访问localhost:9527/gatewayLoadBalance/**的时候,gateway网关负载均衡地将请求分发到springcloud-payment-service服务中去.- id: payment_service_loadbalance #路由IDuri: lb://springcloud-payment-service #springcloud-payment-service为服务提供者注册到Eureka的应用名称(application name)predicates:- Path=/gatewayLoadBalance/** # 断言,路径相匹配的进行路由########################################【After Route Predicate】#######################################################如下配置表示,在配置的时间之后访问localhost:9527/gatewayAfterRoutePredicate/**的时候,,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayAfterRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayAfterRoutePredicate/**# - After=-08-19T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Before Route Predicate】#######################################################如下配置表示,在配置的时间之前访问localhost:9527/gatewayBeforeRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBeforeRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBeforeRoutePredicate/**# - Before=-08-10T20:41:32.999+08:00[Asia/Shanghai]- Before=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Between Route Predicate】#######################################################如下配置表示,在配置的时间之间访问localhost:9527/gatewayBetweenRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBetweenRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBetweenRoutePredicate/**- Between=-08-10T20:41:32.999+08:00[Asia/Shanghai], -08-29T20:41:32.999+08:00[Asia/Shanghai]discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由eureka:instance:hostname: springcloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://springcloud-:7001/eureka/,http://springcloud-:7002/eureka/ #集群版Eureka注册中心

【c】重启网关,浏览器访问:http://localhost:9527/gatewayBetweenRoutePredicate/weishihuai

同上,只有访问时间在配置的两个时间之间时,断言才为true,网关才会进行跳转,小伙伴们可以修改一下时间范围,如果请求时间不在那个范围内,gateway不会帮忙路由请求,访问同样会报错。

(四)、Cookie断言

Cookie断言:Cookie路由断言工厂接受两个参数,Cookie名称和regexp(一个Java正则表达式)。此断言匹配具有给定名称且其值与正则表达式匹配的cookie。下面的示例配置了一个cookie路由断言工厂:

【a】服务提供者添加如下测试方法:

/*** 测试gateway网关Cookie断言*/@GetMapping("/gatewayCookieRoutePredicate/{name}")public String gatewayCookieRoutePredicate(@PathVariable("name") String name) {return "hello, [gatewayCookieRoutePredicate] the name is :" + name;}

【b】application.yml中加入如下配置:

server:port: 9527spring:application:name: springcloud-gatewaycloud:gateway:routes:#如下配置表示,当浏览器URL访问localhost:9527/gatewaySimple/**的时候,gateway网关会帮忙转发到http://localhost:8001/gatewaySimple/**去.- id: payment8001_gatewaySimple #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewaySimple/** # 断言,路径相匹配的进行路由#如下配置表示,当浏览器URL访问localhost:9527/gatewayLoadBalance/**的时候,gateway网关负载均衡地将请求分发到springcloud-payment-service服务中去.- id: payment_service_loadbalance #路由IDuri: lb://springcloud-payment-service #springcloud-payment-service为服务提供者注册到Eureka的应用名称(application name)predicates:- Path=/gatewayLoadBalance/** # 断言,路径相匹配的进行路由########################################【After Route Predicate】#######################################################如下配置表示,在配置的时间之后访问localhost:9527/gatewayAfterRoutePredicate/**的时候,,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayAfterRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayAfterRoutePredicate/**# - After=-08-19T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Before Route Predicate】#######################################################如下配置表示,在配置的时间之前访问localhost:9527/gatewayBeforeRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBeforeRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBeforeRoutePredicate/**# - Before=-08-10T20:41:32.999+08:00[Asia/Shanghai]- Before=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Between Route Predicate】#######################################################如下配置表示,在配置的时间之间访问localhost:9527/gatewayBetweenRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBetweenRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBetweenRoutePredicate/**- Between=-08-10T20:41:32.999+08:00[Asia/Shanghai], -08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Cookie Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayCookieRoutePredicate/**的时候需要携带cookie参数,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayCookieRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayCookieRoutePredicate/**- Cookie=name, weishihuaidiscovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由eureka:instance:hostname: springcloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://springcloud-:7001/eureka/,http://springcloud-:7002/eureka/ #集群版Eureka注册中心

该路由匹配具有一个名为name的cookie的请求,该cookie的值与weishihuai正则表达式匹配。

【c】测试

重启网关,需要需要携带一些cookie参数,这里我们使用curl访问接口进行测试:

携带cookie参数并且值也匹配:curl http://localhost:9527/gatewayCookieRoutePredicate/weishihuai --cookie "name=weishihuai"携带cookie参数并且值不匹配:curl http://localhost:9527/gatewayCookieRoutePredicate/weishihuai --cookie "name=wsh"不携带cookie参数:curl http://localhost:9527/gatewayCookieRoutePredicate/weishihuai

只有请求携带cookie,并且cookie的值需要与正则表达式匹配,这样断言才为真,网关才会正常跳转。

(五)、Header断言

Header断言:消息头路由断言工厂接受两个参数,消息头名称和regexp(一个Java正则表达式)。此断言与具有给定名称的头匹配,该头的值与正则表达式匹配。下面的示例配置了一个请求头路由断言:

【a】服务提供者添加如下测试方法:

/*** 测试gateway网关Header断言*/@GetMapping("/gatewayHeaderRoutePredicate/{name}")public String gatewayHeaderRoutePredicate(@PathVariable("name") String name) {return "hello, [gatewayHeaderRoutePredicate] the name is :" + name;}

【b】application.yml中加入如下配置:

server:port: 9527spring:application:name: springcloud-gatewaycloud:gateway:routes:#如下配置表示,当浏览器URL访问localhost:9527/gatewaySimple/**的时候,gateway网关会帮忙转发到http://localhost:8001/gatewaySimple/**去.- id: payment8001_gatewaySimple #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewaySimple/** # 断言,路径相匹配的进行路由#如下配置表示,当浏览器URL访问localhost:9527/gatewayLoadBalance/**的时候,gateway网关负载均衡地将请求分发到springcloud-payment-service服务中去.- id: payment_service_loadbalance #路由IDuri: lb://springcloud-payment-service #springcloud-payment-service为服务提供者注册到Eureka的应用名称(application name)predicates:- Path=/gatewayLoadBalance/** # 断言,路径相匹配的进行路由########################################【After Route Predicate】#######################################################如下配置表示,在配置的时间之后访问localhost:9527/gatewayAfterRoutePredicate/**的时候,,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayAfterRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayAfterRoutePredicate/**# - After=-08-19T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Before Route Predicate】#######################################################如下配置表示,在配置的时间之前访问localhost:9527/gatewayBeforeRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBeforeRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBeforeRoutePredicate/**# - Before=-08-10T20:41:32.999+08:00[Asia/Shanghai]- Before=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Between Route Predicate】#######################################################如下配置表示,在配置的时间之间访问localhost:9527/gatewayBetweenRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBetweenRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBetweenRoutePredicate/**- Between=-08-10T20:41:32.999+08:00[Asia/Shanghai], -08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Cookie Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayCookieRoutePredicate/**的时候需要携带cookie参数,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayCookieRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayCookieRoutePredicate/**- Cookie=name, weishihuai########################################【Header Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHeaderRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHeaderRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHeaderRoutePredicate/**- Header=X-Request-Id, \d+discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由eureka:instance:hostname: springcloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://springcloud-:7001/eureka/,http://springcloud-:7002/eureka/ #集群版Eureka注册中心

【c】测试

携带参数并且值匹配:curl localhost:9527/gatewayHeaderRoutePredicate/weishihuai -H "X-Request-Id:123456"携带参数但值不匹配:curl localhost:9527/gatewayHeaderRoutePredicate/weishihuai -H "X-Request-Id:abcdef"不携带参数:curl localhost:9527/gatewayHeaderRoutePredicate/weishihuai

只有请求头中携带指定的参数,并且参数的值需要与正则表达式匹配,这样断言才为真,网关才会正常跳转。

(六)、Host断言

Host断言:主机路由断言工厂接受一个参数:主机名模式列表。该模式是一个ant样式的模式。作为分隔符。此断言匹配与模式匹配的主机头。下面的示例配置了一个主机路由断言:

【a】服务提供者添加如下测试方法:

/*** 测试gateway网关Host断言*/@GetMapping("/gatewayHostRoutePredicate/{name}")public String gatewayHostRoutePredicate(@PathVariable("name") String name) {return "hello, [gatewayHostRoutePredicate] the name is :" + name;}

【b】application.yml中加入如下配置:

server:port: 9527spring:application:name: springcloud-gatewaycloud:gateway:routes:#如下配置表示,当浏览器URL访问localhost:9527/gatewaySimple/**的时候,gateway网关会帮忙转发到http://localhost:8001/gatewaySimple/**去.- id: payment8001_gatewaySimple #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewaySimple/** # 断言,路径相匹配的进行路由#如下配置表示,当浏览器URL访问localhost:9527/gatewayLoadBalance/**的时候,gateway网关负载均衡地将请求分发到springcloud-payment-service服务中去.- id: payment_service_loadbalance #路由IDuri: lb://springcloud-payment-service #springcloud-payment-service为服务提供者注册到Eureka的应用名称(application name)predicates:- Path=/gatewayLoadBalance/** # 断言,路径相匹配的进行路由########################################【After Route Predicate】#######################################################如下配置表示,在配置的时间之后访问localhost:9527/gatewayAfterRoutePredicate/**的时候,,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayAfterRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayAfterRoutePredicate/**# - After=-08-19T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Before Route Predicate】#######################################################如下配置表示,在配置的时间之前访问localhost:9527/gatewayBeforeRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBeforeRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBeforeRoutePredicate/**# - Before=-08-10T20:41:32.999+08:00[Asia/Shanghai]- Before=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Between Route Predicate】#######################################################如下配置表示,在配置的时间之间访问localhost:9527/gatewayBetweenRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBetweenRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBetweenRoutePredicate/**- Between=-08-10T20:41:32.999+08:00[Asia/Shanghai], -08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Cookie Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayCookieRoutePredicate/**的时候需要携带cookie参数,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayCookieRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayCookieRoutePredicate/**- Cookie=name, weishihuai########################################【Header Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHeaderRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHeaderRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHeaderRoutePredicate/**- Header=X-Request-Id, \d+########################################【Host Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHostRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHostRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHostRoutePredicate/**- Host=**.,**.discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由eureka:instance:hostname: springcloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://springcloud-:7001/eureka/,http://springcloud-:7002/eureka/ #集群版Eureka注册中心

如果请求的主机头值为或或,则此路由匹配。

【c】测试

携带Host参数并且满足正则匹配:curl localhost:9527/gatewayHostRoutePredicate/weishihuai -H "Host:"携带Host参数但不满足正则匹配:curl localhost:9527/gatewayHostRoutePredicate/weishihuai -H "Host:"不携带Host参数:curl localhost:9527/gatewayHostRoutePredicate/weishihuai

只有携带Host参数并且参数值满足配置的正则表达式,gateway才会正确路由,否则报404。

(七)、Method断言

Method断言:方法路由断言工厂接受一个方法参数,该参数是一个或多个参数:要匹配的HTTP方法。下面的示例配置了一个方法路由断言:

【a】服务提供者添加如下测试方法:

/*** 测试gateway网关Method断言*/@GetMapping("/gatewayMethodRoutePredicate/{name}")public String gatewayMethodRoutePredicate(@PathVariable("name") String name) {return "hello, [gatewayMethodRoutePredicate] the name is :" + name;}

【b】application.yml中加入如下配置:

server:port: 9527spring:application:name: springcloud-gatewaycloud:gateway:routes:#如下配置表示,当浏览器URL访问localhost:9527/gatewaySimple/**的时候,gateway网关会帮忙转发到http://localhost:8001/gatewaySimple/**去.- id: payment8001_gatewaySimple #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewaySimple/** # 断言,路径相匹配的进行路由#如下配置表示,当浏览器URL访问localhost:9527/gatewayLoadBalance/**的时候,gateway网关负载均衡地将请求分发到springcloud-payment-service服务中去.- id: payment_service_loadbalance #路由IDuri: lb://springcloud-payment-service #springcloud-payment-service为服务提供者注册到Eureka的应用名称(application name)predicates:- Path=/gatewayLoadBalance/** # 断言,路径相匹配的进行路由########################################【After Route Predicate】#######################################################如下配置表示,在配置的时间之后访问localhost:9527/gatewayAfterRoutePredicate/**的时候,,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayAfterRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayAfterRoutePredicate/**# - After=-08-19T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Before Route Predicate】#######################################################如下配置表示,在配置的时间之前访问localhost:9527/gatewayBeforeRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBeforeRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBeforeRoutePredicate/**# - Before=-08-10T20:41:32.999+08:00[Asia/Shanghai]- Before=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Between Route Predicate】#######################################################如下配置表示,在配置的时间之间访问localhost:9527/gatewayBetweenRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBetweenRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBetweenRoutePredicate/**- Between=-08-10T20:41:32.999+08:00[Asia/Shanghai], -08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Cookie Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayCookieRoutePredicate/**的时候需要携带cookie参数,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayCookieRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayCookieRoutePredicate/**- Cookie=name, weishihuai########################################【Header Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHeaderRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHeaderRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHeaderRoutePredicate/**- Header=X-Request-Id, \d+########################################【Host Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHostRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHostRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHostRoutePredicate/**- Host=**.,**.########################################【Method Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayMethodRoutePredicate/**只有是get请求,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayMethodRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayMethodRoutePredicate/**- Method=GET# - Method=GET,POST# - Method=POSTdiscovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由eureka:instance:hostname: springcloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://springcloud-:7001/eureka/,http://springcloud-:7002/eureka/ #集群版Eureka注册中心

【c】测试:

curl localhost:9527/gatewayMethodRoutePredicate/weishihuai

修改配置文件:- Method=POST,重启网关,再次测试,发现报错,如下图:

只有请求的方法类型与配置的请求类型相符合时,网关才能正确路由。

(八)、Path断言

Path断言:路径路由断言工厂接受两个参数:Spring PathMatcher模式列表和一个称为matchOptionalTrailingSeparator的可选标志。以上所有示例都使用了Path断言,这里就不过多阐述。

(九)、Query断言

Query断言:查询路由断言工厂接受两个参数:一个必需的param和一个可选的regexp(一个Java正则表达式)。下面的示例配置了一个查询路由断言:

【a】服务提供者添加如下测试方法:

/*** 测试gateway网关Query断言*/@GetMapping("/gatewayQueryRoutePredicate/{name}")public String gatewayQueryRoutePredicate(@PathVariable("name") String name) {return "hello, [gatewayQueryRoutePredicate] the name is :" + name;}

【b】application.yml中加入如下配置:

server:port: 9527spring:application:name: springcloud-gatewaycloud:gateway:routes:#如下配置表示,当浏览器URL访问localhost:9527/gatewaySimple/**的时候,gateway网关会帮忙转发到http://localhost:8001/gatewaySimple/**去.- id: payment8001_gatewaySimple #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewaySimple/** # 断言,路径相匹配的进行路由#如下配置表示,当浏览器URL访问localhost:9527/gatewayLoadBalance/**的时候,gateway网关负载均衡地将请求分发到springcloud-payment-service服务中去.- id: payment_service_loadbalance #路由IDuri: lb://springcloud-payment-service #springcloud-payment-service为服务提供者注册到Eureka的应用名称(application name)predicates:- Path=/gatewayLoadBalance/** # 断言,路径相匹配的进行路由########################################【After Route Predicate】#######################################################如下配置表示,在配置的时间之后访问localhost:9527/gatewayAfterRoutePredicate/**的时候,,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayAfterRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayAfterRoutePredicate/**# - After=-08-19T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Before Route Predicate】#######################################################如下配置表示,在配置的时间之前访问localhost:9527/gatewayBeforeRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBeforeRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBeforeRoutePredicate/**# - Before=-08-10T20:41:32.999+08:00[Asia/Shanghai]- Before=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Between Route Predicate】#######################################################如下配置表示,在配置的时间之间访问localhost:9527/gatewayBetweenRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBetweenRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBetweenRoutePredicate/**- Between=-08-10T20:41:32.999+08:00[Asia/Shanghai], -08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Cookie Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayCookieRoutePredicate/**的时候需要携带cookie参数,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayCookieRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayCookieRoutePredicate/**- Cookie=name, weishihuai########################################【Header Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHeaderRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHeaderRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHeaderRoutePredicate/**- Header=X-Request-Id, \d+########################################【Host Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHostRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHostRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHostRoutePredicate/**- Host=**.,**.########################################【Method Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayMethodRoutePredicate/**只有是get请求,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayMethodRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayMethodRoutePredicate/**- Method=GET# - Method=GET,POST# - Method=POST########################################【Query Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayQueryRoutePredicate/**请求中需要携带id参数,并且值要是整形类型,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayQueryRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayQueryRoutePredicate/**# - Query=green- Query=id, \d+discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由eureka:instance:hostname: springcloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://springcloud-:7001/eureka/,http://springcloud-:7002/eureka/ #集群版Eureka注册中心

【c】测试

携带参数并且参数值满足正则表达式:curl localhost:9527/gatewayQueryRoutePredicate/weishihuai?id=123456携带参数但参数值不满足正则表达式:curl localhost:9527/gatewayQueryRoutePredicate/weishihuai?id=abcdef未携带参数:curl localhost:9527/gatewayQueryRoutePredicate/weishihuai

只有正确携带配置的参数,并且参数对应的值需要满足正则表达式的要求,gateway网关才会正确路由。

(十)、RemoteAddr断言

RemoteAddr断言:RemoteAddr路由断言工厂接受一个源列表(最小为1),这些源是cidr符号(IPv4或IPv6)字符串,比如192.168.0.1/16(其中192.168.0.1是一个IP地址,16是一个子网掩码)。下面的示例配置了一个RemoteAddr路由断言:

【a】服务提供者添加如下测试方法:

/*** 测试gateway网关RemoteAddr断言*/@GetMapping("/gatewayRemoteAddrRoutePredicate/{name}")public String gatewayRemoteAddrRoutePredicate(@PathVariable("name") String name) {return "hello, [gatewayRemoteAddrRoutePredicate] the name is :" + name;}

【b】application.yml中加入如下配置:

server:port: 9527spring:application:name: springcloud-gatewaycloud:gateway:routes:#如下配置表示,当浏览器URL访问localhost:9527/gatewaySimple/**的时候,gateway网关会帮忙转发到http://localhost:8001/gatewaySimple/**去.- id: payment8001_gatewaySimple #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewaySimple/** # 断言,路径相匹配的进行路由#如下配置表示,当浏览器URL访问localhost:9527/gatewayLoadBalance/**的时候,gateway网关负载均衡地将请求分发到springcloud-payment-service服务中去.- id: payment_service_loadbalance #路由IDuri: lb://springcloud-payment-service #springcloud-payment-service为服务提供者注册到Eureka的应用名称(application name)predicates:- Path=/gatewayLoadBalance/** # 断言,路径相匹配的进行路由########################################【After Route Predicate】#######################################################如下配置表示,在配置的时间之后访问localhost:9527/gatewayAfterRoutePredicate/**的时候,,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayAfterRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayAfterRoutePredicate/**# - After=-08-19T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Before Route Predicate】#######################################################如下配置表示,在配置的时间之前访问localhost:9527/gatewayBeforeRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBeforeRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBeforeRoutePredicate/**# - Before=-08-10T20:41:32.999+08:00[Asia/Shanghai]- Before=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Between Route Predicate】#######################################################如下配置表示,在配置的时间之间访问localhost:9527/gatewayBetweenRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBetweenRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBetweenRoutePredicate/**- Between=-08-10T20:41:32.999+08:00[Asia/Shanghai], -08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Cookie Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayCookieRoutePredicate/**的时候需要携带cookie参数,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayCookieRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayCookieRoutePredicate/**- Cookie=name, weishihuai########################################【Header Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHeaderRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHeaderRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHeaderRoutePredicate/**- Header=X-Request-Id, \d+########################################【Host Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHostRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHostRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHostRoutePredicate/**- Host=**.,**.########################################【Method Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayMethodRoutePredicate/**只有是get请求,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayMethodRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayMethodRoutePredicate/**- Method=GET# - Method=GET,POST# - Method=POST########################################【Query Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayQueryRoutePredicate/**请求中需要携带id参数,并且值要是整形类型,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayQueryRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayQueryRoutePredicate/**# - Query=green- Query=id, \d+########################################【RemoteAddr Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayRemoteAddrRoutePredicate/**请求的远程地址为127.0.0.1,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayRemoteAddrRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayRemoteAddrRoutePredicate/**# - RemoteAddr=127.0.0.1- RemoteAddr=192.168.35.105discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由eureka:instance:hostname: springcloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://springcloud-:7001/eureka/,http://springcloud-:7002/eureka/ #集群版Eureka注册中心

【c】测试

浏览器访问:http://127.0.0.1:9527/gatewayRemoteAddrRoutePredicate/weishihuai

可见,当请求从本机127.0.0.0发出时,

路由可以正确路由,下面我们修改配置文件中的远程地址为【- RemoteAddr=192.168.35.105】,重新访问,发现报错,如下图:

(十一)、Weight断言

Weight断言:权重路由断言工厂有两个参数:组和权重(int)。权重按组计算。下面的示例配置了一个权重路由断言:

【a】服务提供者添加如下测试方法:

/*** 测试gateway网关Weight断言*/@GetMapping("/gatewayWeightRoutePredicate/{name}")public String gatewayWeightRoutePredicate(@PathVariable("name") String name) {return "hello, [gatewayWeightRoutePredicate] the name is :" + name + ", the server port is " + serverPort;}

由于需要使用到权重,所有两个服务提供方payment8001和8002都加上上述方法。

【b】application.yml中加入如下配置:

server:port: 9527spring:application:name: springcloud-gatewaycloud:gateway:routes:#如下配置表示,当浏览器URL访问localhost:9527/gatewaySimple/**的时候,gateway网关会帮忙转发到http://localhost:8001/gatewaySimple/**去.- id: payment8001_gatewaySimple #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewaySimple/** # 断言,路径相匹配的进行路由#如下配置表示,当浏览器URL访问localhost:9527/gatewayLoadBalance/**的时候,gateway网关负载均衡地将请求分发到springcloud-payment-service服务中去.- id: payment_service_loadbalance #路由IDuri: lb://springcloud-payment-service #springcloud-payment-service为服务提供者注册到Eureka的应用名称(application name)predicates:- Path=/gatewayLoadBalance/** # 断言,路径相匹配的进行路由########################################【After Route Predicate】#######################################################如下配置表示,在配置的时间之后访问localhost:9527/gatewayAfterRoutePredicate/**的时候,,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayAfterRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayAfterRoutePredicate/**# - After=-08-19T20:41:32.999+08:00[Asia/Shanghai]- After=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Before Route Predicate】#######################################################如下配置表示,在配置的时间之前访问localhost:9527/gatewayBeforeRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBeforeRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBeforeRoutePredicate/**# - Before=-08-10T20:41:32.999+08:00[Asia/Shanghai]- Before=-08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Between Route Predicate】#######################################################如下配置表示,在配置的时间之间访问localhost:9527/gatewayBetweenRoutePredicate/**的时候,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayBetweenRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayBetweenRoutePredicate/**- Between=-08-10T20:41:32.999+08:00[Asia/Shanghai], -08-29T20:41:32.999+08:00[Asia/Shanghai]########################################【Cookie Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayCookieRoutePredicate/**的时候需要携带cookie参数,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayCookieRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayCookieRoutePredicate/**- Cookie=name, weishihuai########################################【Header Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHeaderRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHeaderRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHeaderRoutePredicate/**- Header=X-Request-Id, \d+########################################【Host Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayHostRoutePredicate/**的时候请求头需要包含X-Request-Id属性,并且值需要与正则表达式匹配,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayHostRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayHostRoutePredicate/**- Host=**.,**.########################################【Method Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayMethodRoutePredicate/**只有是get请求,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayMethodRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayMethodRoutePredicate/**- Method=GET# - Method=GET,POST# - Method=POST########################################【Query Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayQueryRoutePredicate/**请求中需要携带id参数,并且值要是整形类型,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayQueryRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayQueryRoutePredicate/**# - Query=green- Query=id, \d+########################################【RemoteAddr Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayRemoteAddrRoutePredicate/**请求的远程地址为127.0.0.1,gateway网关将请求分发到http://localhost:8001中去.- id: payment_service_gatewayRemoteAddrRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayRemoteAddrRoutePredicate/**# - RemoteAddr=127.0.0.1- RemoteAddr=192.168.35.105########################################【Weight Route Predicate】#######################################################如下配置表示,在访问localhost:9527/gatewayWeightRoutePredicate/**请求的远程地址为127.0.0.1,gateway网关将请求分发到http://localhost:8001中去.#路径将把80%的流量转发到http://localhost:8001,并将20%的流量转发到http://localhost:8002- id: payment_service8001_gatewayWeightRoutePredicate #路由IDuri: http://localhost:8001 #指定payment8001的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayWeightRoutePredicate/**- Weight=group1, 8- id: payment_service8002_gatewayWeightRoutePredicate #路由IDuri: http://localhost:8002 #指定payment8002的访问地址,即匹配后提供服务的路由地址predicates:- Path=/gatewayWeightRoutePredicate/**- Weight=group1, 2discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由eureka:instance:hostname: springcloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://springcloud-:7001/eureka/,http://springcloud-:7002/eureka/ #集群版Eureka注册中心

【c】测试

浏览器访问几次:http://localhost:9527/gatewayWeightRoutePredicate/weishihuai

发现访问到payment8001端口的几率比8002端口的大。

三、总结

本文详细介绍了Spring Cloud Gateway官网提到的11中Predicate断言,并且每种类型都有一个简单的示例说明,以上相关项目的代码我已经放在Gitee上,有需要的小伙伴可以去拉取进行学习:/weixiaohuai/springcloud_Hoxton,由于笔者水平有限,如有不对之处,还请小伙伴们指正,相互学习,一起进步。

如果觉得《【Hoxton.SR1版本】Spring Cloud Gateway之Predicate详解》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。