zoukankan      html  css  js  c++  java
  • SpringCloud07Hystrix

    Hystrix 熔断器

    1、Hystrix 概述

    Hystix 是 Netflix 开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败(雪崩)。

     雪崩:一个服务失败,导致整条链路的服务都失败的情形。

    Hystix 主要功能:

    1.   隔离
      1. 线程池隔离
      2. 信号量隔离
    2.   降级:异常,超时
    3.   熔断
    4.   限流

    2、Hystrix 降级 – 服务提供方

    在服务提供方,引入 hystrix 依赖

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

    定义降级方法

        @GetMapping("/{id}")
        @HystrixCommand(fallbackMethod = "queryById_fallback",commandProperties = {
                @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
                @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
                @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"),
                @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50"),
        })
        public User queryById(@PathVariable("id") Long id,
                              @RequestHeader(value = "filter",required = false) String filter,
                              @RequestHeader(value = "default-filter",required = false) String default_filter) {
    //        int i=5/0;
            if(id==1){
                int i=1/0;
            }
    //        try {
    //            Thread.sleep(2000);
    //        } catch (InterruptedException e) {
    //            e.printStackTrace();
    //        }
            System.out.println(filter);
            System.out.println(default_filter);
            return userService.queryById(id);
        }
    
        public User queryById_fallback(Long id, String filter, String default_filter) {
            User user=new User();
            user.setUsername("错误用户...");
            return user;
        }

    使用 @HystrixCommand 注解配置降级方法

    @HystrixCommand(fallbackMethod = "queryById_fallback",commandProperties = {
                @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
                @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
                @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"),
                @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50"),
        })

    在启动类上开启Hystrix功能:@EnableCircuitBreaker

    //@MapperScan("cn.itcast.user.mapper")
    @SpringBootApplication
    @EnableCircuitBreaker
    public class UserApplication {
        public static void main(String[] args) {
            SpringApplication.run(UserApplication.class, args);
        }
    }

    3、Hystrix 降级 – 服务消费方

    feign 组件已经集成了 hystrix 组件。

    定义feign 调用接口实现类,复写方法,即 降级方法

    在 @FeignClient 注解中使用 fallback 属性设置降级处理类。

    @Component
    public class UserClientCallBack implements UserClient {
        @Override
        public User findUserById(Long id) {
            User user = new User();
            user.setUsername("调用端降级...");
            return user;
        }
    }

    配置开启 feign.hystrix.enabled = true

    4、Hystrix 熔断

    Hystrix 熔断机制,用于监控微服务调用情况,当失败的情况达到预定的阈值(5秒失败20次),会打开断路器,拒绝所有请求,直到服务恢复正常为止。

     调节熔断参数:

    1. circuitBreaker.sleepWindowInMilliseconds:监控时间
    2. circuitBreaker.requestVolumeThreshold:失败次数
    3. circuitBreaker.errorThresholdPercentage:失败率

    5、Hystrix 熔断监控

    Hystrix 提供了 Hystrix-dashboard 功能,用于实时监控微服务运行状态。 但是Hystrix-dashboard只能监控一个微服务。 Netflix 还提供了 Turbine ,进行聚合监控。

     

  • 相关阅读:
    python数据1-5
    python密码1-2
    css Loading 教程
    定制化jQuery
    PHP 将MySQL数据导出csv
    windows MySQL 5.7 导出表方法记录
    前端UI
    我的框架说明文档 2016-04-06
    微信公众号内支付(三)
    微信公众号内支付(二)
  • 原文地址:https://www.cnblogs.com/sun-10387834/p/15685404.html
Copyright © 2011-2022 走看看