Spring Cloud Gateway
路由谓词工厂 Route Predicate Factory
1. The After Route Predicate Factory
spring:cloud:gateway:routes:- id: after_routeuri: https://example.orgpredicates:- After=2017-01-20T17:42:47.789-07:00[America/Denver]# 用日期时间匹配
2. The Before Route Predicate Factory
spring:cloud:gateway:routes:- id: before_routeuri: https://example.orgpredicates:- Before=2017-01-20T17:42:47.789-07:00[America/Denver]# 用日期时间匹配
3. The Between Route Predicate Factory
spring:cloud:gateway:routes:- id: between_routeuri: https://example.orgpredicates:- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]# 用日期时间匹配
4. The Cookie Route Predicate Factory
spring:cloud:gateway:routes:- id: cookie_routeuri: https://example.orgpredicates:- Cookie=chocolate, ch.p# 用 cookie 名称和值匹配
5. The Header Route Predicate Factory
spring:cloud:gateway:routes:- id: header_routeuri: https://example.orgpredicates:- Header=X-Request-Id, \d+# 用 header 名称和值匹配
6. The Host Route Predicate Factory
spring:cloud:gateway:routes:- id: host_routeuri: https://example.orgpredicates:- Host=**.somehost.org,**.anotherhost.org# 用 Host header 的值匹配
7. The Method Route Predicate Factory
spring:cloud:gateway:routes:- id: method_routeuri: https://example.orgpredicates:- Method=GET,POST# 用请求方法的类型匹配
8. The Path Route Predicate Factory
spring:cloud:gateway:routes:- id: path_routeuri: https://example.orgpredicates:- Path=/red/{segment},/blue/{segment}# 用请求路径的值匹配
9. The Query Route Predicate Factory
spring:cloud:gateway:routes:- id: query_routeuri: https://example.orgpredicates:- Query=green# 用查询参数的名称和值(值可以省略)匹配
10. The RemoteAddr Route Predicate Factory
spring:cloud:gateway:routes:- id: remoteaddr_routeuri: https://example.orgpredicates:- RemoteAddr=192.168.1.1/24# 用 RemoteAddr(CIDR格式,如:192.168.1.1/24) header 的值匹配
11. The Weight Route Predicate Factory
spring:cloud:gateway:routes:- id: weight_highuri: https://weighthigh.orgpredicates:- Weight=group1, 8- id: weight_lowuri: https://weightlow.orgpredicates:- Weight=group1, 2# 用分组名称和权重匹配
12. The XForwarded Remote Addr Route Predicate Factory
spring:cloud:gateway:routes:- id: xforwarded_remoteaddr_routeuri: https://example.orgpredicates:- XForwardedRemoteAddr=192.168.1.1/24# 用 X-Forwarded-For(CIDR格式,如:192.168.1.1/24) header 值匹配
13. 自定义路由谓词工厂:继承自 AbstractRoutePredicateFactory
https://blog.csdn.net/abu935009066/article/details/112260405
网关过滤器工厂 GatewayFilter Factories
包括这些过滤器:
添加请求头、添加请求参数、添加响应头、断路器、缓存请求体、去重响应头、回退头、JSON转gRPC、本地响应缓存、映射请求头、修改请求体、修改响应体、前缀路径、保留主机头、重定向、移除JSON属性响应体、移除请求头、移除请求参数、移除响应头、请求头大小、请求速率限制器、重写位置响应头、重写路径、重写响应头、保存会话、安全头、设置路径、设置请求头、设置响应头、设置状态、去除前缀、重试、请求大小、设置请求主机头、令牌中继等过滤器和默认过滤器。
https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories
路由谓词和过滤器在 yml 中的两种配置方式
Shortcut annotation,简洁型写法:
spring:cloud:gateway:routes:- id: cookie_routeuri: https://example.orgpredicates:- Cookie=chocolate, ch.p
Fully expanded arguments,复杂型写法(这种方式需要详细写出每一项):
spring:cloud:gateway:routes:- id: cookie_routeuri: https://example.orgpredicates:- name: Cookieargs: name: chocolateregexp: ch.p