hystrix的作用是隔离远程访问远程系统、服务或者第三方库,防止级联失败,从而提高系统可用性、容错性与局部应用的弹性

使用方式

1.导入依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  4. </dependency>

2.在启动类上添加注解@EnableHystrix

3.在需要进行熔断的方法上添加注解@HystrixCommand

  1. @HystrixCommand(fallbackMethod = "printDefaultMsg",
  2. threadPoolKey = "testThreadPool",
  3. threadPoolProperties = {
  4. @HystrixProperty(name = "coreSize", value = "30"),
  5. @HystrixProperty(name = "maxQueueSize", value = "10")
  6. }
  7. )
  8. @RequestMapping("/test1")
  9. public String test1(){
  10. String url = "http://eureka-application-service/test2";
  11. return restTemplate.getForObject(url, String.class);
  12. }