1.0.4.RELEASE
1. 配置Resilience4J熔断器
1.1. 开始
Resilience4J有两个实现,一个用户响应式服务,另一个用于非响应式服务。
- org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j 非响应式
- org.springframework.cloud:spring-cloud-starter-circuitbreaker-reactor-resilience4j 响应式
1.2. 自动配置
要禁止Resilience4J的自动配置,设置spring.cloud.circuitbreaker.resilience4j.enabled = false。
1.3. 默认配置
要提供默认配置,只需创建一个自定义bean,返回Resilience4JCircuitBreakerFactory
或ReactiveResilience4JCircuitBreakerFactory
即可。例如:
@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());
}
响应式:
@Bean
public Customizer<ReactiveResilience4JCircuitBreakerFactory> defaultCustomizer() {
return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
.timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build())
.circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
.build());
}
1.4. 自定义配置
和默认配置类似,你可以创建一个自定义bean,返回Resilience4JCircuitBreakerFactory
或ReactiveResilience4JCircuitBreakerFactory
即可:
@Bean
public Customizer<Resilience4JCircuitBreakerFactory> slowCustomizer() {
return factory -> factory.configure(builder -> builder.circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
.timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(2)).build()), "slow");
}
在熔断器创建之后、方法返回之前,你可以通过addCircuitBreakerCustomizer
添加一些事件处理方法:
@Bean
public Customizer<Resilience4JCircuitBreakerFactory> slowCustomizer() {
return factory -> factory.addCircuitBreakerCustomizer(circuitBreaker -> circuitBreaker.getEventPublisher()
.onError(normalFluxErrorConsumer).onSuccess(normalFluxSuccessConsumer), "normalflux");
}
响应式:
@Bean
public Customizer<ReactiveResilience4JCircuitBreakerFactory> slowCusomtizer() {
return factory -> {
factory.configure(builder -> builder
.timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(2)).build())
.circuitBreakerConfig(CircuitBreakerConfig.ofDefaults()), "slow", "slowflux");
factory.addCircuitBreakerCustomizer(circuitBreaker -> circuitBreaker.getEventPublisher()
.onError(normalFluxErrorConsumer).onSuccess(normalFluxSuccessConsumer), "normalflux");
};
}
1.5. 数据收集
只要添加了对应的依赖,熔断器会自动配置数据收集功能。对应的依赖有org.springframework.boot:spring-boot-starter-actuator
和io.github.resilience4j:resilience4j-micrometer
。更多数据信息请查看Resilience4j文档
2. 重试
Spring Retry给Spring应用提供声明式重试支持。该项目的一个子集也包括了实现熔断器的功能。 Spring Retry通过CircuitBreakerRetryPolicy
和stateful retry
提供一个熔断器的实现。所有Spring Retry的熔断器都会用到CircuitBreakerRetryPolicy
和DefaultRetryState
。这两个类可以通过SpringRetryConfigBuilder
配置。
2.1 默认配置
新建一个返回SpringRetryCircuitBreakerFactory的自定义bean,为所有的熔断器提供一个默认配置。下例中的configureDefault方法用来提供一个默认配置:
@Bean
public Customizer<SpringRetryCircuitBreakerFactory> defaultCustomizer() {
return factory -> factory.configureDefault(id -> new SpringRetryConfigBuilder(id)
.retryPolicy(new TimeoutRetryPolicy()).build());
}
2.2 详细配置
和默认配置类似,自定义一个返回SpringRetryCircuitBreakerFactory的bean:
@Bean
public Customizer<SpringRetryCircuitBreakerFactory> slowCustomizer() {
return factory -> factory.configure(builder -> builder.retryPolicy(new SimpleRetryPolicy(1)).build(), "slow");
}
在熔断器创建之后、方法返回之前,你可以通过addRetryTemplateCustomizers
添加一些事件处理方法:
@Bean
public Customizer<SpringRetryCircuitBreakerFactory> slowCustomizer() {
return factory -> factory.addRetryTemplateCustomizers(retryTemplate -> retryTemplate.registerListener(new RetryListener() {
@Override
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
return false;
}
@Override
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
}
@Override
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
}
}));
}