Spring Cloud Gateway路由及过滤规则
路由
Spring Cloud Gateway包含许多内置的路由断言Factories。这些断言都匹配HTTP请求的不同属性。
After 路由断言
After Route Predicate 在该日期时间之后发生的请求都将被匹配(UTC日期个数xxxx-xx-xxTxx:xx:xx+08:00[Asia/Shanghai])
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://localhost:8080
predicates:
- After=2022-03-30T18:00:00+08:00[Asia/Shanghai]
Before 路由断言
Before Route Predicate 在该日期时间之前发生的请求都将被匹配
spring:
cloud:
gateway:
routes:
- id: before_route
uri: http://localhost:8080
predicates:
- Before=2022-03-30T18:00:00+08:00[Asia/Shanghai]
Between 路由断言
Before Route Predicate 有两个参数,x和y。在x和y之间的请求将被匹配。y参数的实际时间必须在x之后。
spring:
cloud:
gateway:
routes:
- id: between_route
uri: http://localhost:8080
predicates:
- Between=2022-03-30T18:00:00+08:00[Asia/Shanghai], 2022-03-31T18:00:00+08:00[Asia/Shanghai]
Cookie路由断言
Cookie Route Predicate 有两个参数,cookie名称和正则表达式。请求包含次cookie名称且正则表达式匹配的将会被匹配。
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: http://localhost:8080
predicates:
- Cookie=token, \d
Header路由断言
Header Route Predicate 有两个参数,header名称和正则表达式。请求包含header名称且正则表达式匹配的将会被匹配。
spring:
cloud:
gateway:
routes:
- id: header_route
uri: http://localhost:8080
predicates:
- Header=X-Request-Id, \d+
Host 路由断言
Host Route Predicate 包括一个参数:host name列表。使用Ant path匹配规则。‘,’作为分隔符。
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://localhost:8080
predicates:
- Host=**.somehost.org,**.anotherhost.org
Ant path匹配规则:
符号 | 描述 |
---|---|
? | 匹配任何单字符 |
* | 匹配0或者任意数量的字符 |
** | 匹配0或者更多的目录 |
Method 路由断言
Method Route Predicate 包括一个参数: 需要匹配的HTTP请求方式。‘,’作为分隔符。
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://localhost:8080
predicates:
- Method=GET,POST
Path 路由断言
Path Route Predicate 有2个参数: 一个Spring PathMatcher表达式列表和可选matchOptionalTrailingSeparator标识 .
spring:
cloud:
gateway:
routes:
- id: path_route
uri: http://localhost:8080
predicates:
- Path=/foo/{
segment},/bar/{
segment}
URI 模板变量 (如上例中的 segment ) 将以Map的方式保存于ServerWebExchange.getAttributes()。key为ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE. 这些值将在GatewayFilter Factories使用
Map<String, String> map = ServerWebExchangeUtils.getPathPredicateVariables(exchange);
String segment = map .get("segment");
Query 路由断言
Query Route Predicate 有2个参数: 必选项 param 和可选项 regexp.
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://localhost:8080
predicates:
- Query=abc,ac. //请求包含abc参数,并且值匹配为ac.正则表达式 可以访问
RemoteAddr路由断言
RemoteAddr Route Predicate 的参数为IP地址
spring:
cloud:
gateway:
routes:
- id: remoteAddr_route
uri: http://localhost:8080
predicates:
- RemoteAddr=192.168.1.1
Weight路由断言
Weight Route Predicate 的参数有两个group和weight。按组分配权重
spring:
cloud:
gateway:
routes:
- id: weight_route1
uri: http://localhost:8080
predicates:
- Weight=group1,8 # 80%
- id: weight_route2
uri: http://localhost:8080
predicates:
- Weight=group2,2 # 20%
过滤
Gateway分为pre类型的过滤器和post类型的过滤器
- pre类型的过滤在转发到后端微服务之前执行,可以做鉴权、限流等操作
- post类型的过滤在请求完成之后、将结果范围给客户端之前执行
大概可以分为以下几类:
- Header
- Parameter
- Path
- Body
- Status
- Session
- Redirect
- Retry
- RateLimiter
- Hystrix
常用的四种过滤器:
AddRequestParameter Gateway Filter
添加请求参数,属于前置过滤。两个参数:key和value
spring:
cloud:
gateway:
routes:
- id: route1
uri: http://localhost:8080
predicates:
- Path=/product/**
filters:
- AddRequestParameter=info,hehe
RewritePath Gateway Filter
将请求路径重写如:/api-gateway/xxx/重写为/xxx/,属于前置过滤。
spring:
cloud:
gateway:
routes:
- id: route1
uri: http://localhost:8080
predicates:
- Path=/product/**,/api-gateway/**
filters:
- RewritePath=/api-gateway(?<segment>/?.*),$\{
segment}
SetStatus Gateway Filter
将响应的HTTP请求头设置为454,属于后置过滤
spring:
cloud:
gateway:
routes:
- id: route1
uri: http://localhost:8080
predicates:
- Path=/product/**,/api-gateway/**
filters:
- SetStatus=454
AddResponsHeader Gateway Filter
添加头信息,属于后置过滤
spring:
cloud:
gateway:
routes:
- id: route1
uri: http://localhost:8080
predicates:
- Path=/product/**,/api-gateway/**
filters:
- AddResponsHeader=X-Response-Author,test
作者:苍山不墨
来源链接:https://blog.csdn.net/qq_37032116/article/details/123855621
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。