Spring cloud 熔断器Hystrix

  • 在微服务的体系下,服务之间是与依赖关系的!服务的调用中也存在着各种可能的问题,如果一个服务处理缓慢,可能会导致服务被阻塞住,导致整个系统被拖慢,甚至是崩溃。这种情况下可以使用熔断的机制,让服务降级,不让问题继续蔓延。
  • Hystrix,是Netflix的一个开源熔断器,通过Hystrix,我们可以很方便的实现资源隔离、限流、超时设计、服务降级等服务容灾措施,并且还提供了强大的监控,可以查看各个熔断器的允许情况。
  • Hystrix框架图
  •  
  • Hystrix提供了一个HystrixCommand用来包装调用请求。HystrixCommand的执行流程大概如下
  • 首先检查缓存中是否有结果。如果有则直接返回缓存结果。
  • 判断断路器是否开启,如果断路器闭合,执行降级业务逻辑并返回降级结果。
  • 判断信号量/线程池资源是否饱和,如饱和则执行降级业务逻辑并返回降级结果。
  • 调用实际服务,如发生异常,执行降级业务逻辑并返回降级结果,并调整断路器阈值。
  • 判断实际业务是否超时,超时则返回超时响应结果,并调整断路器阈值。
  • Ribbon使用Hystrix

  • 演示项目
  • 在需要进行熔断的服务上进行修改
  • 修改pom.xml文件,增加Hystrix的依赖
  •         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            </dependency>
    
  • 启动类上增加Hystrix的注解
  • @SpringBootApplication
    @EnableDiscoveryClient
    @EnableCircuitBreaker
    public class HystrixRibbonApp {
        public static void main(String[] args) {
            SpringApplication.run(HystrixRibbonApp.class, args);
        }
        
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
  • 修改代码逻辑
  • @RestController
    public class HelloController {
        @Autowired
        HystrixRibbonService helloService;
        
        @RequestMapping("/hi")
        public String hello(){
            return helloService.helloService("姓名");
        }
    }
    
  • 在需要熔断的方法上增加@HystrixCommand注解,当调用有问题的时候就会使用fallbackMethod参数指定的方法进行服务降级
  • @Service
    public class HystrixRibbonService {
        private static final String SERVICE_NAME = "EUREKACLIENT";
        
        @Autowired
        RestTemplate restTemplate;
        
        @Autowired
        private LoadBalancerClient loadBalancerClient;
    
        @HystrixCommand(fallbackMethod = "helloServiceFallBack")
        public String helloService(String name) {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose(SERVICE_NAME);
            System.out.println("服务主机:" + serviceInstance.getHost());
            System.out.println("服务端口:" + serviceInstance.getPort());
            
            //  通过服务名来访问
            return restTemplate.getForObject("http://" + SERVICE_NAME + "/hello?name="+name,String.class);
        }
    
        @SuppressWarnings("unused")
        private String helloServiceFallBack(String name) {
            return "这个是失败的信息!";
        }
    }
    
  • Feign使用Hystrix

  • 演示项目
  • 修改pom.xml文件,增加Hystrix的依赖
  •         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
    
  • 增加注解
  • @SpringBootApplication
    @EnableDiscoveryClient
    @EnableCircuitBreaker
    public class HystrixFeignApp {
        public static void main(String[] args) {
            SpringApplication.run(HystrixFeignApp.class, args);
        }
    }
    
  • 书写逻辑代码
  • @RestController
    public class HelloController {
        @Autowired
        HystrixFeignService helloService;
        
        @RequestMapping("/hifeign")
        public String hello(){
            return helloService.sayHiUseFeign("姓名");
        }
    }
    
  • 指定fallback的类
  • @FeignClient(value="EUREKACLIENT", fallback = HystrixFeignServiceFallback.class)
    public interface HystrixFeignService {
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        String sayHiUseFeign(@RequestParam(value = "name") String name);
    }
    
  • fallback的逻辑,是服务接口的一个实现,书写服务降级逻辑
  • @Component
    public class HystrixFeignServiceFallback implements HystrixFeignService{
        @Override
        public String sayHiUseFeign(String name) {
            return "feign调用错误!";
        }
    }
    
  • 仪表盘

  • 在pom.xml中增加依赖
  •         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  • 增加注解
  • @EnableHystrixDashboard
    @SpringCloudApplication
    public class HystrixRibbonApp {
        public static void main(String[] args) {
            SpringApplication.run(HystrixRibbonApp.class, args);
        }
        
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
  • 需要有@HystrixCommand熔断命令被使用
  • 启动应用:http://host:port/hystrix

    这是一个可爱的小熊

  • 监控的画面
  • 不错的资料:
  • http://blog.csdn.net/zheng0518/article/details/51713900
  • https://github.com/Netflix/Hystrix/wiki/How-To-Use
  • Hystrix Turbine聚合Hystrix stream信息

  • Hystrix Turbine将每个服务Hystrix Dashboard数据进行了整合。
  • 新建一个项目:springCloudTurbine
  • 增加pom.xml依赖
  •         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-turbine</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-netflix-turbine</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  • 增加注解
  • @SpringBootApplication
    @EnableTurbine
    public class ServiceTurbineApplication {
        public static void main(String[] args) {
            new SpringApplicationBuilder(ServiceTurbineApplication.class).web(true).run(args);
        }
    }
    
  • 修改配置信息
    可以在turbine.appConfig配置监控的应用名称
  • eureka.client.serviceUrl.defaultZone=http://master:8671/eureka/,http://backup:8672/eureka
    #eureka.client.serviceUrl.defaultZone=http://lxm:111111@localhost:1111/eureka/
    security.basic.enabled=false
    # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
    turbine.aggregator.clusterConfig=default
    # 配置Eureka中的serviceId列表,表明监控哪些服务
    turbine.appConfig=hystrixClient1,hystrixClient2,hystrixClient3
    turbine.clusterNameExpression=new String("default")
    # 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称
    # 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
    # 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC  
    
  • 启动应用:http://host:port/hystrix
    监控:http://host:port/turbine.stream
  •  
  •  
  • 作者:hutou
  • 链接:https://www.jianshu.com/p/a28960a8e829
  • 來源:简书
  • 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自blog.csdn.net/y532798113/article/details/81273147