hystrix的作用是隔离远程访问远程系统、服务或者第三方库,防止级联失败,从而提高系统可用性、容错性与局部应用的弹性
使用方式
1.导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.在启动类上添加注解@EnableHystrix
3.在需要进行熔断的方法上添加注解@HystrixCommand
@HystrixCommand(fallbackMethod = "printDefaultMsg",
threadPoolKey = "testThreadPool",
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "30"),
@HystrixProperty(name = "maxQueueSize", value = "10")
}
)
@RequestMapping("/test1")
public String test1(){
String url = "http://eureka-application-service/test2";
return restTemplate.getForObject(url, String.class);
}