路由

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 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类型的过滤在请求完成之后、将结果范围给客户端之前执行

大概可以分为以下几类:

HeaderParameterPathBodyStatusSessionRedirectRetryRateLimiterHystrix

常用的四种过滤器:

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}

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

好文链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: