十四、SpringCloud学习笔记之Gateway-Predicate的使用
Predicate的使用
启动我们的gatewat9527
Route Predicate Factories:
Spring 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
1.After Route Predicate
after路由谓词工厂接受一个参数,一个datetime。此谓词匹配发生在指定日期时间之后的请求。
获取当前时区时间:
package com.liang.cloud.utils;
import java.time.ZonedDateTime;
public class Time {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
// ZonedDateTime zonedDateTime1 = ZonedDateTime.now(ZoneId.of("America/New_York")); 获取指定时区时间
}
}
yml:
- id: to_baidu_mil #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: https://news.baidu.com/mil
predicates:
- Path=/mil #断言,路径相匹配的进行路由
- After=2020-06-04T13:57:23.583+08:00[Asia/Shanghai]
可以正常访问,当修改时间为
2.Before Route Predicate
before route谓词工厂接受一个参数,即datetime。此谓词匹配发生在指定日期时间之前的请求。
同上,只有请求时间在指定日期之前可访问
3.Between Route Predicate Factory
同上:请求的时间需要在指定的两个时间段内
路由之间的谓词工厂接受两个参数,datetime1和datetime2。此谓词匹配发生在datetime1之后和datetime2之前的请求。datetime2参数必须位于datetime1之后。
4.Cookie Route Predicate Factory
cookie路由谓词工厂接受两个参数,cookie名称和一个正则表达式。此谓词匹配具有给定名称且其值与正则表达式匹配的cookie。
yml:
官网下载curl:
https://curl.haxx.se/download.html
解压:
添加环境变量:
%CURL_HOME%;
使用curl测试:
curl http://localhost:9527/mil
携带cookie访问
curl http://localhost:9527/mil --Cookie username=zhangl
成功返回百度页面
5.Header Route Predicate Factory
头路由谓词工厂接受两个参数,头名称和一个正则表达式。此谓词与具有给定名称的头匹配,该头的值与正则表达式匹配。
yml:
携带整数访问
6.Host Route Predicate
主机路由谓词工厂接受一个参数:主机名模式列表。该模式是一个ant样式的模式。作为分隔符。此谓词匹配与模式匹配的主机头。
yml:
curl -H Host:www.liang.com localhost:9527/mil
7.Method Route Predicate
方法路由谓词工厂接受一个或多个参数:要匹配的HTTP方法
如果请求方法是GET或POST,则此路由匹配。
yml
8.Path Route Predicate
路径路由谓词工厂接受两个参数:Spring PathMatcher模式列表和一个称为matchOptionalTrailingSeparator的可选标志。
yml
9.Query Route Predicate
-
查询路由谓词工厂接受两个参数:一个必需的参数和一个可选的regexp。
总结
yml
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: lb://cloud-payment-service
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由
- After=2020-06-04T13:57:23.583+08:00[Asia/Shanghai]
- id: payment_routh2
uri: lb://cloud-payment-service
predicates:
- Path=/payment/payment/lb/** #断言,路径相匹配的进行路由
- id: to_baidu_mil #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: https://news.baidu.com/mil
predicates:
- Path=/mil #断言,路径相匹配的进行路由
- After=2020-06-04T13:57:23.583+08:00[Asia/Shanghai]
- Method=GET
- Query=username, \d+ #要有参数名称并且是正整数才能路由
# - Cookie=username,zhangl #并且Cookie是username=zhangl
# - Header=X-Request-Id, \d+ #请求头中要有X-Request-Id属性并且值为整数的正则表达式
# - Host=**.liang.com
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
说白了,Predicate就是为了实现一组匹配规则,让请求过来找到对应的Route进行处理
推荐阅读
-
Golang学习笔记之延迟函数(defer)的使用小结
-
es6学习笔记之Async函数的使用示例
-
ng2学习笔记之bootstrap中的component使用教程
-
php学习笔记之mb_strstr的基本使用
-
Material组件之ShapeableImageView的使用(学习笔记)
-
Vue 2.0学习笔记之使用$refs访问Vue中的DOM
-
Kite的学习历程SpringCloud之Sentinel监控初步使用
-
ESP8266学习笔记之:使用AT命令操作ESP8266的低功耗模式
-
react学习笔记之state以及setState的使用
-
SpringCloud学习笔记之SpringCloud搭建父工程的过程图解