hystrix熔断器使用

Hystrix是Netflix开源的一款容错框架,包含常用的容错方法:线程池隔离、信号量隔离、熔断、降级回退。
在高并发访问下,系统所依赖的服务的稳定性对系统的影响非常大,依赖有很多不可控的因素,比如网络连接变慢,资源突然繁忙,暂时不可用,服务脱机等。
我们要构建稳定、可靠的分布式系统,就必须要有这样一套容错方法。
————————————————
版权声明:本文为CSDN博主「makyan」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/makyan/java/article/details/88662141

 

Hystrix要使用在服务的调用方,而不是服务的提供方。

1.添加依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

2.在服务调用方的引导类上添加注 解    @EnableCircuitBreaker 

@SpringBootApplication
@EnableDiscoveryClient  //  启用eureka注册中心的客户端
@EnableCircuitBreaker  // 启用熔断器
public class CustomerApplication {

    @Bean
    @LoadBalanced // 启用 ribbon 负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {

        SpringApplication.run(CustomerApplication.class, args);
    }

}

3.在controller上或者方法上添加注解使用 fallback 调用服务的熔断后的函数,返回值必须相同

@Controller
@RequestMapping("/customer/user")
public class UserController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/queryById")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "queryByIdfallback")
    public String queryById(@RequestParam(name = "id",required = true)int id){
        List<ServiceInstance> instances = discoveryClient.getInstances("ITCAST-PROVIDER");//获取指定服务名的提供的所有服务
        ServiceInstance serviceInstance = instances.get(0);
        return this.restTemplate.getForObject("http://ITCAST-PROVIDER/user/queryById?id="+id,String.class);
    }

    public String queryByIdfallback(int id){

        return "服务正忙,请稍后再试";
    }
}

在需要使用熔断调用函数的方法上使用  @HystrixCommand 注解的  fallbackMethod 属性,定义熔断后 的调用函数,熔断函数的参数要保持一致 ,适用于单个方法上

当你需要熔断的函数过多的使用,在使用单个的熔断,就过于繁琐,每一个方法都需要一个熔断,所以可以使用全局的熔断,使用在类上 

使用注解    

@DefaultProperties:这个使用在类上,声明全局的熔断函数

@HystrixCommand:那个方法需要使用熔断函数,就是用这个类,默认使用 全局的熔断 ,也可以配置自己的熔断函数,fallbackMethod 属性指明自己的熔断函数

另外,在全局的熔断函数不需要参数

package cn.itcast.service.controller;

@Controller
@RequestMapping("/customer/user")
@DefaultProperties(defaultFallback = "fallbackMethod")
public class UserController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/queryById")
    @ResponseBody
    @HystrixCommand
    public String queryById(@RequestParam(name = "id",required = true)int id){
        List<ServiceInstance> instances = discoveryClient.getInstances("ITCAST-PROVIDER");//获取指定服务名的提供的所有服务
        ServiceInstance serviceInstance = instances.get(0);
        return this.restTemplate.getForObject("http://ITCAST-PROVIDER/user/queryById?id="+id,String.class);
    }

    public String fallbackMethod(){

        return "服务正忙,请稍后再试";
    }
}

 4.当你访问资源的时候hystrix默认的超时时间是1s,但是在上线之后,服务间的调用需要网路传输,1s是不够的,需要覆盖默认的超时时间,在配置文件中

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000 #设置hystrix超时时间,默认为ms

但是时会发现在引导类上有了好多注解,不方便记忆,可以使用一个组合注解   @SpringCloudApplication

//@SpringBootApplication
//@EnableDiscoveryClient  //  启用eureka注册中心的客户端
//@EnableCircuitBreaker  // 启用熔断器
@SpringCloudApplication // 上面三个的组合注解
public class CustomerApplication {

 

posted @ 2020-05-05 18:21  刀锋意志1  阅读(283)  评论(0编辑  收藏  举报