SpringCloud 如何包括Hystrix
要将Hystrix包含在您的项目中,请使用起始者,其组ID为org.springframework.cloud,工件ID为spring-cloud-starter-netflix-hystrix。有关使用当前Spring Cloud版本Train设置构建系统的详细信息,请参见Spring Cloud项目页面。
以下示例显示了具有Hystrix断路器的最小Eureka服务器:
@SpringBootApplication
@EnableCircuitBreaker
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
@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 */;
}
}
@HystrixCommand由一个名为“ javanica ”的Netflix contrib库提供。Spring Cloud将带有注释的Spring beans自动包装在与Hystrix断路器连接的代理中。断路器计算何时断开和闭合电路,以及在发生故障时应采取的措施。
要配置@HystrixCommand,可以将commandProperties属性与@HystrixProperty批注一起使用。有关 更多详细信息,请参见 此处。有关 可用属性的详细信息,请参见Hystrix Wiki。