首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的断路器总是在默认情况下是打开的,并且从属性上不能在minimumNumberOfCalls上工作

为什么我的断路器总是在默认情况下是打开的,并且从属性上不能在minimumNumberOfCalls上工作
EN

Stack Overflow用户
提问于 2022-10-13 13:02:20
回答 1查看 90关注 0票数 0

我是新的实现断路器的功能,在春季启动。我在build.gradle文件中添加了所有所需的依赖项,并在异常情况下将断路器注释添加到回退方法中,因此我知道断路器依赖项正在工作。问题是,断路器总是打开的,不能根据application.yml文件中设置的断路器属性工作。我将解释我通过代码片段添加了什么。

我的build.gradle文件具有依赖关系:

代码语言:javascript
复制
```implementation('io.github.resilience4j:resilience4j-spring-boot2:1.6.1')```
代码语言:javascript
复制
**Controller:**

```javascript

@GET

代码语言:javascript
复制
 @Path("product/{id}")
代码语言:javascript
复制
   public Response getProduct(@PathParam("id") Integer id) {
代码语言:javascript
复制
        logger.info("demo-service called for productId {}", id);
代码语言:javascript
复制
        return Response.ok(userService.getProduct(id)).build();
代码语言:javascript
复制
 }
代码语言:javascript
复制
**Service:**

```javascript

@断路器(名称= "demoServiceCircuitBreaker",fallbackMethod = "demoServiceFallbackMethod")

代码语言:javascript
复制
public ProductResponse getProduct(Integer id) {
代码语言:javascript
复制
    throw new IllegalStateException("Service is in error");
代码语言:javascript
复制
}

public ProductResponse demoServiceFallbackMethod(整数id,异常exc) {

代码语言:javascript
复制
        logger.error("Got an error, executing fallbackmethod and returning default from application");
代码语言:javascript
复制
        return defaultProduct();
代码语言:javascript
复制
 }

公众ProductResponse defaultProduct() {

代码语言:javascript
复制
    ProductResponse productResponse = new ProductResponse();
代码语言:javascript
复制
    productResponse.setId(999);
代码语言:javascript
复制
    productResponse.setName("Free coffee");
代码语言:javascript
复制
    productResponse.setPrice(0.0);
代码语言:javascript
复制
    return productResponse;
代码语言:javascript
复制
}
代码语言:javascript
复制
**application.yml**

```javascript

resilience4j:

代码语言:javascript
复制
circuitbreaker:
代码语言:javascript
复制
    configs:
代码语言:javascript
复制
      default:
代码语言:javascript
复制
        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. 
代码语言:javascript
复制
        slowCallRateThreshold: 50                # Configures a threshold in percentage. The CircuitBreaker considers a call as slow when the call duration is greater than slowCallDurationThreshold
代码语言:javascript
复制
        slowCallDurationThreshold: PT5S          # Configures the duration threshold above which calls are considered as slow and increase the rate of slow calls.
代码语言:javascript
复制
        permittedNumberOfCallsInHalfOpenState: 3 # Configures the number of permitted calls when the CircuitBreaker is half open.
代码语言:javascript
复制
        slidingWindowType: COUNT_BASED           # If the sliding window is COUNT_BASED, the last slidingWindowSize calls are recorded and aggregated.
代码语言:javascript
复制
        slidingWindowSize: 10                    # Configures the size of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed.
代码语言:javascript
复制
        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.
代码语言:javascript
复制
        waitDurationInOpenState: PT5S            # The time that the CircuitBreaker should wait before transitioning from open to half-open.
代码语言:javascript
复制
        #recordExceptions:
代码语言:javascript
复制
        #   - org.springframework.web.client.HttpServerErrorException
代码语言:javascript
复制
         #  - java.io.IOException
代码语言:javascript
复制
          # - java.util.concurrent.TimeoutException
代码语言:javascript
复制
           #- org.springframework.web.client.ResourceAccessException
代码语言:javascript
复制
instances:
代码语言:javascript
复制
     demoServiceCircuitBreaker:
代码语言:javascript
复制
        baseConfig: default
代码语言:javascript
复制

每当我到达端点时,它都会以json (从回退方法)返回ProductResponse。当需要启动监视或忽略故障阈值时,它看不到最小的调用数。我希望看到异常(服务出错),首先在前2次调用中,最后在达到阈值之后,它应该返回json中的ProductResponse。

请帮我找出我错过了什么。试着从application.yml中删除一些秘密,但问题依然存在。

EN

回答 1

Stack Overflow用户

发布于 2022-11-25 14:55:32

您的配置和属性是正确的。只有在记录了minimumNumberOfCalls后,断路器才会打开。

以上ProductResponse的原因是由于Resilience4J的回退机制

如果配置了回退方法,则将每个异常转发给回退方法执行器。回退方法执行器正在寻找能够处理异常的最佳匹配回退方法。

从今以后,缩小从ExceptionCallNotPermittedException的回退方法的论证范围。

代码语言:javascript
复制
public ProductResponse demoServiceFallbackMethod(Integer id, CallNotPermittedException exc) {
            logger.error("Got an error, executing fallbackmethod and returning default from application");
            return defaultProduct();
     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74056243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档