【问题标题】:Hystrix Circuit breaker not opening the circuitHystrix 断路器不打开电路
【发布时间】:2020-10-29 08:26:08
【问题描述】:

我正在我的 Spring 启动应用程序中使用 Hystrix 实现断路器,我的代码如下所示:

@service
public class MyServiceHandler {

    @HystrixCommand(fallbackMethod="fallback")
    
    public String callService() {
       // if(remote service is not reachable
       // throw ServiceException
    }
    
    public String fallback() {
       // return default response
    }
}

// In application.properties, I have below properties defined:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
hystrix.command.default.circuitBreaker.requestVolumeThreshold=3
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=30000
hystrix.threadpool.default.coreSize=4
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=200000

我看到每次callService() 失败都会调用fallback()。但是,在 3 次故障后电路没有打开。 3次失败后,我期待它会直接调用fallback()并跳过callService()。但这并没有发生。有人可以建议我在这里做错了什么吗?

谢谢, B贾根

于 7 月 26 日编辑,在下面添加更多详细信息:

以下是实际代码。我玩得更远了。当我直接在 RegistrationHystrix.registerSeller() 方法中调用远程服务时,我看到电路在重复失败时按预期打开。但是,当我将远程服务调用包装在 Spring 重试模板中时,它会继续进入回退方法,但电路永远不会打开。

@Service
public class RegistrationHystrix {
    Logger logger = LoggerFactory.getLogger(RegistrationHystrix.class);
    private RestTemplate restTemplate;
    private RetryTemplate retryTemplate;

    public RegistrationHystrix(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
        retryTemplate = new RetryTemplate();
        FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
        fixedBackOffPolicy.setBackOffPeriod(1000l);
        retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(3);
        retryTemplate.setRetryPolicy(retryPolicy);
    }

    @HystrixCommand(fallbackMethod = "fallbackForRegisterSeller", commandKey = "ordermanagement")
    public String registerSeller(SellerDto sellerDto)  throws Exception {
        String response = retryTemplate.execute(new RetryCallback<String, Exception>() {
                   @Override
                   public String doWithRetry(RetryContext context) {
                           logger.info(String.format("Retry count %d", context.getRetryCount()));
                          return restTemplate.postForObject("/addSeller", sellerDto, String.class);
                   }
       });
       return response;
    }

    public List<SellerDto> getSellersList() {
        return restTemplate.getForObject("/sellersList", List.class);
    }

    public String fallbackForRegisterSeller(SellerDto sellerDto, Throwable t) {
        logger.error("Inside fall back, cause - {}", t.toString());
        return "Inside fallback method. Some error occured while calling service for seller registration";
    }
}

下面是服务类,它依次调用上面的 Hystrix 包装服务。此类依次由控制器调用。

@Service
public class RegistrationServiceImpl implements RegistrationService {

    Logger logger = LoggerFactory.getLogger(RegistrationServiceImpl.class);
    private RegistrationHystrix registrationHystrix;


    public RegistrationServiceImpl(RegistrationHystrix registrationHystrix) {
        this.registrationHystrix = registrationHystrix;
    }

    @Override
    public String registerSeller(SellerDto sellerDto)  throws Exception {
        long start = System.currentTimeMillis();
        String registerSeller = registrationHystrix.registerSeller(sellerDto);
        logger.info("add seller call returned in - {}", System.currentTimeMillis() - start);
        return registerSeller;

    }

所以,我想了解为什么断路器在与 Spring RetryTemplate 一起使用时不能按预期工作。

【问题讨论】:

    标签: hystrix circuit-breaker


    【解决方案1】:

    您应该在测试时使用metrics.healthSnapshot.intervalInMilliseconds。我猜你是在默认的 500 毫秒内执行所有 3 个请求,因此电路没有打开。您可以缩短此间隔,也可以在 3 个请求之间添加一个sleep

    【讨论】:

    • 感谢 FlapPy 的回复。我的请求需要几秒钟,而不是在 500 毫秒内完成。我在上面添加了更多细节。如前所述,仅当我在 HystrixCommand 中使用 Spring Retry 模板时才会出现电路未打开的问题。如果我删除 Spring Retry,它会按预期工作。我的理解是 HystrixCommand 不关心我们在带注释的方法中放了什么代码。
    猜你喜欢
    • 2015-05-23
    • 2017-05-29
    • 2018-10-05
    • 2019-06-27
    • 2018-07-09
    • 2016-12-11
    • 2016-08-06
    • 2015-12-06
    • 2020-04-13
    相关资源
    最近更新 更多