当用于包装Spring Integration Gateway调用时,HystrixCommand Annotation不起作用

时间:2018-04-15 09:48:38

标签: spring-boot spring-integration hystrix

我想在Spring Integration Application中为我的网关调用添加一个回退。

我已使用@HystrixCommand注释将方法调用到网关,并在fallbackMethod属性中提供了回退方法的名称。

@HystrixCommand(fallbackMethod = "getMockData")
public String gatewayCallGetMessage(String name) {
   return serviceGateway.getMessage(name);
}

public String getMockData(String name) {
  return "mock data";
}

我还在同一个类中定义了回退方法。

我的网关界面如下,

@MessagingGateway
public interface HystrixServiceGateway {

    @Gateway(requestChannel = "get.request.channel", replyChannel = "reply.channel")
    String getMessage(String name);

}

我在classpath / pom.xml中有 spring-cloud-starter-hystrix 依赖项。 另外,我在Spring Boot Application类中有 @EnableHystrix 注释,如下所示。

@EnableHystrix
public class HystrixIntegrationApplication {

...
}

但是,当服务停止时,网关调用不会通过,但回退方法没有被执行。

我已经分享了我用来重现我的问题的代码,如下所示, https://github.com/sri420/hystrix-integration-demo

如果有人遇到类似的问题,并知道如何处理,请告诉我。

1 个答案:

答案 0 :(得分:1)

方法gatewayCallGetMessage是从getByName调用的,它位于同一个类中。无法在同一个类中调用@HystrixCommand的方法。

正如this问题的答案中所讨论的,这是对Spring的AOP的限制。