我是新的实现断路器的功能,在春季启动。我在build.gradle文件中添加了所有所需的依赖项,并在异常情况下将断路器注释添加到回退方法中,因此我知道断路器依赖项正在工作。问题是,断路器总是打开的,不能根据application.yml文件中设置的断路器属性工作。我将解释我通过代码片段添加了什么。
我的build.gradle文件具有依赖关系:
```implementation('io.github.resilience4j:resilience4j-spring-boot2:1.6.1')```**Controller:**
```javascript@GET
@Path("product/{id}") public Response getProduct(@PathParam("id") Integer id) { logger.info("demo-service called for productId {}", id); return Response.ok(userService.getProduct(id)).build(); }**Service:**
```javascript@断路器(名称= "demoServiceCircuitBreaker",fallbackMethod = "demoServiceFallbackMethod")
public ProductResponse getProduct(Integer id) { throw new IllegalStateException("Service is in error");}public ProductResponse demoServiceFallbackMethod(整数id,异常exc) {
logger.error("Got an error, executing fallbackmethod and returning default from application"); return defaultProduct(); }公众ProductResponse defaultProduct() {
ProductResponse productResponse = new ProductResponse(); productResponse.setId(999); productResponse.setName("Free coffee"); productResponse.setPrice(0.0); return productResponse;}**application.yml**
```javascriptresilience4j:
circuitbreaker: configs: default: failureRateThreshold: 50 # Configures the failure rate threshold in percentage. When the failure rate is equal or greater than the threshold the CircuitBreaker transitions to open and starts short-circuiting calls. slowCallRateThreshold: 50 # Configures a threshold in percentage. The CircuitBreaker considers a call as slow when the call duration is greater than slowCallDurationThreshold slowCallDurationThreshold: PT5S # Configures the duration threshold above which calls are considered as slow and increase the rate of slow calls. permittedNumberOfCallsInHalfOpenState: 3 # Configures the number of permitted calls when the CircuitBreaker is half open. slidingWindowType: COUNT_BASED # If the sliding window is COUNT_BASED, the last slidingWindowSize calls are recorded and aggregated. slidingWindowSize: 10 # Configures the size of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed. minimumNumberOfCalls: 2 # Configures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate or slow call rate. waitDurationInOpenState: PT5S # The time that the CircuitBreaker should wait before transitioning from open to half-open. #recordExceptions: # - org.springframework.web.client.HttpServerErrorException # - java.io.IOException # - java.util.concurrent.TimeoutException #- org.springframework.web.client.ResourceAccessExceptioninstances: demoServiceCircuitBreaker: baseConfig: default每当我到达端点时,它都会以json (从回退方法)返回ProductResponse。当需要启动监视或忽略故障阈值时,它看不到最小的调用数。我希望看到异常(服务出错),首先在前2次调用中,最后在达到阈值之后,它应该返回json中的ProductResponse。
请帮我找出我错过了什么。试着从application.yml中删除一些秘密,但问题依然存在。
发布于 2022-11-25 14:55:32
您的配置和属性是正确的。只有在记录了minimumNumberOfCalls后,断路器才会打开。
以上ProductResponse的原因是由于Resilience4J的回退机制。
如果配置了回退方法,则将每个异常转发给回退方法执行器。回退方法执行器正在寻找能够处理异常的最佳匹配回退方法。
从今以后,缩小从Exception到CallNotPermittedException的回退方法的论证范围。
public ProductResponse demoServiceFallbackMethod(Integer id, CallNotPermittedException exc) {
logger.error("Got an error, executing fallbackmethod and returning default from application");
return defaultProduct();
}https://stackoverflow.com/questions/74056243
复制相似问题