gateway routes规则
1、After Route
crm开发定制crm开发定制所有匹配的请求必须在crm开发定制这个时间点之后
spring: cloud: gateway: routes: - id: after_route uri: https://example.org predicates: - After=2022-05-23T19:52:33.736+08:00[Asia/Shanghai] #crm开发定制时间格式为ZonedDateTime.now()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2、Before Route
所有匹配的请求必须在这个时间点之前
spring: cloud: gateway: routes: - id: before_route uri: https://example.org predicates: - Before=2021-05-23T19:52:33.736+08:00[Asia/Shanghai]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
3、Between Route
所有匹配的请求必须在这两个时间点之内
spring: cloud: gateway: routes: - id: between_route uri: https://example.org predicates: - Between=2022-05-23T19:52:33.736+08:00[Asia/Shanghai], 2023-05-23T19:52:33.736+08:00[Asia/Shanghai] #两个时间点之间不能用空格隔开
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
4、Cookie Route
接收Cookie参数,且值需要与正则表达是匹配
spring: cloud: gateway: routes: - id: cookie_route uri: https://example.org predicates: - Cookie=chocolate, ch.p
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
5、Header Route
在请求Header中,需要带有X-Request-Id的参数,且值需要和正则表达式匹配
spring: cloud: gateway: routes: - id: header_route uri: https://example.org predicates: - Header=X-Request-Id, \d+
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
6、Host Route
接收一个参数,主机名模式,判断请求的host是否满足匹配规则
spring: cloud: gateway: routes: - id: host_route uri: https://example.org predicates: - Host=**.somehost.org,**.anotherhost.org
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
7、Method Route
接收一个参数Method,表示请求的方式匹配参数
spring: cloud: gateway: routes: - id: method_route uri: https://example.org predicates: - Method=GET,POST
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
8、Path Route
接收一个路径集合,表示请求的路径需要匹配Path中的路径
spring: cloud: gateway: routes: - id: path_route uri: https://example.org predicates: - Path=/red/{segment},/blue/{segment}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
9、Query Route
接收两个参数,请求param和正则表达式,判断请求参数是否具有给定名称且值与正则表达式
spring: cloud: gateway: routes: - id: query_route uri: https://example.org predicates: - Query=green
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
10、RemoteAddr Route
路由谓词工厂采用的RemoteAddr
列表(最小大小为 1)sources
,它们是 CIDR 表示法(IPv4 或 IPv6)字符串,例如192.168.0.1/16
(其中192.168.0.1
是 IP 地址和16
子网掩码)。以下示例配置 RemoteAddr 路由谓词:
spring: cloud: gateway: routes: - id: remoteaddr_route uri: https://example.org predicates: - RemoteAddr=192.168.1.1/24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
11、 Weight Route
接收一组【组名,权重】,然后对于同一个组内的路由按照权重转发
spring: cloud: gateway: routes: - id: weight_high uri: https://weighthigh.org predicates: - Weight=group1, 8 - id: weight_low uri: https://weightlow.org predicates: - Weight=group1, 2
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
12、 XForwardedRemoteAddr Route
路由谓词工厂采用的XForwarded Remote Addr
列表(最小大小为 1)sources
,它们是 CIDR 表示法(IPv4 或 IPv6)字符串,例如192.168.0.1/16
(其中192.168.0.1
是 IP 地址和16
子网掩码)。
此路由谓词允许根据X-Forwarded-For
HTTP 标头过滤请求。
这可以与反向代理一起使用,例如负载平衡器或 Web 应用程序防火墙,其中仅当请求来自这些反向代理使用的受信任的 IP 地址列表时才允许请求。
spring: cloud: gateway: routes: - id: xforwarded_remoteaddr_route uri: https://example.org predicates: - XForwardedRemoteAddr=192.168.1.1/24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
11、自定义路由断言工厂
1、必须是Spring的Bean
2、类必须以RoutePredicateFactory结尾(约定大于配置)
3、必须继承AbstractRoutePredicateFactory
4、必须声明静态内部类 声明属性来接收 配置文件中对应的断言的信息
5、需要结合shortcutFieldOrder进行绑定
6、通过apply进行逻辑判断 true就是匹配成功 false匹配失败
package com.wwh.blog.config;import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;import org.springframework.stereotype.Component;import org.springframework.validation.annotation.Validated;import org.springframework.web.server.ServerWebExchange;import javax.validation.constraints.NotEmpty;import java.util.Collections;import java.util.List;import java.util.Objects;import java.util.function.Predicate;@Componentpublic class TokenRoutePredicateFactory extends AbstractRoutePredicateFactory<TokenRoutePredicateFactory.Config> { public TokenRoutePredicateFactory() { super(Config.class); } @Override public Predicate<ServerWebExchange> apply(Config config) { return new GatewayPredicate() { @Override public boolean test(ServerWebExchange exchange) { String token = config.getToken(); if (Objects.isNull(token) && !token.equals("auth")) { return false; } // there is a value and since regexp is empty, we only check existence. return true; } @Override public Object getConfig() { return config; } @Override public String toString() { return String.format("Token: %s ", config.token); } }; } @Override public List<String> shortcutFieldOrder() { return Collections.singletonList("token"); } @Validated public static class Config { @NotEmpty private String token; public String getToken() { return token; } public void setToken(String token) { this.token = token; } public Config(String token) { this.token = token; } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72