(四)熔斷監(jiān)控Hystrix Dashboard和Turbine

1、Hystrix Dashboard

通過(guò)Hystrix Dashboard可以看到單個(gè)應(yīng)用內(nèi)的服務(wù)信息,本次使用到的應(yīng)用包括:

  • eureka-server:服務(wù)注冊(cè)中心
  • eureka-producer:服務(wù)提供者
  • eureka-consumer-feign-hystrix:使用 Feign 和 Hystrix 實(shí)現(xiàn)的服務(wù)消費(fèi)者

使用新的工程Hystrix Dashboard
(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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hystrix-dashboard</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>
    <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-openfeign</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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(2)application.yml

server:
  port: 8093
spring:
  application:
    name: hystrix-dashboard
eureka:
  server:
    enable-self-preservation: false #關(guān)閉eureka的自我保護(hù),防止已被關(guān)停的節(jié)點(diǎn)也錯(cuò)誤的顯示在線
  client:
    register-with-eureka: true #否允許客戶(hù)端向Eureka 注冊(cè)表獲取信息,服務(wù)器為設(shè)置為false,客戶(hù)端設(shè)置為true
    fetch-registry: true #是否允許向Eureka Server注冊(cè)信息 如果是服務(wù)器端,應(yīng)該設(shè)置為false
    service-url:
      defaultZone: http://peer1:8761/eureka/ #此eureka server的應(yīng)用注冊(cè)地址
feign:
  hystrix:
    enabled: true

(3)HystrixDashboardApplication.java

@SpringBootApplication
@EnableHystrixDashboard
@EnableEurekaClient
@EnableFeignClients //這個(gè)注解是通知SpringBoot在啟動(dòng)的時(shí)候,掃描被 @FeignClient 修飾的類(lèi)
public class HystrixDashboardApplication {

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

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

(4)Controller和遠(yuǎn)程調(diào)用文件

@RestController
public class HelloController {

    @Autowired
    private HelloRemote helloRemote;

    @RequestMapping("/hello")
    public String hello(@RequestParam("name") String name) {
        return helloRemote.hello(name);
    }

    @RequestMapping("/hello1")
    public String hello1(@RequestParam("name") String name) {
        return helloRemote.hello(name);
    }
}
@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteFallback.class)
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    String hello(@RequestParam(value = "name") String name);
}
@Component
public class HelloRemoteFallback implements HelloRemote {

    @Override
    public String hello(@RequestParam(value = "name") String name) {
        return "hello " + name + ",i am fallback message";
    }
}

啟動(dòng)eureka和hystrix-dashboard,訪問(wèn)http://localhost:8093/hystrix,在頁(yè)面中間長(zhǎng)文本框中輸入http://localhost:8093/hystrix.stream,點(diǎn)擊Monitor Stream,顯示Loading ...,再訪問(wèn)http://localhost:8093/hello?name=1,可以看到生成如下圖表

2、Turbine

引入Turbine來(lái)聚合Ribbon-consumer服務(wù)的監(jiān)控信息,并輸出給Hystrix Dashboard來(lái)進(jìn)行展示。
創(chuàng)建一個(gè)新的工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>turbine</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>
    <dependencies>
        <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-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>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(2)application.yml

server:
  port: 8094
spring:
  application:
    name: hystrix-dashboard-turbine
turbine:
  app-config: consumer-node1,consumer-node2 #配置Eureka中的serviceId列表,表明監(jiān)控哪些服務(wù)
  aggregator:
    cluster-config: default #指定聚合哪些集群,多個(gè)使用”,”分割,默認(rèn)為default
  cluster-name-expression: new String("default") #參數(shù)指定了集群名稱(chēng)為 default,當(dāng)我們服務(wù)數(shù)量非常多的時(shí)候,可以啟動(dòng)多個(gè) Turbine 服務(wù)來(lái)構(gòu)建不同的聚合集群,而該參數(shù)可以用來(lái)區(qū)分這些不同的聚合集群,同時(shí)該參數(shù)值可以在 Hystrix 儀表盤(pán)中用來(lái)定位不同的聚合集群,只需要在 Hystrix Stream 的 URL 中通過(guò) cluster 參數(shù)來(lái)指定
  combine-host-port: true #參數(shù)設(shè)置為true,可以讓同一主機(jī)上的服務(wù)通過(guò)主機(jī)名與端口號(hào)的組合來(lái)進(jìn)行區(qū)分,默認(rèn)情況下會(huì)以 host 來(lái)區(qū)分不同的服務(wù),這會(huì)使得在本地調(diào)試的時(shí)候,本機(jī)上的不同服務(wù)聚合成一個(gè)服務(wù)來(lái)統(tǒng)計(jì)
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8761/eureka/

(3)TurbineApplication.java啟動(dòng)類(lèi)

@SpringBootApplication
@EnableTurbine
@EnableHystrixDashboard
public class TurbineApplication {

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

}

創(chuàng)建消費(fèi)者集群,按consumers項(xiàng)目的內(nèi)容創(chuàng)建兩個(gè)項(xiàng)目consumer-node1,consumer-node2
修改對(duì)應(yīng)的應(yīng)用名

spring:
  application:
    name: consumer-node1

增加依賴(lài)包

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

增加啟動(dòng)類(lèi)注冊(cè)

@Bean
public ServletRegistrationBean getServlet(){
    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/actuator/hystrix.stream");
    registrationBean.setName("HystrixMetricsStreamServlet");
    return registrationBean;
}

依次啟動(dòng)注冊(cè)中心eureka,turbine和兩個(gè)消費(fèi)者,訪問(wèn)兩個(gè)消費(fèi)者,輸入http://localhost:8094/hystrix, 會(huì)返回小熊界面,輸入:http://localhost:8094/turbine.stream,然后點(diǎn)擊 Monitor Stream ,可以看到出現(xiàn)了倆個(gè)監(jiān)控列表

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容