1、AddRequestHeader GatewayFilter
AddRequestHeader GatewayFilter采用名称和值参数。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/**
filters:
- StripPrefix=1
- AddRequestHeader=X-Request-red, blue
此列表将X-Request-red:blue标头添加到所有匹配请求的下游请求标头中。
Producer服务的controller:
@RequestMapping("/hello")
public String hello(String name, HttpServletRequest request) {
System.out.println(request.getHeader("x-request-red"));
return "hello," + name + "," + port;
}
访问http://localhost:8500/producer/hello,看到
AddRequestHeader也用于匹配路径或主机的URI变量:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- AddRequestHeader=X-Request-red, Blue-{segment}
访问http://localhost:8500/producer/hello,看到
2、AddRequestParameter GatewayFilter
AddRequestParameter GatewayFilter采用名称和值参数。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- AddRequestHeader=X-Request-red, Blue-{segment}
- AddRequestParameter=red, blue
这将为所有匹配请求的下游请求的查询字符串添加red=blue。
Producer服务controller:
@RequestMapping("/hello")
public String hello(String name, HttpServletRequest request) {
System.out.println(request.getHeader("x-request-red"));
System.out.println(request.getParameter("red"));
return "hello," + name + "," + port;
}
访问http://localhost:8500/producer/hello,看到
AddRequestParameter也用于匹配路径或主机的URI变量:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- AddRequestHeader=X-Request-red, Blue-{segment}
- AddRequestParameter=red, bar-{segment}
访问http://localhost:8500/producer/hello,看到
3、AddResponseHeader GatewayFilter
AddResponseHeader GatewayFilter采用名称和值参数。这会将响应头添加到所有匹配请求的下游响应标头中。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- AddResponseHeader=X-Response-Red, Blue
访问http://localhost:8500/producer/hello,从浏览器控制台中可看到响应头:
AddResponseHeader也用于匹配路径或主机的URI变量:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- AddResponseHeader=X-Response-Red, Blue-{segment}
访问http://localhost:8500/producer/hello,从浏览器控制台中可看到响应头:
4、DedupeResponseHeader GatewayFilter
DedupeResponseHeader GatewayFilter采用名称参数和可选策略参数。name可以包含一个以空格分隔的标头名称列表。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
将删除Access-Control-Allow-Credentials和Access-Control-Allow-Origin头的重复值。
5、Spring Cloud CircuitBreaker GatewayFilter
Spring Cloud CircuitBreaker GatewayFilter工厂使用Spring Cloud CircutBreaker API将网关路由封装在断路器中。Spring Cloud CircuitBreaker支持可与Spring Cloud Gateway一起使用的多个库。Spring Cloud支持开箱即用的Resilience4J。在GatewayServer模块加入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
</dependency>
并添加controller:
@RestController
public class Fallback {
private String dateStr(){
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
}
/**
* 返回字符串类型
* @return
*/
@GetMapping("/myfallback")
public String helloStr() {
return "myfallback, " + dateStr();
}
}
在application.yml配置:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- name: CircuitBreaker
args:
name: myCircuitBreaker
fallbackUri: forward:/myfallback
fallbackUri中的url要和上面controller中的url相同。当出现异常时会转发到fallbackUri。现在关闭Producer服务,访问http://localhost:8500/producer/hello,看到myfallback, 2023-05-04 08:27:53。
也可以根据响应状态码列表来触发熔断开关。可以配置响应状态码,也可以配置HttpStatus枚举。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- AddRequestHeader=X-Request-red, Blue-{segment}
- AddRequestParameter=red, bar-{segment}
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
- name: CircuitBreaker
args:
name: myCircuitBreaker
fallbackUri: forward:/myfallback
statusCodes:
- 500
修改Producer的controller:
@RequestMapping("/hello")
public String hello(String name, HttpServletRequest request, HttpServletResponse response) {
System.out.println(request.getHeader("x-request-red"));
System.out.println(request.getParameter("red"));
response.setStatus(500);
return "hello," + name + "," + port;
}
访问http://localhost:8500/producer/hello,看到myfallback, 2023-05-04 08:48:07。将response.setStatus(500);改成response.setStatus(404);重新访问http://localhost:8500/producer/hello,看到hello,null,8002。
修改gateway的application.yml配置
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
- AddRequestHeader=X-Request-red, Blue-{segment}
- AddRequestParameter=red, bar-{segment}
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
- name: CircuitBreaker
args:
name: myCircuitBreaker
fallbackUri: forward:/myfallback
statusCodes:
- 500
- "NOT_FOUND"
重新访问http://localhost:8500/producer/hello,看到myfallback, 2023-05-04 08:50:27。
标签:之一,producer,SpringCloud,hello,gatewayeFilter,Path,segment,id,路由 From: https://www.cnblogs.com/shigongp/p/17369737.html