天天看点

Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心

推荐:​​微服务汇总​​

Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心

为了大家能更好的了解Hystrix原理,可以读一下这篇博客:​​Hystrix原理​​

在之前的博客中,我介绍了Hystrix的基本使用:​​Spring Cloud 之Hystrix初使用​​

所以这里有一些重复且不相关的代码就不写了,比如注册中心,Server服务的​

​pom.xml​

​​和​

​application.yml​

​​、Client服务的​

​pom.xml​

​​、​

​application.yml​

​以及RestTemplate组件。

Server服务的getMessage接口:

package com.kaven.server.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageServerController {

    @GetMapping("/getMessage")
    public String getMessage(){
        System.out.println("进来了");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("2000ms后又开始了");
        return "hello , Jack";
    }
}      

​Thread.sleep(2000);​

​的原因是想模拟请求服务超时,从而导致请求错误率达到进入熔断器打开的阈值。

Client服务的getMessage接口:

package com.kaven.client.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class MessageController{

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled" , value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold" , value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds" , value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage" , value = "60")
    })
    @GetMapping("/getMessage")
    public String getMessage(@RequestParam("flag") Integer flag){

        if(flag % 2 == 0){
            String message = restTemplate.getForObject("http://server/getMessage" , String.class);
            return "get: "+message;
        }
        else{
            return "success";
        }
    }

    public String defaultFallback(){
        return "error";
    }
}      

​flag​

​参数的作用是用来控制请求错误与成功的占比,方便演示熔断器的状态。

关键是下面这四个属性配置。

@HystrixProperty(name = "circuitBreaker.enabled" , value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold" , value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds" , value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage" , value = "60")      

熔断器配置

​Circuit Breaker​

​主要包括如下6个参数:

  1. ​circuitBreaker.enabled​

    ​ :是否启用熔断器,默认是TRUE。
  2. ​circuitBreaker.forceOpen​

    ​:熔断器强制打开,始终保持打开状态,不关注熔断开关的实际状态。默认值FLASE。
  3. ​circuitBreaker.forceClosed​

    ​:熔断器强制关闭,始终保持关闭状态,不关注熔断开关的实际状态。默认值FLASE。
  4. ​circuitBreaker.errorThresholdPercentage​

    ​:错误率阈值,默认值50%,例如一段时间(10s)内有100个请求,其中有54个超时或者异常,那么这段时间内的错误率是54%,大于了默认值50%,这种情况下会触发熔断器打开。
  5. ​circuitBreaker.requestVolumeThreshold​

    ​​:默认值20。含义是一段时间内至少有20个请求才进行​

    ​errorThresholdPercentage​

    ​计算。比如一段时间有19个请求,且这些请求全部失败了,错误率是100%,但熔断器不会打开,总请求数不满足20。
  6. ​circuitBreaker.sleepWindowInMilliseconds​

    ​:半开状态试探睡眠时间,默认值5000ms。如:当熔断器开启5000ms之后,会尝试放过去一部分流量进行试探,确定依赖服务是否恢复。

源码中的默认值:

private static final Boolean default_circuitBreakerEnabled = true;
   private static final Integer default_circuitBreakerRequestVolumeThreshold = 20;// default => statisticalWindowVolumeThreshold: 20 requests in 10 seconds must occur before statistics matter
   private static final Integer default_circuitBreakerSleepWindowInMilliseconds = 5000;// default => sleepWindow: 5000 = 5 seconds that we will sleep before trying again after tripping the circuit
   private static final Integer default_circuitBreakerErrorThresholdPercentage = 50;// default => errorThresholdPercentage = 50 = if 50%+ of requests in 10 seconds are failures or latent then we will trip the circuit
   private static final Boolean default_circuitBreakerForceOpen = false;// default => forceCircuitOpen = false (we want to allow traffic)
  /* package */ static final Boolean default_circuitBreakerForceClosed = false;// default => ignoreErrors = false      

代码都写好了,接下来要演示熔断器的各种状态,为了可视化熔断器的状态,这里还要介绍Hystrix-Dashboard。

Client服务还需要加入如下依赖:

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

Hystrix-Dashboard是通过浏览器请求固定接口来访问的,所以这里直接暴露所有的端点(接口):

management:
  endpoints:
    web:
      exposure:
        include: '*'      

访问​

​http://localhost:8002/hystrix​

​,可以得到如下图所示的界面,填入相关信息即可。

Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心

点击​

​Monitor Stream​

​,可以得到如下图所示的界面。

Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心

接下来我们使用Postman来进行演示,不会有人去疯狂点吧(滑稽),大家按下面图示跟着操作即可。

Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心
Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心
Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心
Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心
Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心

这里直接请求100次,前面几次执行会慢一些,请求到达十次之后就执行非常快了,这是为什么呢?因为一段时间内至少有10个请求才进行​

​errorThresholdPercentage​

​​计算( ​

​@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold" , value = "10")​

​​),所以,即使前几次请求错误率是100%,熔断器也不会打开,当请求到达十次,满足了​

​requestVolumeThreshold​

​​,并且错误率是100%,熔断器就打开了,其实错误率达到60%就可以了(​

​@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage" , value = "60")​

​);这样后续的请求,会快速返回,这就是为什么当请求到达十次之后执行会非常快的原因。

Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心

错误率100%,熔断器打开了。

Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心

等10秒(​

​@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds" , value = "10000")​

​),之后熔断器尝试半开(其实在Dashboard上还是会显示open状态),放入一部分流量请求进来,相当于对依赖服务进行一次健康检查,如果请求成功,熔断器关闭,我们来演示请求成功到底会不会导致熔断器关闭。

Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心

请求​

​http://127.0.0.1:8002/getMessage?flag=1​

​很显然会成功。

Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心

熔断器确实关闭。

Spring Cloud 之Hystrix熔断器、Hystrix-Dashboard可视化监控中心