我试图在springmvc项目中使用@CircuitBreaker注释,但它似乎不起作用。reslience4j注释是否适用于传统的非弹簧引导设置?
@Bean
public CircuitBreaker edocCircuitBreaker() {
LOGGER.info("Creating Circuit");
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
.slowCallDurationThreshold(Duration.ofMillis(1000))
.minimumNumberOfCalls(2)
.slidingWindowSize(2)
.failureRateThreshold(100)
.build();
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig);
CircuitBreaker serviceClientCircuitBreaker = circuitBreakerRegistry.circuitBreaker("ClientCircuitBreaker");
serviceClientCircuitBreaker.getEventPublisher().onStateTransition(this::serviceCircuitOpenHanlder);
return serviceClientCircuitBreaker;
}
@CircuitBreaker(name="ClientCircuitBreaker")
public String sendAndReceive(String request, RequestParameters parameters) throws RuntimeException {
StreamSource source = new StreamSource(new StringReader(request));
StreamResult result = new StreamResult(new StringWriter());
.....
您应该使用resilience4j-spring
。它为您提供了许多@Configuration
类。
您需要导入配置类:
@Import({ CircuitBreakerConfiguration.class, RetryConfiguration.class, TimeLimiterConfiguration.class, BulkheadConfiguration.class }
CircuitBreakerConfiguration
很重要,因为它配置了CircuitBreakerAspect
bean,这是注释工作所必需的。
您必须创建一个@Bean
,它使用您的外部配置属性文件并填充并返回CircuitBreakerConfigurationProperties
。
在弹性4j-spring-bot2中,我们使用@EnableConfigurationProperties(CircuitBreakerProperties.class)
自动配置它