第十二篇 : 斷路器聚合監控(Hystrix Turbine)

第十二篇 : 斷路器聚合監控(Hystrix Turbine)

上一篇我們看了一個監控單體應用的例子,在實際應用中,我們要監控的應用往往是一個集羣,這個時候我們就得采取Turbine集羣監控了。Turbine有一個重要的功能就是匯聚監控信息,並將匯聚到的監控信息提供給Hystrix Dashboard來集中展示和監控。那我們就來看看Turbine集羣監控如何使用。

一、創建 service-oh

複製上一篇工程使用,在此基礎上進行改造。因爲我們需要多個服務的Dashboard,所以需要再建一個服務,取名爲service-oh,它的基本配置同service-hi,具體見源碼,在這裏就不詳細說明。

1. 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.gf</groupId>
    <artifactId>service-oh</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-oh</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.gf</groupId>
        <artifactId>chapter12</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</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-netflix-hystrix</artifactId>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2. application.yml

server:
  port: 8763

spring:
  application:
    name: service-oh
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

3. 主啓動類 ServiceOhApplication

package com.gf;
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.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
public class ServiceOhApplication {    public static void main(String[] args) {        SpringApplication.run(ServiceOhApplication.class, args);    }    @Value( "${server.port}" )    private String port;    @GetMapping("/oh")    @HystrixCommand(fallbackMethod = "ohError")    public String oh() {        return "oh , port is " + port;    }    public String ohError() {        return "oh , server error";    }

}

二、創建service-turbine

1. 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.gf</groupId>
    <artifactId>service-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-turbine</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.gf</groupId>
        <artifactId>chapter12</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2. application.yml

server:
  port: 8764

spring:
  application:
    name: service-turbine

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

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

#1.turbine.app-config=service-hi,service-oh指定了要監控的應用名字爲service-hi,service-oh
#2.turbine.cluster-name-expression="default",表示集羣的名字爲default
#3.turbine.combine-host-port=true表示同一主機上的服務通過host和port的組合來進行區分,默認情況下是使用host來區分,這樣會使本地調試有問題
turbine:
  app-config: service-hi,service-oh
  aggregator:
    cluster-config: default
  cluster-name-expression: new String("default")
  combine-host-port: true

3. 主啓動類 ServiceTurbineApplication

package com.gf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
@EnableTurbine
public class ServiceTurbineApplication {    /**     * http://localhost:8764/turbine.stream     */    public static void main(String[] args) {        SpringApplication.run(ServiceTurbineApplication.class, args);    }

}

三、Turbine演示

依次開啓eureka-server、service-hi、service-lucy、service-turbine工程。

依次請求:

http://localhost:8762/hi
http://localhost:8763/hi

打開:http://localhost:8763/hystrix 

輸入監控流http://localhost:8764/turbine.stream ,點擊Monitor Stream 進入頁面 

可以看到這個頁面聚合了2個service的hystrix dashbord數據。

源碼下載:https://github.com/gf-huanchupk/SpringCloudLearning


本文分享自微信公衆號 - 程序員果果(huanchuguofupk_gz)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章