【问题标题】:Wrap spring integration outbound gateway calls with hystrix command使用 hystrix 命令包装 spring 集成出站网关调用
【发布时间】:2018-09-22 00:25:20
【问题描述】:

我想在我的集成应用程序中包装对出站网关的调用,使用 hystrix 命令类似于它在 Spring Boot 应用程序中的可用方式。

<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>

我的出站网关如上所述。

对于目标应用程序经常停机/无响应且 我们正在寻找一种在这些情况下提供模拟响应的方法,并提供一个机会 要恢复的目标应用程序。

基本上,我想在这里使用 hystrix 命令并模拟一个断路器模式

我觉得使用 ExpressionEvaluatingRequestHandlerAdvice 和 RequestHandlerCircuitBreakerAdvice 的组合 可能会有所帮助,但我没有找到任何文档如何将它们一起用于我的场景。

使用 Spring Boot 似乎更简单,但使用集成我发现不清楚。

如果有人通过向出站网关添加自定义行为来实现类似行为,请告诉我们。

更新:

根据建议我做了如下,

在我的 Spring boot 应用程序类中添加了@EnableCircuitBreaker 注解。

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

.. }

另外,在我的网关接口中添加了@HystrixCommand 注释,如下所示,

@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;
    }
}

我在接口本身中添加了方法,因为 java 8 支持接口中的默认方法。

我什至尝试在接口中使用静态方法,如下所示。

@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;
    }
}

我使用了 Spring Boot 1.5.12 和 Spring cloud Edgware.SR3 版本。 我还在我的应用程序 pom.xml 中添加了 spring-cloud-starter-hystrix 和 spring-cloud-starter-eureka 依赖项。

不确定@hystrix 注释是否能解决问题。

【问题讨论】:

    标签: spring-boot spring-integration hystrix circuit-breaker


    【解决方案1】:

    任何出站通道适配器(或网关)都可以使用request-handler-advice-chain 进行配置,其中“链”是核心问题。所以,你真的可以将一个建议包装到另一个组合中,就像你的情况一样。一个个配置就够了。

    示例retry and more 应该会给你一些想法:https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/retry-and-more

    如果可能的话,我稍后会用 Hystrix 解决方案回来。

    更新

    嗯,根据 Spring Cloud 文档,我们可以有类似的东西:

    @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 */;
              }
       }
    

    从 Spring 集成的角度来看,我们可以在任何消息通道前面添加一个 @MessagingGateway。所以,我建议在 @Gateway 方法上尝试 Hystrix 注释:一个用于 HTTP 网关调用,另一个作为备用选项:https://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/reference/html/messaging-endpoints-chapter.html#gateway

    我在sendbox:https://github.com/artembilan/sendbox/tree/master/spring-integration-with-hystrix中添加了一些简单的示例

    所以,解决方案是这样的:

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

    【讨论】:

    • 感谢您的建议。我尝试使用 hystrix 注释,但它仍然无法处理服务关闭时的异常情况。我会尽快提供我所做的更改。
    • 在这里也可以看到一些讨论:stackoverflow.com/questions/45480446/…
    • 在我的回答中查看更新。
    • 谢谢。但是,我无法遵循重试和更多示例。可能需要更多时间来理解它。无论如何,您的建议很有帮助。
    • 酷!听起来是时候了:stackoverflow.com/help/someone-answers ?
    猜你喜欢
    • 1970-01-01
    • 2017-04-12
    • 2014-06-08
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多