zoukankan      html  css  js  c++  java
  • Spring Cloud (断路器) Hystrix(三)

      在微服务当道的现下,系统架构中由业务拆分出多个系统之间,通常是通过远程RPC调用进行通信,比如系统1调用系统2的服务,系统2调用系统3,当系统3发生故障的时候就会导致,可能会导致前置的两个系统发生崩溃,所以在系统架构中通常要保证系统的健壮性,比如使用降级策略,来保证由其他系统提供的服务发生错误不可用的时候,服务的调用方可以切换降级到正常的服务上使用。

    Hystrix开源的一个限流熔断的项目

      Netflix开源了Hystrix组件,实现了断路器模式,Spring Cloud 对这一组组件进行了组合

    特点:隔离、降级、熔断、缓存

      隔离:包括线程池隔离和信号隔离,限制调用分布式服务的资源使用,一个微服务出现问题不会影响其他服务

      降级:超时降级、资源不足时降级、降级后可以配合降级接口返回托底数据

      熔断:当失败率达到阀值自动触发降级(比如网络故障、服务异常),熔断器触发的快速失败会进行快速恢复。

      缓存:提供请求缓存、请求合并实现

    熔断

      正常情况下,断路器处于关闭状态。如果在某段时间内,调用持续出错或者超时,断路器会被打开进入熔断状态,后续一段时间内的所有请求调用都会被拒绝。一段时间以后,保护器会尝试进入半熔断状态,开放少量的请求进来,如果调用败任然回到熔断状态,如果调用成功服务会到正常状态,断路状态关闭。

    三种状态的转换:

      closed -> open: 正常情况熔断器为close状态,当访问的同一个服务或者接口次数超过一定的阀值,并且错误比例超过我们预定的阀值时,就会打开熔断机制,这个时候熔断器的状态就会从closed变成open状态

      open -> half-open:当服务的熔断器状态为open的时候,所有的服务调用方在请求该服务的时候都是在执行本地的降级方法,Hystrix提供了一个策略,从open状态开始的时候记录了一个时间窗口,当熔断时间超过了这个时间时,就会把状态变更为half-open,这个时候远程调用请求服务接口时,发起的是服务正常应该提供的调用,而不再是降级策略,如果服务调用正常熔断状态会从half-open->closed转变,熔断关闭

    如果请求还是异常,就继续从half-open->open转变

    如果没有发生熔断,还有一道门槛,Hystrix提供了一个信号量限流器,限制进入熔断器最大并发数,可以控制请求下游的并发量,如果超过这个阈值,会被降级处理,有效的保护下游服务不会被突发流量给攻击。

    在ribbon使用断路器

      将前两节中的eureka服务与client启动起来,在service-ribbon项目中添加Hystrix引用

     <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    

      启动类中添加注解@EnableHystrix 

    @EnableDiscoveryClient
    @EnableEurekaClient
    @SpringBootApplication
    @EnableHystrix
    public class ServiceribbonApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ServiceribbonApplication.class, args);
        }
    
        @Bean
        @LoadBalanced
        RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }
    

      修改Controller加上@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法

    @RestController
    @RequestMapping("hello")
    public class HelloControler {
        @Autowired
        HelloService helloService;
    
        @HystrixCommand(fallbackMethod = "hiError")
        @GetMapping(value = "/hi")
        public String hi(@RequestParam String name) {
            String a=helloService.hiService( name );
            return a;
        }
    
        public  String hiError(String name){
            return "调用服务下线";
        }
    }
    

      run起来,请求hi接口的时候,调用服务不通或者异常的时候将会执行指定的熔断方法。

      正常启动eureka的client后,运行service-ribbon可以正常调用服务并返回字符串

    hi aa ,i am from port:1002
    

      手动停掉client后,再次请求

    调用服务下线

     

  • 相关阅读:
    I.MX6 busybox set hosname and login with root
    Linux busybox mount -a fstab
    MDEV Primer
    qtcreator cannot find -lts
    I.MX6 linux Qt 同时支持Touch、mouse
    Android ashmem hacking
    I.MX6 Android U-blox miniPCI 4G porting
    Android service binder aidl 关系
    OK335xS psplash 进度条工作原理 hacking
    设置Android默认锁定屏幕旋转
  • 原文地址:https://www.cnblogs.com/li-lun/p/11494500.html
Copyright © 2011-2022 走看看