Wrap spring integration outbound gateway calls with hystrix command

荒凉一梦 提交于 2019-12-24 08:47:35

问题


I want to wrap the call to outbound gateway in my integration application using hystrix command similar to how it is available in Spring boot application.

<int-http:outbound-gateway id="outbound.gateway"
    request-channel="get.request.channel" url="http://localhost:9090/profileapplication"
    http-method="GET" charset='UTF-8' reply-channel="reply.channel"
    >
</int-http:outbound-gateway>

I have my outbound gateway as above.

I need this for a scenario where the target application is frequently down/not responsive and we are looking for a way to provide a mock response during those scenarios and provide a chance for the target application to recover.

Basically, I want to use hystrix command and simulate a circuit breaker pattern over here.

I feel using a combination of ExpressionEvaluatingRequestHandlerAdvice and RequestHandlerCircuitBreakerAdvice might be helpful but I did not find any docs how to use them together for my scenario.

With Spring Boot it seems simpler, but with Integration I find it is not clear.

If anyone has implemented similar behaviour by adding custom behaviour to outbound gateway please let us know.

UPDATE:

As per Suggestion I did as below,

Added the @EnableCircuitBreaker annotation to my Spring boot application class.

@SpringBootApplication
@RestController
@ImportResource("classpath:/META-INF/spring/integration/hystrix-outbound-config.xml")
@EnableCircuitBreaker
public class HystrixIntegrationApplication {

.. }

Also, added @HystrixCommand annotation to my Gateway interface as below,

@MessagingGateway
public interface HystrixServiceGateway {


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

    default String getMockData(String name) {
        return "Mock Data:" + name;
    }
}

I added the method in the interface itself as java 8 supports default methods in interface.

I even tried with static methods in interface as below.

@MessagingGateway
public interface HystrixServiceGateway {


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

    static String getMockData(String name) {
        return "Mock Data:" + name;
    }
}

I used Spring Boot 1.5.12 and Spring cloud Edgware.SR3 versions. I also added the spring-cloud-starter-hystrix and spring-cloud-starter-eureka dependencies to my application pom.xml.

Not sure if the @hystrix annotation seems to resolve the issue.


回答1:


Any Outbound Channel Adapter (or Gateway) can be configured with the request-handler-advice-chain, where “chain” is the core concern. So, you really can wrap one advice to another having their combination, like in your case. There is just enough to configure one after another.

The sample retry and more should give you some ideas: https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/retry-and-more

I’ll come back later with the Hystrix solution if that is possible.

UPDATE

Well, according Spring Cloud docs we can have something like:

@Component
 public class StoreIntegration {

         @HystrixCommand(fallbackMethod = "defaultStores")
          public Object getStores(Map<String, Object> parameters) {
                   //do stuff that might fail
          }

          public Object defaultStores(Map<String, Object> parameters) {
                   return /* something useful */;
          }
   }

And from the Spring Integration perspective we can have a @MessagingGateway in front of any message channel. So, I’d suggest to try that Hystrix annotation on the @Gateway method: one for the HTTP Gateway call and another as a fallback option: https://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/reference/html/messaging-endpoints-chapter.html#gateway

I added some simple sample to my sendbox: https://github.com/artembilan/sendbox/tree/master/spring-integration-with-hystrix

So, the solution is like this:

@ServiceActivator(inputChannel = "serviceChannel")
@HystrixCommand(fallbackMethod = "serviceFallback")
public String myService(String payload) {
    // Some external service call
}

public String serviceFallback(String payload) {
    // some fallback logic
}


来源:https://stackoverflow.com/questions/49786571/wrap-spring-integration-outbound-gateway-calls-with-hystrix-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!