【问题标题】:What is the use of @HystrixCommand annotation without a fallback method?没有回退方法的 @HystrixCommand 注释有什么用?
【发布时间】:2018-09-23 06:23:30
【问题描述】:

我的项目中有以下代码

@HystrixCommand(groupKey = "AddressRepository", commandKey = 
"getAddressById", threadPoolKey = "addressSearchPool")
public List<Address> getAddressById(String id)
  throws MyCustomException, HystrixRuntimeException {

     try (Connection con = sql2o.open()) {
         return // some logic....

     } catch (Sql2oException e) {
        throw new MyCustomException();
     } 
}

如您所见,@HystrixCommand 没有定义任何 fallbackMethod。我想知道 HystrixCommand 在这里服务的目的是什么。

没有定义回退时使用 HystrixCommand 的用例是什么?

代码是 Spring 应用程序的一部分。

【问题讨论】:

    标签: spring hystrix


    【解决方案1】:

    我相信在失败的情况下返回默认响应不仅仅是 hystrix 的功能之一。

    停止级联故障。回退和优雅降级。快速失败 和快速恢复。

    1) 如果你不覆盖getFallback 方法默认实现将被使用:

      protected R getFallback() {
            throw new UnsupportedOperationException("No fallback available.");
        }
    

    尽管没有返回优雅的响应(仅抛出异常)circuit breaker pattern 和快速失败的原则仍然有效。

    2) 在回退方法中,您通常会返回空对象或集合。在某些情况下,您可能不想在失败的情况下返回空对象,但您想表明您的服务不起作用并返回一些 5xx http 状态代码和适当的消息。

    【讨论】:

      猜你喜欢
      • 2016-05-15
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 2020-04-08
      • 2016-08-29
      相关资源
      最近更新 更多