十、Hystrix Turbine

一、簡介

Netflix提供的一個開源項目(Turbine),把多個hystrix.stream的內容聚合爲一個數據源供Dashboard展示。Hystrix Turbine將每個服務Hystrix Dashboard數據進行了整合。

 

二、搭建 Hystrix Dashboard服務

<?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>mo-cloud</artifactId>
        <groupId>com.mo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>多個服務的Hystrix Dashboard</description>

    <artifactId>hystrix-dashboard-two</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-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>
    </dependencies>
</project>
server:
  # springboot設置隨機端口號
  port: 0
eureka:
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
  instance:
    # 重寫服務的唯一Id,防止多實例對被註冊成一個服務
    instance-id: ${spring.application.name}:${random.int}
    # 設置優先使用ip進行註冊
    prefer-ip-address: true
spring:
  application:
    name: hystrix-dashboard-two
# actuator 安全檢查開放消息推送刷新
management:
  endpoints:
    web:
      exposure:
        # 開放安全檢查的端點
        include: "*"
package com.mo;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * EnableHystrixDashboard註解,開啓HystrixDashboard,儀表盤監控服務
 * SpringCloudApplication註解,支持服務註冊,springboot啓動,hystrix熔斷
 *
 * @author x.pan
 * @email [email protected]
 * @date 2020/5/2 16:23
 */
@RestController
@SpringCloudApplication
@EnableHystrixDashboard
public class HystrixDashboardTwoApplication {

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


    @GetMapping("/hello")
    @HystrixCommand(fallbackMethod = "helloError")
    public String hello() {
        return "hello";
    }

    /**
     * HystrixCommand 熔斷的失敗返回方法
     *
     * @return
     */
    public String helloError() {
        return "hello error";
    }

    /**
     * 版本差異:添加actuator端點hystrix.stream
     * <p>
     * 訪問地址 http://localhost:8762/actuator/hystrix.stream
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registration.addUrlMappings("/actuator/hystrix.stream");
        return registration;
    }
}

備註hystrix-dashboard-twohystrix-dashboard都是Hystrix Dashboard服務。

 

三、搭建Hystrix Turbine服務

<?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>mo-cloud</artifactId>
        <groupId>com.mo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hystrix-turbine</artifactId>
    <description>Hystrix Turbine綜合Hystrix Dashboard數據</description>


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

        <!-- Hystrix Turbine依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>

</project>
server:
  # springboot設置隨機端口號
  port: 8764
eureka:
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
  instance:
    # 重寫服務的唯一Id,防止多實例對被註冊成一個服務
    instance-id: ${spring.application.name}:${random.int}
    # 設置優先使用ip進行註冊
    prefer-ip-address: true
spring:
  application:
    name: hystrix-turbine
# actuator 安全檢查開放消息推送刷新
management:
  endpoints:
    web:
      exposure:
        # 開放安全檢查的端點
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"
turbine:
  # turbine監控的服務
  app-config: hystrix-dashboard,hystrix-dashboard-two
  aggregator:
    # 指定聚合哪些集羣,默認default
    cluster-config: default
    # clusterNameExpression指定集羣名稱
  clusterNameExpression: new String("default")
  combine-host: true
  instanceUrlSuffix:
    default: actuator/hystrix.stream
package com.mo;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

/**
 * EnableTurbine,開啓turbine,綜合HystrixDashboard的數據
 *
 * @author x.pan
 * @email [email protected]
 * @date 2020/5/2 17:12
 */
@SpringCloudApplication
@EnableHystrixDashboard
@EnableTurbine
public class HystrixTurbine {
    public static void main(String[] args) {
        SpringApplication.run(HystrixTurbine.class, args);
    }
}

 

 四、Hystrix Turbine演示

1. 訪問 http://127.0.0.1:8764/turbine.stream

2. 訪問 http://127.0.0.1:8764/hystrix

3. 添加 http://127.0.0.1:8764/turbine.stream端點

4.  dashbord監控,訪問dashbord服務的接口

 

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