没有回退方法的@HystrixCommand 注释有什么用?

Rav*_*pta 5 spring hystrix

我的项目中有以下代码

@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();
     } 
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Iva*_*mar 3

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

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

1) 如果您不重写getFallback方法,将使用默认实现:

  protected R getFallback() {
        throw new UnsupportedOperationException("No fallback available.");
    }
Run Code Online (Sandbox Code Playgroud)

尽管事实上没有返回优雅的响应(仅抛出异常),断路器模式和快速失败原则仍然有效。

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