环境:springboot2.3.7 + spring cloud Hoxton.SR9
路由过滤器允许以某种方式修改传入的HTTP请求或输出HTTP响应。路由过滤器的作用域为特定路由。Spring Cloud Gateway包括许多内置的GatewayFilter工厂。
1.AddRequestHeader 过滤器工厂
作用:在请求中添加header信息(向目标服务)。对应过滤器工厂AddRequestHeaderGatewayFilterFactory
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: AddRequestHeader_filter
uri: http://localhost:20001
predicates:
- Path=/api/{user}
filters:
- AddRequestHeader=access-token,123456789
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
向目标服务http://localhost:20001/api/xxx添加请求header access-token信息。
20001服务中有对应的接口:
@RestController
@RequestMapping("/api/")
public class UsersController {
@Resource
private HttpServletRequest request ;
@GetMapping("/{user}")
public Object save(@PathVariable("user") String username) {
System.out.println(username) ;
System.out.println("access-token = " + request.getHeader("access-token")) ;
return "success" ;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
启动两个服务,测试:

20001服务控制台输出:

动态header信息配置:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: AddRequestHeader_filter
uri: http://localhost:20001
predicates:
- Path=/api/{token}
filters:
- AddRequestHeader=access-token,{token}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
测试:

2.AddRequestParameter 过滤器工厂
作用:给下游服务添加查询参数。对应过滤器工厂AddRequestParameterGatewayFilterFactory
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: add_request_parameter_route
uri: http://localhost:20001
predicates:
- Path=/api/query
filters:
- AddRequestParameter=username, admin
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
目标服务:
@RestController
@RequestMapping("/api/")
public class UsersController {
@GetMapping("/query")
public Object query(String username) {
return "query " + username ;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
测试:

3.AddResponseHeader 过滤器工厂
作用:在响应header中添加头信息。对应过滤器工厂AddResponseHeaderGatewayFilterFactory
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: add_response_header_route
uri: http://localhost:20001
predicates:
- Path=/api/query
filters:
- AddResponseHeader=server-id, nginx-001
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
测试:

4.PrefixPath 过滤器工厂
作用:为原始的请求路径添加一个前缀路径。对应过滤器工厂PrefixPathGatewayFilterFactory
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: prefixpath_route
uri: http://localhost:20001
predicates:
- Path=/api-1/**
filters:
- PrefixPath=/api-1
- StripPrefix=2
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
这里为了演示用到了StripPrefix过滤器,如果不配置StripPrefix那么在做请求的时候转发到服务的地址将是:http://xxxx/api-1/api-1/api/query明显这个地址在我们的服务上是不存在的。
StripPrefix这个过滤器的作用就是截取路径,截取几段路径。如这里的http://xxxx/api-1/api-1/api/query 那会截取为http://xxxx/api/query再进行转发。
测试:

5.StripPrefix 过滤器工厂
作用:截取指定段的请求路径后进行路由转发。对应过滤器工厂StripPrefixGatewayFilterFactory
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: prefixpath_route
uri: http://localhost:20001
predicates:
- Path=/api-1/**
filters:
- StripPrefix=1
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
测试:
请求:http://xxx/api-1/api/query 截取后:http://xxx/api/query 这里StripPrefix=1表示只截取几段路径。

6.Retry 过滤器工厂
作用:针对不同的响应结果进行重试。对应过滤器工厂RetryGatewayFilterFactory
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: retry_test
uri: http://localhost:20001
predicates:
- Path=/api-1/**
filters:
- StripPrefix=1
- name: Retry
args:
retries: 3
statuses: INTERNAL_SERVER_ERROR
methods: GET,POST
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
说明:
retries:重试次数
statuses:需要重试的状态码,取值在 org.springframework.http.HttpStatus 中
methods:需要重试的请求方法,取值在 org.springframework.http.HttpMethod 中
series:HTTP状态码序列,取值在 org.springframework.http.HttpStatus.Series 中
exceptions:异常列表,对于抛出的哪些异常将会进行重试。
接口服务:
@GetMapping("/query")
public Object query(String username) {
if ("dead".equals(username)) {
throw new RuntimeException("错误的用户名") ;
}
return "query " + username ;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
当请求参数username为dead时抛出异常。
测试:
成功:

失败:

7.RedirectTo 过滤器工厂
作用:将原始请求重定向到指定的Url。对应过滤器工厂RedirectToGatewayFilterFactory
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: prefixpath_route
uri: http://localhost:20001
predicates:
- Path=/api-1/**
filters:
- RedirectTo=302, http://localhost:20001/api/query
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
请求将会被重定向到 http://localhost:20001/api/query
8.default 过滤器工厂
作用:默认过滤器,为所有的路由配置默认的过滤功能。
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
default-filters:
- PrefixPath=/api-1
- AddRequestHeader=access-token,123
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
以上配置将会为所有的路由增加前缀及请求header信息。
以上是用的比较多的一些内置Filter。