SpringCloud gateway内置过滤器之五

1、 SetRequestHeader GatewayFilter

SetRequestHeader GatewayFilter设置请求头,会覆盖原来已有的请求头。

spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- SetRequestHeader=X-Request-Red, Blue

用Postman调用http://localhost:8500/producer/hello:

从Producer模块的控制台看到X-Request-Red请求头的值为Blue。
 
SetRequestHeader也支持uri变量:

spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- SetRequestHeader=X-Request-Red, Blue-{segment}

重新访问,看到X-Request-Red请求头的值为Blue-hello。

2、SetResponseHeader GatewayFilter

SetResponseHeader GatewayFilter设置响应头,会替换原来的头。

spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- SetResponseHeader=X-Response-Red, Blue

修改Producer模块的Controller:

@RequestMapping("/hello")
public String hello(String name, HttpServletRequest request, HttpServletResponse response) {
response.setHeader("X-Response-Red", "123");
return "hello," + name + "," + port;
}

访问 http://localhost:8500/producer/hello ,从浏览器控制台看到响应头:

 

SetResponseHeader GatewayFilter也支持uri变量。

3、SetStatus GatewayFilter

SetStatus GatewayFilter设置响应状态码,它必须是有效的Spring HttpStatus。它可以是整数值404,也可以是枚举的字符串表示:NOT_FOUND。

spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- SetStatus=401

访问 http://localhost:8500/producer/hello ,看到:

4、StripPrefix GatewayFilter

StripPrefix GatewayFilter采用一个参数,即部件。parts参数指示在将请求发送到下游之前要从请求中剥离的路径中的部件数量。

spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1

将请求url从/producer/hello分割成/hello。

5、Retry GatewayFilter

  • retries:应尝试的重试次数。
  • status:应该重试的HTTP状态代码,使用org.springframework.HTTP.HttpStatus表示。
  • methods:应该重试的HTTP方法,使用org.springframework.HTTP.HttpMethod表示。
  • series:要重试的一系列状态代码,使用org.springframework.http.HttpStatus.series表示。
  • exceptions:应该重试的抛出异常的列表。
  • backoff:为重试配置的指数回退。在firstBackoff * (factor ^ n)的间隔时间之后执行重试,其中n是迭代。如果配置了maxBackoff,则应用的最大间隔时间限制为最大隔间时间。如果basedOnPreviousValue为true,则通过使用prevBackoff * factor来计算回退间隔时间。

 

Retry filter默认值:

  • retries: 3

  • series: 5XX 状态码

  • methods: GET 方法

  • exceptions: IOException and TimeoutException

  • backoff: 禁用

 

spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- name: Retry
args:
retries: 3
statuses: SERVICE_UNAVAILABLE
methods: GET,POST
exceptions: java.lang.RuntimeException
backoff:
firstBackoff: 10ms
maxBackoff: 50ms
factor: 2
basedOnPreviousValue: false

修改Producer模块的Controller:

int i = 0;
@RequestMapping("/hello")
public String hello(String name, HttpServletRequest request, HttpServletResponse response) {
System.out.println("123");
i++;
if (i > 3) {
throw new RuntimeException();
}
return "hello," + name + "," + port;
}

访问超过3次后就会抛出RuntimeException,然后Retry GatewayFilter就会重试,查看Producer控制台就会看到重试打印的日志。

posted @ 2023-05-06 20:11  shigp1  阅读(198)  评论(0)    收藏  举报