spring - 如果没有后备方法,@HystrixCommand 注解有什么用?

标签 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();
     } 
}

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

未定义后备时使用 HystrixCommand 的用例是什么?

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

最佳答案

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

Stop cascading failures. Fallbacks and graceful degradation. Fail fast and rapid recovery.

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

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

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

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

关于spring - 如果没有后备方法,@HystrixCommand 注解有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49807726/

相关文章:

java - 使用重定向时 Spring MVC 消息未出现在 View 中

java - API证书不受浏览器信任,我该怎么办?

spring - 如何使用 Spring Cloud 将 HystrixProperty 设置为 Feign 请求?

java - tomcat : slf4j doesn't find logback. xml 上的 spring mvc 应用程序,仅记录到 catalina.out

java - 在 Eclipse 中创建新的 Maven 项目时显示的错误

java - 从基类 stub 方法时,Mockito 抛出 NPE

java - 当响应为空时释放 apache http 连接到池

aop - Hystrix 命令不在 Hystrix 环境中运行

java - 将 Spring 托管 bean 的引用传递给非托管类?

java - 如何正确处理 Hystrix 回退中的预期错误?