1

我想使用 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 似乎更简单,但使用集成我发现不清楚。

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

更新:

根据建议我做了如下,

将 @EnableCircuitBreaker 注释添加到我的 Spring 引导应用程序类。

@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和 s pring-cloud-starter-eureka依赖项。

不确定 @hystrix 注释是否似乎可以解决问题。

4

1 回答 1

0

任何出站通道适配器(或网关)都可以配置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 Integration 的角度来看,我们可以@MessagingGateway在任何消息通道之前拥有一个。所以,我建议在@Gateway方法上尝试 Hystrix 注释:一个用于 HTTP 网关调用,另一个作为后备选项:https ://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/参考/html/messaging-endpoints-chapter.html#gateway

我添加了一些简单的示例sendboxhttps ://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
}
于 2018-04-12T01:48:25.040 回答