【发布时间】: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