多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
其实在启动网关服务时,IDEA控制台就会打印常用的断言。 ``` ... : Loaded RoutePredicateFactory [After] ... : Loaded RoutePredicateFactory [Before] ... : Loaded RoutePredicateFactory [Between] ... : Loaded RoutePredicateFactory [Cookie] ... : Loaded RoutePredicateFactory [Header] ... : Loaded RoutePredicateFactory [Host] ... : Loaded RoutePredicateFactory [Method] ... : Loaded RoutePredicateFactory [Path] ... : Loaded RoutePredicateFactory [Query] ... : Loaded RoutePredicateFactory [ReadBodyPredicateFactory] ... : Loaded RoutePredicateFactory [RemoteAddr] ... : Loaded RoutePredicateFactory [Weight] ... : Loaded RoutePredicateFactory [CloudFoundryRouteService] ``` 下面介绍几个断言的使用。 <br/> **1. After** *`application.yml`* ```yml spring: cloud: gateway: routes: - id: payment_routh uri: lb://cloud-payment-service predicates: - Path=/payment/get/** #当过了 After 指定的时间后才能通过路由访问 /payment/get/** - After=2021-11-18T16:39:38.482+08:00[Asia/Shanghai] ``` 上面一大串的字符串可以如下获取: ```java public static final String getZoneDateTime() { //默认时区 ZonedDateTime zbj = ZonedDateTime.now(); return zbj.toString(); } ``` <br/> **2. Between** *`application.yml`* ```yml spring: cloud: gateway: routes: - id: payment_routh uri: lb://cloud-payment-service predicates: - Path=/payment/get/** #当时间在两者之间时才能通过路由访问 /payment/get/** - Between=2021-11-18T16:42:23.093+08:00[Asia/Shanghai],2021-11-18T20:42:23.093+08:00[Asia/Shanghai] ``` <br/> **3. Cookie** *`application.yml`* ```yml spring: cloud: gateway: routes: - id: payment_routh uri: lb://cloud-payment-service predicates: - Path=/payment/get/** #当携带Cookie时才能通过路由访问 /payment/get/** - Cookie=chocolate, ch.p ``` 测试: ```shell -- GET 请求,不携带 Cookie >curl http://localhost:9527/payment/get/nameone/zhangsan 不携带Cookie不能访问,这里报错 -- GET 请求,携带 Cookie >curl http://localhost:9527/payment/get/nameone/zhangsan --cookie "chocolate=chip" serverPort:8002,name=zhangsan ``` <br/> **4. Header** *`application.yml`* ```yml spring: cloud: gateway: routes: - id: payment_routh uri: lb://cloud-payment-service predicates: - Path=/payment/get/** #当携带指定的请求头时才能通过路由访问 /payment/get/** - Header=X-Request-Id, \d+ ``` 测试: ```shell --GET 请求,携带请求头 >curl http://localhost:9527/payment/get/nameone/zhangsan -H "X-Request-Id:123" serverPort:8001,name=zhangsan ``` <br/> **5. Host** *`application.yml`* ```yml spring: cloud: gateway: routes: - id: payment_routh uri: lb://cloud-payment-service predicates: - Path=/payment/get/** #只要携带下面两个主机之一便可以通过路由访问 /payment/get/** - Host=**.somehost.org,**.otherhost.org ``` 测试: ```shell >curl http://localhost:9527/payment/get/nameone/zhangsan -H "Host: www.somehost.org" serverPort:8001,name=zhangsan ``` <br/> **6. Query** *`application.yml`* ```yml spring: cloud: gateway: routes: - id: payment_routh uri: lb://cloud-payment-service predicates: - Path=/payment/get/** #需要携带参数 username,并且必须是正整数,才可以通过路由访问 /payment/get/** - Query=username,\d+ ``` 测试: ```shell curl http://localhost:9527/payment/get/must/?username=33 serverPort:8002,username=33 ``` <br/> **7. Method** *`application.yml`* ```yml spring: cloud: gateway: routes: - id: payment_routh uri: lb://cloud-payment-service predicates: - Path=/payment/get/** #并且是 GET 请求,才可以通过路由访问 /payment/get/** - Method=GET ``` 测试: ```shell >curl http://localhost:9527/payment/get/nameone/zhangsan serverPort:8001,name=zhangsan ```