我想使用 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 注释是否似乎可以解决问题。