天天看點

十四:Spring Cloud 之Hystrix Dashboard結合Turbine1. 簡介2. 代碼實作3. 驗證4. 思考5. 補充

文章目錄

  • 1. 簡介
  • 2. 代碼實作
    • 2.1 涉及的子產品及整體步驟
      • 2.1.1 涉及的子產品
      • 2.1.2 整體步驟
    • 2.2 源代碼
      • 2.2.1 Github位址
    • 2.3 eureka-server-singleton
    • 2.4 eureka-hystrix-dashboard
    • 2.4 eureka-hystrix-dashboard-another
      • 2.4.1 整體實作
      • 2.4.2 pom.xml
      • 2.4.3 application.yml
      • 2.4.4 bootstrap.yml
      • 2.4.5 EurekaHystrixDashboardAnotherApplication
    • 2.4 eureka-hystrix-dashboard-turbine
      • 2.4.1 整體實作
      • 2.4.2 pom.xml
      • 2.4.3 application.yml
      • 2.4.4 EurekaHystrixDashboardApplication
  • 3. 驗證
    • 3.1 建立SpringBoot啟動類
      • 3.1.1 EurekaServerSingletonApplication
      • 3.1.2 EurekaHystrixDashboardApplication
      • 3.1.2 EurekaHystrixDashboardAnotherApplication
      • 3.1.2 EurekaHystrixDashboardTurbineApplication
    • 3.2 啟動
    • 3.3 檢視Hystrix Dashboard
  • 4. 思考
  • 5. 補充
    • 5.1 資料

1. 簡介

Turbine (provided by the Spring Cloud Netflix project), aggregates multiple instances Hystrix metrics streams, so the dashboard can display an aggregate view. Turbine uses the DiscoveryClient interface to lookup relevant instances.
Hystrix是Netflix解決自己業務不穩定性的一個限流容錯架構,可以幫助我們解決微服務架構體系中的限流、降級、熔斷等功能。提高系統穩定性,提供了完善的監控實作,并且Hystrix可以根據監控資料動态調整内部處理機制。
Hystrix Dashboard提供了數字與圖形化的調用監控情況。結合Turbine可實作多Hystrix執行個體監控整合。

2. 代碼實作

2.1 涉及的子產品及整體步驟

2.1.1 涉及的子產品

  • eureka-server-singleton:微服務的服務注冊中心,限流熔斷針對的是服務調用異常的處理。
  • eureka-hystrix-dashboard
  • eureka-hystrix-dashboard-another
  • eureka-hystrix-dashboard-turbine:圖形化展示資料監控情況

2.1.2 整體步驟

  1. 實作eureka-server-singleton
  2. 實作eureka-hystrix-dashboard
  3. 實作eureka-hystrix-dashboard-another
  4. 實作eureka-hystrix-dashboard-turbine
  5. 調用eureka-hystrix-dashboard與eureka-hystrix-dashboard-another提供的服務接口
  6. 打開eureka-hystrix-dashboard-turbine的監控界面,輸入相關資訊,檢視調用情況

2.2 源代碼

2.2.1 Github位址

https://github.com/andyChenHuaYing/spring-cloud-demo

2.3 eureka-server-singleton

與 二:Spring Cloud 之Eureka服務釋出與注冊沒有任何差別。

2.4 eureka-hystrix-dashboard

與十三:Spring Cloud 之Hystrix Dashboard#2.4沒有任何差別

2.4 eureka-hystrix-dashboard-another

2.4.1 整體實作

  1. pom.xml:引入相關依賴,具體檢視下方具體代碼
  2. application.yml:指定端口,應用名,注冊中心通路位址資訊
  3. bootstrap.yml:指定公開SpringBoot的actuator監控端點為所有
  4. EurekaHystrixDashboardApplication:Spring boot啟動類,提供一個簡單的REST服務接口

2.4.2 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-hystrix-dashboard-another</artifactId>

    <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-eureka-client</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
    </dependencies>
</project>
                

2.4.3 application.yml

server:
  port: 8786

spring:
  application:
    name: eureka-hystrix-dashboard-another

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
                

2.4.4 bootstrap.yml

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

2.4.5 EurekaHystrixDashboardAnotherApplication

package org.oscar.scd.eureka.hystrix.dashboard.another;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableHystrix
@RestController
@EnableEurekaClient
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrixDashboard
public class EurekaHystrixDashboardAnotherApplication {

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

    @Value("${spring.application.name}")
    private String appName;

    @RequestMapping("/appName")
    @HystrixCommand(fallbackMethod = "errorMsg")
    public String getAppName() {
        return this.appName;
    }

    @SuppressWarnings("unused")
    public String errorMsg() {
        return "Error msg by hystrix.";
    }
}

                

2.4 eureka-hystrix-dashboard-turbine

2.4.1 整體實作

  1. pom.xml:引入相關依賴,具體檢視下方具體代碼
  2. application.yml:指定端口,應用名,注冊中心通路位址資訊
  3. EurekaHystrixDashboardTurbineApplication:Spring boot啟動類,使用注解開啟Hystrix Turbine監控

2.4.2 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-hystrix-dashboard-turbine</artifactId>

    <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-eureka-client</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>
</project>
                

2.4.3 application.yml

server:
  port: 8787
spring:
  application:
    name: eureka-hystrix-dashboard-turbine
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

turbine:
  app-config: eureka-hystrix-dashboard-another,eureka-hystrix-dashboard
  aggregator:
    clusterConfig: default
  clusterNameExpression: new String("default")
  combine-host: true
  instanceUrlSuffix:
    default: actuator/hystrix.stream


                

2.4.4 EurekaHystrixDashboardApplication

package org.oscar.scd.eureka.hystrix.dashboard.turbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import org.springframework.web.bind.annotation.RestController;

@EnableHystrix
@EnableTurbine
@RestController
@EnableEurekaClient
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrixDashboard
public class EurekaHystrixDashboardTurbineApplication {

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

}

                

3. 驗證

3.1 建立SpringBoot啟動類

最簡單的方式添加一個SpringBoot啟動類型的啟動類就行,如果對SpringBoot有所了解或者看過前面系列的文章,這裡很簡單。
           

3.1.1 EurekaServerSingletonApplication

3.1.2 EurekaHystrixDashboardApplication

3.1.2 EurekaHystrixDashboardAnotherApplication

3.1.2 EurekaHystrixDashboardTurbineApplication

3.2 啟動

  1. EurekaServerSingletonApplication

  2. EurekaHystrixDashboardApplication

  3. EurekaHystrixDashboardAnotherApplication

  4. EurekaHystrixDashboardTurbineApplication

3.3 檢視Hystrix Dashboard

  1. 通路:http://localhost:8785/hystrix
    十四:Spring Cloud 之Hystrix Dashboard結合Turbine1. 簡介2. 代碼實作3. 驗證4. 思考5. 補充
  2. 通路:http://localhost:8785/appName調用服務
eureka-hystrix-dashboard
  1. 通路:http://localhost:8786/appName調用服務
eureka-hystrix-dashboard-another
  1. 通路:http://localhost:8787/turbine.stream 檢視Turbine聚合的監控資訊
  2. 通路http://localhost:8787/hystrixHystrix界面前兩個輸入框依次輸入:

    http://localhost:8787/turbine.stream

    2000

    。注意是8787端口的turbine.stream,如果沒有資料,多調用幾次服務端口
    十四:Spring Cloud 之Hystrix Dashboard結合Turbine1. 簡介2. 代碼實作3. 驗證4. 思考5. 補充

4. 思考

  • 如何與調用鍊監控整合,也就是如何搭建一個完整的監控平台
  • 現在這個要輸入查詢條件,如何定制化首界面和展示項,更接近于實用和生産
  • 監控項或者監控資訊是否可定制化,Spring Boot的actuator如何使用的,具體有哪些endpoint。

5. 補充

5.1 資料

九:對微服務限流容錯的了解

十一:對微服務調用鍊監控的了解

十:對微服務監控系統分層和監控架構的了解

https://github.com/Netflix/hystrix

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-consul-turbine.html

繼續閱讀