天天看點

spring cloud 基于2.X實作熔斷監控Hystrix Dashboard和Turbine

1. 熔斷監控

Hystrix-dashboard是一款針對Hystrix進行實時監控的工具,通過Hystrix Dashboard我們可以在直覺地看到各Hystrix Command的請求響應時間, 請求成功率等資料。但是隻使用Hystrix Dashboard的話, 你隻能看到單個應用内的服務資訊, 這明顯不夠. 我們需要一個工具能讓我們彙總系統内多個服務的資料并顯示到Hystrix Dashboard上, 這個工具就是Turbine.

2. Hystrix Dashboard

2.1 引入pom檔案

<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>
           

2.2 啟動類

# ---------添加@EnableHystrixDashboard  注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard 
public class CustomerApplication {

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

    /**
     * @author pengjw
     * @date 2019/3/19 20:32
     * @return org.springframework.boot.web.servlet.ServletRegistrationBean
     * spring boot 版本大于2.0就需要配置這個,否則就會報404
     */
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
           

2.3 測試

這個是在customer上改造的,是以啟動項目通路 位址:http://localhost:8003/hystrix,輸入http://localhost:8003/hystrix.stream,就會有圖形見面展示,但是這個是但服務監控的,是以我們需要Turbine群監控。

spring cloud 基于2.X實作熔斷監控Hystrix Dashboard和Turbine
spring cloud 基于2.X實作熔斷監控Hystrix Dashboard和Turbine

3. Turbine

建立turbine項目

3.1 引入依賴

<dependencies>
	<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>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
	</dependency>
</dependencies>
           

3.2 配置檔案

spring:
  application:
    name: hystrix-dashboard-turbine
server:
  port: 8004
turbine:
  app-config: spring-cloud-node1,spring-cloud-node2   #需要監控的服務
  aggregator:
    cluster-config: default
  cluster-name-expression: new String("default")
  instanceUrlSuffix: /hystrix.stream          #由于預設是/actuator/hystrix.stream,是以在此需要重新聲明一下
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/  #注冊中心
           

3.3 啟動類

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class TurbineApplication {

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

}
           

3.4 建立兩個執行個體

由于和customer差不多,是以就不貼代碼了,如果需要可以去github。

spring:
  application:
    name: spring-cloud-node1
server:
  port: 8005
feign:
  hystrix:
    enabled: true
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/
           
spring:
  application:
    name: spring-cloud-node2
server:
  port: 8006
feign:
  hystrix:
    enabled: true
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/在這裡插入代碼片
           

3.5 啟動類

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
public class Node1Application {

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


	/**
	 * @author pengjw
	 * @date 2019/3/23 16:45
	 * @return org.springframework.boot.web.servlet.ServletRegistrationBean
	 * 由于出現通路http://localhost:8004/turbine.stream 時出現404,配置完了就ok了
	 */
	@Bean
	public ServletRegistrationBean getServlet() {
		HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
		ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
		registrationBean.setLoadOnStartup(1);
		registrationBean.addUrlMappings("/hystrix.stream");
		registrationBean.setName("HystrixMetricsStreamServlet");
		return registrationBean;
	}
}
           

3.6 測試

修改完畢後,依次啟動eureka、node1、node2、turbine

spring cloud 基于2.X實作熔斷監控Hystrix Dashboard和Turbine

這說明已經啟動完成。

輸入位址:http://localhost:8004/hystrix,又會出現熟悉的小熊熊,通路下兩個服務 http://localhost:8005/hello/pipi、http://localhost:8006/hello/pipi 就會出現如下圖所示:

spring cloud 基于2.X實作熔斷監控Hystrix Dashboard和Turbine

spring-cloud系列之Turbine示例代碼-github

繼續閱讀