Hystrix circuit breaker for microservices you must know

Microservices have become the mainstream technology architecture pursued by major manufacturers, and the prospect of learning microservices is very promising, and SpringCloud has become the mainstream microservice technology stack. This series of articles will focus on the SpringCloud technology stack, and comprehensively analyze and explain the actual application of the SpringCloud technology stack in the microservice scenario.

Spring Cloud Hystrix

Knowledge Index

  • Avalanche effect
  • Introduction to Hystrix
  • Case demonstration
  • global fallback
  • Hystrix Dashboard Monitoring Platform

1 Avalanche effect

In a distributed system environment, similar dependencies between services are very common, and a business call usually depends on multiple basic services. As shown in the figure below, for a synchronous call, when the inventory service is unavailable, the commodity service request thread is blocked. When there are large batches of requests to call the inventory service, the entire commodity service resource may eventually be exhausted, making it impossible to continue to provide external services. And this unavailability may be passed up the request call chain, a phenomenon known as the avalanche effect.

170502_7fqS_2663573

So, if an application cannot isolate failures from dependencies, the application itself is at risk of being dragged down. Therefore, in order to build a stable and reliable distributed system, our services should have self-protection capabilities. When the dependent services are unavailable, the current service starts the self-protection function to avoid the avalanche effect.

2 Introduction to Hystrix

Hystrix, which means porcupine in English, with thorns all over its body, because of its back covered with thorns, it has the ability to protect itself. .

HystrixIt is an Netflixopen source fault-tolerant system that also has self-protection capabilities.

HystrixUsed to isolate access to remote services, third-party libraries, and prevent cascading failures 雪崩效应.

HystrixHas the following characteristics:

请求熔断:类似于电路中的保险丝,当Hystrix Command请求后端服务失败数量超过一定比例(默认50%), 断路器会切换到开路状态(Open). 这时所有请求会直接失败而不会发送到后端服务. 断路器保持在开路状态一段时间后(默认5秒), 自动切换到半开路状态(HALF-OPEN).这时会判断下一次请求的返回情况, 如果请求成功, 断路器切回闭路状态(CLOSED), 否则重新切换到开路状态(OPEN). Hystrix的断路器就像我们家庭电路中的保险丝, 一旦后端服务不可用, 断路器会直接切断请求链, 避免发送大量无效请求影响系统吞吐量, 并且断路器有自我检测并恢复的能力。
服务降级:Fallback相当于是降级操作. 对于查询操作, 我们可以实现一个fallback方法, 当请求后端服务出现异常的时候, 可以使用fallback方法返回的值. fallback方法的返回值一般是设置的默认值或者来自缓存.告知后面的请求服务不可用了,不要再来了。
依赖隔离(采用舱壁模式,Docker就是舱壁模式的一种):在Hystrix中, 主要通过线程池来实现资源隔离. 通常在使用的时候我们会根据调用的远程服务划分出多个线程池.比如说,服务A的两个不同的接口分别调用了服务B和服务C,假设服务B出现了阻塞,同时服务B在服务A中对应的接口被频繁调用,由于服务B的阻塞导致了资源在调用B时被耗尽,此时其他请求服务A中服务C对应的接口时也会出现阻塞,为了避免这种情况Hystrix允许针对不同的服务调用配置不同的线程池,用以进行依赖隔离,如服务A中针对服务B和服务C的调用都配置了50个线程的线程池。此时如果在调用服务A时发生了阻塞,那么不会耗尽所有资源,此时服务A中的调用服务B的接口依然可以对外提供服务。
请求缓存:比如一个请求过来请求我userId=1的数据,你后面的请求也过来请求同样的数据,这时我不会继续走原来的那条请求链路了,而是把第一次请求缓存过了,把第一次的请求结果返回给后面的请求。
请求合并:利用一个合并处理器,将对同一个服务发起的连续请求合并成一个请求进行处理(这些连续请求的时间窗默认为10ms),在这个过程中涉及到的一个核心类就是HystrixCollapser。

Spring Cloud Hystrixyes Hystrixencapsulation makes it easy to use features in the Spring CloudsystemHystrix

3 case demonstration

Next we service-consumerdemonstrate Hystrixhow to use

3.1 Introducing dependencies

Introduced in service_consumer_pom.xml

<!--熔断Hystrix starter-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

3.2 Turn on the fuse

Turn on fuse via @EnableCircuitBreakerannotation

/**
 * Copyright (c) 2022 itmentu.com, All rights reserved.
 *
 * @Author: yang
 */
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class ServiceConsumerApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(ServiceConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced//开启负载均衡
    public RestTemplate restTemplate(){
    
    
        return new RestTemplate();
    }
}

3.3 Enable service downgrade

@HystrixCommandAnnotate the interface and the downgrade method that allow downgrade processing .

@GetMapping("/hello-loadbanlance")
@HystrixCommand(fallbackMethod ="fallBackMethod")
public String hello2(){
    
    
    // 1.通过注册中心的服务名构建url
    String url = "http://service-provider/hello";
    // 2.发送请求
    return restTemplate.getForObject(url,String.class);
}

@GetMapping("/hello-feign")
@HystrixCommand(fallbackMethod ="fallBackMethod")
public String hello3(){
    
    
    return helloFeign.hello();
}

// 熔断方法
public String fallBackMethod(){
    
    
    return "fail";
}

Code description:

1:@HystrixCommand注解用来标记一个可熔断的接口方法
2:fallbackMethod=““表示触发熔断后的快速响应结果
3:需注意熔断方法的返回值必须和@HystrixCommand标记的接口返回值相同

3.4 Configure circuit breaker policy

# 配置熔断策略:
hystrix:
  command:
    default:
      circuitBreaker:
        forceOpen: false # 强制打开熔断器 默认false关闭的
        errorThresholdPercentage: 50 # 触发熔断错误比例阈值,默认值50%
        sleepWindowInMilliseconds: 5000  # 熔断后休眠时长,默认值5秒
        requestVolumeThreshold: 20  # 熔断触发最小请求次数,默认值是20
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 1000  # 熔断超时设置,默认为1秒

Code Description:

1:当Hystrix Command请求后端服务失败数量超过errorThresholdPercentage配置比例(默认50%),断路器会切换到开路状态(Open). 
2:这时所有请求会直接失败而不会发送到后端服务. 断路器保持在开路状态一段时间后(默认5秒), 自动切换到半开路状态(HALF-OPEN).
3:这时会判断下一次请求的返回情况, 如果请求成功, 断路器切回闭路状态(CLOSED), 否则重新切换到开路状态(OPEN). 
4:Hystrix的断路器就像我们家庭电路中的保险丝, 一旦后端服务不可用, 断路器会直接切断请求链, 避免发送大量无效请求影响系统吞吐量, 并且断路器有自我检测并恢复的能力。

3.5 Testing

Start the service, call

http://localhost:8002/consumer/hello-loadbanlance
http://localhost:8002/consumer/hello-feign

After success, stop the service-providerinstance and call the circuit breaker again

image-20220320170021670

image-20220320170043537

4 Global fallback

ConsumerControllerAdd an @DefaultPropertiesannotation on it and configure the defaultFallbackdefault fallbackmethod by

@RestController
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback="defaultFallBack")
public class ConsumerController {
    
    
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private HelloFeign helloFeign;

    @GetMapping("/hello")
    public String hello(){
    
    
        //1、获取Eureka中注册的provider-service实例列表
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("service-provider");
        //2、获取实例
        ServiceInstance serviceInstance = serviceInstanceList.get(0);
        //3、根据实例的信息拼接的请求地址
        String url = serviceInstance.getUri()+ "/hello";
        //4、发送请求
        return restTemplate.getForObject(url,String.class);
    }

    @GetMapping("/hello-loadbanlance")
    @HystrixCommand
    public String hello2(){
    
    
        // 1.通过注册中心的服务名构建url
        String url = "http://service-provider/hello";
        // 2.发送请求
        return restTemplate.getForObject(url,String.class);
    }

    @GetMapping("/hello-feign")
    @HystrixCommand
    public String hello3(){
    
    
        return helloFeign.hello();
    }

    // 全局熔断方法
    public String defaultFallBack(){
    
    
        return "fail";
    }
}

Code Description:

1:@DefaultProperties表示默认配置
2:该配置只会对和全局熔断方法返回值相同的接口方法生效
3:当配置了defaultFallback,@HystrixCommand中无需配置即可快速失败,当然也可以自行配置

5 Hystrix Dashboard Monitoring Platform

HystrixThe official provides a graphical-based DashBoard(dashboard) monitoring platform. HystrixDashboards can display @HystrixCommandthe status of each circuit breaker (annotated method).

5.1 pom dependencies

<!--监控坐标-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--仪表板坐标-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

5.2 Open the Dashboard

/**
 * Copyright (c) 2022 itmentu.com, All rights reserved.
 *
 * @Author: yang
 */
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class ServiceConsumerApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(ServiceConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced//开启负载均衡
    public RestTemplate restTemplate(){
    
    
        return new RestTemplate();
    }
}

5.3 Configuration files

#暴露全部监控信息,可以只填写hystrix.stream,也可以暴露全部 * ,注意在yml文件中特殊符号需要加引号
management:
  endpoints:
    web:
      exposure:
        include: '*'

5.4 Console

Visit http://localhost:8002/hystrix and add the service to be monitored http://localhost:8002/turbine/turbine.stream

image-20220320173308448

image-20220320173503787

Guess you like

Origin blog.csdn.net/scmagic/article/details/123919191