美文网首页
熔断器 Hystrix

熔断器 Hystrix

作者: 隔壁小王coding | 来源:发表于2020-01-29 17:30 被阅读0次

    熔断器Hystrix

    版本 2.2.1.RELEASE

    1. 介绍

     Hystrix是一个延迟和容错工具,旨在隔离远程系统,服务以及第三方库,阻止级联故障,在复杂的分布式系统中实现恢复能力。


     Hysrix的设计目标:

    1. 对延迟和故障进行隔离保护
    2. 在一个复杂的分布式系统中防止级联故障
    3. 快速失败(防止阻塞)和快速恢复
    4. 回退和优雅的降级
    5. 开启近实时监控,报警和操作控制

    2. 流程图 hystrix-command-flow-chart.png

    1. 创建一个HystrixCommand或者HystrixObservableCommand对象
    2. 执行command命令
    3. 判断缓存是否可用
    4. 判断短路器是否处于打开状态
    5. 判断线程池/队列/信号量是否已经达到最大值
    6. 执行HystrixCommand中的目标方法
    7. 根据目标方法的返回情况决定短路器是否打开
    8. 获取fallback返回对象
    9. 获取正常的返回对象

    3. 简单上手

    spring-hystrix

     引入相关依赖

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

     在启动类上添加注解@EnableHystrix

    @EnableHystrix
    @EnableDiscoveryClient
    @SpringBootApplication
    public class CloudHystrixApplication {
        public static void main(String[] args) {
            SpringApplication.run(CloudHystrixApplication.class, args);
        }
    }
    

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

        @ResponseBody
        @RequestMapping(value = "/getUser", method = RequestMethod.GET)
        @HystrixCommand(fallbackMethod = "defaultUser")
        public String getUser(String userName) throws Exception{
            if("wang".equals(userName)){
                return "this is real user";
            } else {
                throw new Exception("123");
            }
        }
    
        /**
         * hystrix的fallback方法
         * @param userName
         * @return
         */
        public String defaultUser(String userName){
            return "The user does not exist";
        }
    

     启动工程,当输入错误的参数时,就会返回fallback方法的对象

    hystrix配合feign使用

     feign内嵌了Hystrix和Eureka,所以就使用feign的依赖即可

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

     在启动类上增加@EnableFeignClients注解

    @EnableDiscoveryClient
    @SpringBootApplication
    @EnableFeignClients
    public class CloudEurekaClientApplication {
        public static void main(String[] args) {
            SpringApplication.run(CloudEurekaClientApplication.class, args);
        }
    }
    

     新增feign接口,调用目标服务的url,接口上添加@FeignClient注解

    @FeignClient(name = "Hystrix", fallback = HystrixFallback.class)
    public interface Hystrix {
        @RequestMapping(value = "/getUser", method = RequestMethod.GET)
        String getUser(@RequestParam("userName") String userName);
    }
    

     同时,创建实现类实现该接口,在实现类中实现fallback返回对象,实现类要用@Component注解修饰

    @Component
    public class HystrixFallback implements Hystrix {
        @Override
        public String getUser(String userName) {
            return "the user does not exist in this system";
        }
    }
    

     在需要调用该接口的类中自动注入该接口

    @Controller
    public class UserController {
    
        @Autowired
        private Hystrix hystrixClient;
    
        @ResponseBody
        @RequestMapping(value = "/getUser", method = RequestMethod.GET)
        public String getUser(String userName){
            String user = hystrixClient.getUser(userName);
            return user;
        }
    }
    

     在该工程的properties文件新增下列语句,开启feign的Hystrix

    feign.hystrix.enabled=true
    

     启动工程,调用对应的url,如果参数输入错误,如果被调用方出现异常,则返回HystrixFallback实现的fallback对象

    Hystrix-Dashboard

     新建一个工程,引入dashboard相关依赖

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

     在启动类上添加@EnableHystrixDashboard注解

    @EnableHystrixDashboard
    @SpringBootApplication
    @EnableDiscoveryClient
    public class CloudHystrixDashboardApplication {
        public static void main(String[] args) {
            SpringApplication.run(CloudHystrixDashboardApplication.class, args);
        }
    }
    
     浏览器输入对应的网址ip:port/hystrix即可访问dashboard页面 image.png

    Hystrix或者Feign如何整合dashboard

     在hystrix工程的依赖中添加spring-actuator依赖

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    

     在该工程的properties文件中新增如下语句

    management.endpoints.web.exposure.include=hystrix.stream
    

     将http://localhost:8081/actuator/hystrix.stream输入到dashboard页面中的地址栏中,点击monitor stream按钮即可进入监控页面

    image.png
    feign整合dashboard

     和上面hystrix整合dashboard原理差不多,只是在启动类上除了有@FeignClients还需要添加@EnableHystrix注解

    @EnableDiscoveryClient
    @SpringBootApplication
    @EnableFeignClients
    @EnableHystrix
    public class CloudEurekaClientApplication {
        public static void main(String[] args) {
            SpringApplication.run(CloudEurekaClientApplication.class, args);
        }
    }
    

    相关文章

      网友评论

          本文标题:熔断器 Hystrix

          本文链接:https://www.haomeiwen.com/subject/fmgvthtx.html