FallbackHeaders
篩檢程式
FallbackHeaders
工廠可讓您在轉發到外部應用程式的 fallbackUri
的請求標頭中新增 Spring Cloud CircuitBreaker 執行例外詳細資訊,如下列情境所示
application.yml
spring:
cloud:
gateway:
mvc:
routes:
- id: ingredients
uri: lb://ingredients
predicates:
- Path=//ingredients/**
filters:
- name: CircuitBreaker
args:
name: fetchIngredients
fallbackUri: forward:/fallback
- id: ingredients-fallback
uri: http://localhost:9994
predicates:
- Path=/fallback
filters:
- name: FallbackHeaders
args:
executionExceptionTypeHeaderName: Test-Header
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.CircuitBreakerFilterFunctions.circuitBreaker;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.fallbackHeaders;
import static org.springframework.cloud.gateway.server.mvc.filter.LoadBalancerFilterFunctions.lb;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
@Configuration
class RouteConfiguration {
@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsCircuitBreakerFallbackToGatewayRoute() {
return route("ingredients")
.route(path("/ingredients/**"), http())
.filter(lb("ingredients"))
.filter(circuitBreaker("fetchIngredients", URI.create("forward:/fallback")))
.build()
.and(route("ingredients-fallback")
.route(path("/fallback"), http("http://localhost:9994"))
.before(fallbackHeaders())
.build());
}
}
在此範例中,在執行斷路器時發生執行例外之後,請求會轉發到在 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 篩檢程式章節。