FallbackHeaders GatewayFilter 工厂
FallbackHeaders 工厂允许您将 Spring Cloud CircuitBreaker 执行异常详细信息添加到转发到外部应用程序中 fallbackUri 的请求头中,如下所示:
application.yml
spring:
cloud:
gateway:
routes:
- id: ingredients
uri: lb://ingredients
predicates:
- Path=//ingredients/**
filters:
- name: CircuitBreaker
args:
name: fetchIngredients
fallbackUri: forward:/fallback
- id: ingredients-fallback
uri: https://:9994
predicates:
- Path=/fallback
filters:
- name: FallbackHeaders
args:
executionExceptionTypeHeaderName: Test-Header
在此示例中,当执行熔断器时发生执行异常后,请求将被转发到运行在 localhost:9994 的应用程序中的 fallback 端点或处理程序。FallbackHeaders 过滤器将包含异常类型、消息以及(如果可用)根原因异常类型和消息的请求头添加到该请求中。
您可以通过设置以下参数的值(显示默认值)来覆盖配置中请求头的名称:
-
executionExceptionTypeHeaderName("Execution-Exception-Type") -
executionExceptionMessageHeaderName("Execution-Exception-Message") -
rootCauseExceptionTypeHeaderName("Root-Cause-Exception-Type") -
rootCauseExceptionMessageHeaderName("Root-Cause-Exception-Message")
有关熔断器和网关的更多信息,请参阅 Spring Cloud CircuitBreaker Factory 部分。