我希望将断路器配置从application.yml文件移动到某个作为bean声明的config java文件中,因为这会使application.yml文件变得很大。我是否有可能从application.yml中删除配置并使用配置注释来定义断路器配置?我有一个以下代码的config java文件:
在我的服务文件中,我正在为断路器注释方法,例如:
@Configuration
@Component
public class CircuitBreakConfig {
@Bean
public CircuitBreaker defaultCircuitBreaker() {
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.slidingWindowSize(5)
.waitDurationInOpenState(Duration.ofMillis(6000))
.recordExceptions(IOException.class, TimeoutException.class,DataIntegrityViolationException.class)
.build();
// Create a CircuitBreakerRegistry with a custom global configuration
CircuitBreakerRegistry circuitBreakerRegistry =
CircuitBreakerRegistry.of(circuitBreakerConfig);
// Get or create a CircuitBreaker from the CircuitBreakerRegistry
// with the global default configuration
return circuitBreakerRegistry.circuitBreaker("default");
}
}
在我的服务文件中,我正在为断路器注释方法,例如:
@CircuitBreaker(name = "default", fallbackMethod = "fallback")
public Employee createEmployeeDefinition(Employee emp) throws Exception{
if(Objects.nonNull(sample)){
return EmployeeRepository.save(sample);
}
return null;
}
public Employee fallback(Employee emp,Exception ex) throws Exception{
System.out.println(ex.getClass()+" "+ex.getMessage() );
return null;
}
在当前实现中,我的熔断器无法达到开放状态。请给我一些建议。