天天看點

SpringCloud(七)hystrix Dashboard

前言:

    官網解釋如下:

One of the main benefits of Hystrix is the set of metrics it gathers about each HystrixCommand. 
The Hystrix Dashboard displays the health of each circuit breaker in an efficient manner.      

Hystrix的主要優點之一是它收集的每個HystrixCommand的名額集。Hystrix訓示闆以一種有效的方式顯示每個斷路器的健康狀況。

代碼:

修改上一篇部落格代碼eureka client的代碼

加入下面maven依賴:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

actuator提供對服務的監控,dashboard為ui

要使用Dashboard必須加兩個注解:

@EnableHystrix
//@EnableCircuitBreaker  //與@EnableHystrix 一樣
@EnableHystrixDashboard      
package com.xhx.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
//@EnableCircuitBreaker  //與@EnableHystrix 一樣
@EnableHystrixDashboard
public class EurekaFeignApplication {

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

之前的Feign服務由于内置斷路器支援, 是以沒有

@EnableCircuitBreaker注解,但要使用Dashboard則必須加

,如果不加,Dashboard無法接收到來自Feign内部斷路器的監控資料,會報“Unable to connect to Command Metric Stream”錯誤

啟動各個微服務:

打開http://127.0.0.1:8085/hystrix這個網址:

SpringCloud(七)hystrix Dashboard

按照上圖輸入,改成自己的ip和端口,點選monitor stream

發送幾次請求

SpringCloud(七)hystrix Dashboard
SpringCloud(七)hystrix Dashboard

出現上圖所示,證明監控成功了

​​我的github位址​​