我想将我的断路器配置从 application.yml 文件移动到一些配置 java 文件作为 bean 声明,因为它使 application.yml 文件很大,我是否可以从 applciation.yml 中删除配置并使用配置注释定义断路器配置。我有如下代码的配置 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;
}
使用当前的实现,我无法在我的断路器中达到打开状态。请给我一些建议