详解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

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时抛出异常。

测试:

成功:

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

失败:

 

 

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。

 

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

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过滤器微服务

2023-02-15 08:12:19

http超时过滤器

2022-02-21 23:58:49

Spring过滤器顺序值

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

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

2024-11-04 08:45:48

布隆过滤器元数据指纹值

2022-02-16 23:58:41

Spring过滤器验证码

2009-07-08 15:30:56

Servlet过滤器

2009-07-14 09:09:08

Swing模型过滤器
点赞
收藏

51CTO技术栈公众号