HystrixCommand注释 - 如何使用throw进行回退

时间:2016-02-05 16:05:03

标签: java netflix hystrix

我正在使用@HystrixCommand在java服务器中创建回退。

这是我有的一种方法,但我遇到的问题是我想知道我是否可以抛弃后退?

@HystrixCommand(fallbackMethod = "doFallback", commandKey = "doFallbackCommand")
   public Response getGraphPoints(String Id, String position) {

//do some work ... finally create a response

       return a_response;

   }

   public Response doFallback(String Id, String position) {
     //can i do this in hystrix command ? or do i really have to return a Response here?no other method will catch this throw for now
       throw new ServiceUnavailableException("points could not be found");
   }

我要问的原因是,当我运行这个时我得到以下错误:

ERROR [HystrixTimer-1] [com.netflix.hystrix.contrib.javanica.command.GenericCommand] [myserver] failed to processed fallback is the method: 'doFallback'. 

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,现在我对hystrix的工作原理有了一点了解。 Hystrix不要求您设置一个后备方法。除非您想要返回默认数据或为该案例添加业务逻辑,否则没有必要。如果你抛出异常,你就会混淆" hystrix,所以它会抛出一个HystrixRuntimeException。

我希望它有所帮助。

相关问题