Default Configuration
若要为所有断路器提供默认配置,请创建一个 Customizer
Bean,该 Bean 传递给 Resilience4JCircuitBreakerFactory
或 ReactiveResilience4JCircuitBreakerFactory
。configureDefault
方法可用于提供默认配置。
To provide a default configuration for all of your circuit breakers create a Customizer
bean that is passed a
Resilience4JCircuitBreakerFactory
or ReactiveResilience4JCircuitBreakerFactory
.
The configureDefault
method can be used to provide a default configuration.
@Bean
public Customizer<Resilience4JCircuitBreakerFactory> defaultCustomizer() {
return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
.timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build())
.circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
.build());
}
Reactive Example
@Bean
public Customizer<ReactiveResilience4JCircuitBreakerFactory> defaultCustomizer() {
return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
.circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
.timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build()).build());
}
Customizing The ExecutorService
如果您想配置执行断路器的 ExecutorService
,可以使用 Resilience4JCircuitBreakerFactory
来配置。
If you would like to configure the ExecutorService
which executes the circuit breaker you can do so using the Resilience4JCircuitBreakerFactory
.
例如,如果您想使用上下文化感知的 ExecutorService
,可以执行以下操作。
For example if you would like to use a context aware ExecutorService
you could do the following.
@Bean
public Customizer<ReactiveResilience4JCircuitBreakerFactory> defaultCustomizer() {
return factory -> {
ContextAwareScheduledThreadPoolExecutor executor = ContextAwareScheduledThreadPoolExecutor.newScheduledThreadPool().corePoolSize(5)
.build();
factory.configureExecutorService(executor);
};
}