环境:springboot2.3.7 + spring cloud Hoxton.SR9
路由过滤器允许以某种方式修改传入的HTTP请求或输出HTTP响应。路由过滤器的作用域为特定路由。Spring Cloud Gateway包括许多内置的GatewayFilter工厂。
-
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
向目标服务
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" ;
}
}
启动两个服务,测试:
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}
测试:
-
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
目标服务:
@RestController
@RequestMapping("/api/")
public class UsersController {
@GetMapping("/query")
public Object query(String username) {
return "query " + username ;
}
}
测试:
-
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
测试:
-
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
这里为了演示用到了StripPrefix过滤器,如果不配置StripPrefix那么在做请求的时候转发到服务的地址将是:
http://xxxx/api-1/api-1/api/query明显这个地址在我们的服务上是不存在的。
StripPrefix这个过滤器的作用就是截取路径,截取几段路径。如这里的
http://xxxx/api-1/api-1/api/query 那会截取为http://xxxx/api/query再进行转发。
测试:
-
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
测试:
请求:
http://xxx/api-1/api/query 截取后:http://xxx/api/query 这里StripPrefix=1表示只截取几段路径。
-
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
说明:
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 ;
}
当请求参数username为dead时抛出异常。
测试:
成功:
失败:
-
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
请求将会被重定向到
http://localhost:20001/api/query
-
default 过滤器工厂
作用:默认过滤器,为所有的路由配置默认的过滤功能。
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
default-filters:
- PrefixPath=/api-1
- AddRequestHeader=access-token,123
以上配置将会为所有的路由增加前缀及请求header信息。
以上是用的比较多的一些内置Filter。
完毕!!!
关注+转发谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/79993.html