详解Spring Cloud Gateway应用2内置过滤器

系统
路由过滤器允许以某种方式修改传入的HTTP请求或输出HTTP响应。路由过滤器的作用域为特定路由。Spring Cloud Gateway包括许多内置的GatewayFilter工厂。

[[375777]]

 环境:springboot2.3.7 + spring cloud Hoxton.SR9

路由过滤器允许以某种方式修改传入的HTTP请求或输出HTTP响应。路由过滤器的作用域为特定路由。Spring Cloud Gateway包括许多内置的GatewayFilter工厂。

1.AddRequestHeader 过滤器工厂

作用:在请求中添加header信息(向目标服务)。对应过滤器工厂AddRequestHeaderGatewayFilterFactory

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: AddRequestHeader_filter 
  11.         uri: http://localhost:20001 
  12.         predicates: 
  13.         - Path=/api/{user
  14.         filters: 
  15.         - AddRequestHeader=access-token,123456789 

 向目标服务http://localhost:20001/api/xxx添加请求header access-token信息。

20001服务中有对应的接口:

  1. @RestController 
  2. @RequestMapping("/api/"
  3. public class UsersController { 
  4.      
  5.     @Resource 
  6.     private HttpServletRequest request ; 
  7.      
  8.     @GetMapping("/{user}"
  9.     public Object save(@PathVariable("user") String username) { 
  10.         System.out.println(username) ; 
  11.         System.out.println("access-token = " + request.getHeader("access-token")) ; 
  12.         return "success" ; 
  13.     } 
  14.      

 启动两个服务,测试:


20001服务控制台输出:


动态header信息配置:

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

 测试:


2.AddRequestParameter 过滤器工厂

作用:给下游服务添加查询参数。对应过滤器工厂AddRequestParameterGatewayFilterFactory

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: add_request_parameter_route 
  11.         uri: http://localhost:20001 
  12.         predicates: 
  13.         - Path=/api/query 
  14.         filters: 
  15.         - AddRequestParameter=username, admin 

 目标服务:

  1. @RestController 
  2. @RequestMapping("/api/"
  3. public class UsersController { 
  4.      
  5.     @GetMapping("/query"
  6.     public Object query(String username) { 
  7.         return "query " + username ; 
  8.     } 

 测试:

 

 

3.AddResponseHeader 过滤器工厂

作用:在响应header中添加头信息。对应过滤器工厂AddResponseHeaderGatewayFilterFactory

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: add_response_header_route 
  11.         uri: http://localhost:20001 
  12.         predicates: 
  13.         - Path=/api/query 
  14.         filters: 
  15.         - AddResponseHeader=server-id, nginx-001 

 测试:


4.PrefixPath 过滤器工厂

作用:为原始的请求路径添加一个前缀路径。对应过滤器工厂PrefixPathGatewayFilterFactory

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: prefixpath_route 
  11.         uri: http://localhost:20001 
  12.         predicates: 
  13.         - Path=/api-1/** 
  14.         filters: 
  15.         - PrefixPath=/api-1 
  16.         - StripPrefix=2 

 这里为了演示用到了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

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: prefixpath_route 
  11.         uri: http://localhost:20001 
  12.         predicates: 
  13.         - Path=/api-1/** 
  14.         filters: 
  15.         - StripPrefix=1 

 测试:

请求:http://xxx/api-1/api/query 截取后:http://xxx/api/query 这里StripPrefix=1表示只截取几段路径。


6.Retry 过滤器工厂

作用:针对不同的响应结果进行重试。对应过滤器工厂RetryGatewayFilterFactory

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: retry_test 
  11.         uri: http://localhost:20001 
  12.         predicates: 
  13.         - Path=/api-1/** 
  14.         filters: 
  15.         - StripPrefix=1 
  16.         - name: Retry 
  17.           args: 
  18.             retries: 3 
  19.             statuses: INTERNAL_SERVER_ERROR 
  20.             methods: GET,POST 

 说明:

retries:重试次数

statuses:需要重试的状态码,取值在 org.springframework.http.HttpStatus 中

methods:需要重试的请求方法,取值在 org.springframework.http.HttpMethod 中

series:HTTP状态码序列,取值在 org.springframework.http.HttpStatus.Series 中

exceptions:异常列表,对于抛出的哪些异常将会进行重试。

接口服务:

  1. @GetMapping("/query"
  2.     public Object query(String username) { 
  3.         if ("dead".equals(username)) { 
  4.             throw new RuntimeException("错误的用户名") ; 
  5.         } 
  6.         return "query " + username ; 
  7.     } 

 当请求参数username为dead时抛出异常。

测试:

成功:

Spring Cloud Gateway应用详解2内置过滤器

失败:

 

 

7.RedirectTo 过滤器工厂

作用:将原始请求重定向到指定的Url。对应过滤器工厂RedirectToGatewayFilterFactory

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       routes: 
  10.       - id: prefixpath_route 
  11.         uri: http://localhost:20001 
  12.         predicates: 
  13.         - Path=/api-1/** 
  14.         filters: 
  15.         - RedirectTo=302, http://localhost:20001/api/query 

 请求将会被重定向到 http://localhost:20001/api/query

8.default 过滤器工厂

作用:默认过滤器,为所有的路由配置默认的过滤功能。

  1. spring: 
  2.   cloud: 
  3.     gateway: 
  4.       enabled: true 
  5.       discovery: 
  6.         locator: 
  7.           enabled: true 
  8.           lowerCaseServiceId: true 
  9.       default-filters: 
  10.       - PrefixPath=/api-1 
  11.       - AddRequestHeader=access-token,123 

 以上配置将会为所有的路由增加前缀及请求header信息。

以上是用的比较多的一些内置Filter。

 

责任编辑:姜华 来源: 今日头条
相关推荐

2017-04-12 14:43:01

Spring ClouZuul过滤器

2023-01-26 01:41:27

核心全局过滤器

2023-04-14 09:01:25

2023-07-24 08:00:56

客户端访问指定

2024-04-03 08:08:15

谓词网关开发

2017-05-04 22:30:17

Zuul过滤器微服务

2022-02-21 23:58:49

Spring过滤器顺序值

2023-02-15 08:12:19

http超时过滤器

2009-07-08 16:07:04

Servlet过滤器配

2011-06-29 16:14:59

Qt 事件 过滤器

2021-01-14 07:54:19

Spring Clou应用路由

2009-06-18 10:13:00

Hibernate过滤

2016-12-07 09:56:13

JavaFilter过滤器

2017-09-15 23:29:53

Spring Clou微服务架构过滤器

2021-07-05 15:22:03

Servlet过滤器客户端

2024-01-05 09:04:35

隆过滤器数据结构哈希函数

2022-02-16 23:58:41

Spring过滤器验证码

2009-07-08 15:30:56

Servlet过滤器

2009-09-29 13:55:23

Hibernate设置

2009-07-14 09:09:08

Swing模型过滤器
点赞
收藏

51CTO技术栈公众号