springCloud Finchley 實(shí)戰(zhàn)入門(基于springBoot 2.0.3)【六 Hystrix 儀表盤】

Hystrix儀表盤

通過上一篇我們已經(jīng)成功的實(shí)現(xiàn)了spring cloud對Hystrix的整合了。除此之外,spring cloud還完美的整合了Hystrix的儀表盤組件Hystrix Dashboard。該組件主要是用來實(shí)時監(jiān)控Hystrix的各項(xiàng)指示信息的。通過Hystrix Dashboard反饋的信息,可以幫助我們快速的發(fā)現(xiàn)系統(tǒng)中存在的問題,從而即使采取應(yīng)對方法。

快速入門(單實(shí)例監(jiān)控)

先演示針對單個應(yīng)用的監(jiān)控

創(chuàng)建一個module命名為"eureka-hystrix-dashboard",創(chuàng)建時選擇對應(yīng)的組件


1532502505125.png

創(chuàng)建成功后,我們還需要在pom.xml中添加actuator的依賴:

<?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.example</groupId>
    <artifactId>eureka-hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-hystrix-dashboard</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

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

application.yml配置:

spring:
  application:
    name: hystrix-dashboard
server:
  port: 2001
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka/

在主類"EurekaHystrixDashboardApplication",添加@EnableHystrixDashboard注解,表示開啟Hystrix儀表盤監(jiān)控

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard
public class EurekaHystrixDashboardApplication {

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

配置完成啟動項(xiàng)目后,訪問地址:http://localhost:2001/hystrix
如果看也下面的頁面就說明項(xiàng)目正常的:

1532502951844.png

通過 Hystrix Dashboard 主頁面的文字介紹,我們可以知道,Hystrix Dashboard 共支持三種不同的監(jiān)控方式:

默認(rèn)的集群監(jiān)控:通過 URL:http://turbine-hostname:port/turbine.stream 開啟,實(shí)現(xiàn)對默認(rèn)集群的監(jiān)控。
指定的集群監(jiān)控:通過 URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName] 開啟,實(shí)現(xiàn)對 clusterName 集群的監(jiān)控。
單體應(yīng)用的監(jiān)控: 通過 URL:http://hystrix-app:port/hystrix.stream 開啟 ,實(shí)現(xiàn)對具體某個服務(wù)實(shí)例的監(jiān)控。(現(xiàn)在這里的 URL 應(yīng)該為 http://hystrix-app:port/actuator/hystrix.stream,Actuator 2.x 以后endpoints 全部在/actuator下,可以通過management.endpoints.web.base-path修改)。

前兩者都對集群的監(jiān)控,需要整合 Turbine 才能實(shí)現(xiàn)。

因?yàn)槲覀兡壳跋茸鰧?shí)現(xiàn)對單體應(yīng)用的監(jiān)控,所以這里的單體應(yīng)用就用使用 Hystrix 實(shí)現(xiàn)的服務(wù)提供者"eureka-bussniss-service-user-client-ribbon"

頁面上的另外兩個參數(shù):
Delay:控制服務(wù)器上輪詢監(jiān)控信息的延遲時間,默認(rèn)為 2000 毫秒,可以通過配置該屬性來降低客戶端的網(wǎng)絡(luò)和 CPU 消耗。
Title:該參數(shù)可以展示合適的標(biāo)題。

配置服務(wù)提供者

Hystrix Dashboard 監(jiān)控單實(shí)例節(jié)點(diǎn)需要通過訪問實(shí)例的/actuator/hystrix.stream接口來實(shí)現(xiàn),所以我們需要為服務(wù)提供者做一下配置。 在"eureka-bussniss-service-user-client-ribbon"項(xiàng)目的pom文件添加autuator和hystrix的依賴。

 <!--添加Hystrix依賴 斷路器容錯保護(hù)-->
 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
 </dependency>
 <!--監(jiān)控中心-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>

修改application.yml配置
添加management.endpoints.web.exposure.include=hystrix.stream屬性,表示暴露hystrix.stream的監(jiān)控點(diǎn)地址。

spring:
  application:
    name: service-user-ribbon
server:
  port: 8901
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka/
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

接著在主類名"EurekaBussnissServiceUserClientRibbonApplication"添加@EnableCircuitBreaker 表示啟動Hystrix儀表盤 服務(wù)熔斷(ribbon 單服務(wù)實(shí)例監(jiān)控)

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableCircuitBreaker //Hystrix儀表盤 服務(wù)熔斷(ribbon 單服務(wù)實(shí)例監(jiān)控)
public class EurekaBussnissServiceUserClientRibbonApplication {

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

    @Bean
    @LoadBalanced //開啟客戶端負(fù)載均衡
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

重啟"eureka-bussniss-service-user-client-ribbon"項(xiàng)目。


1532504332432.png

在2001項(xiàng)目頁面中輸入"http://localhost:8901/actuator/hystrix.stream",點(diǎn)擊"Monitor Stream ",注意:(spring boot 2.x)監(jiān)控地址需要加上actuator的路徑??吹较聢D,顯示loding...

1532504462251.png

一直顯示loding...這是因?yàn)檫€沒有檢測到請求,我們訪問一下地址:http://localhost:8901/listUsersByRibbon,然后就可以看到下面的信息了。

1532504537311.png

頁面上面的信息就是顯示服務(wù)的具體情況,具體的信息可以去spring cloud的官網(wǎng)深入了解。這里就不過多解釋了。
到這里我們就已經(jīng)實(shí)現(xiàn)了Hystrix dashboard對單個應(yīng)用的儀表監(jiān)控了。

快速入門 (集群監(jiān)控)

創(chuàng)建一個新的module命名為"eureka-hystrix-dashboard-turbine"。創(chuàng)建時選擇對應(yīng)的組件依賴


1532505439252.png

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.example</groupId>
    <artifactId>eureka-hystrix-dashboard-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-hystrix-dashboard-turbine</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

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

application.yml配置如下:

spring:
  application:
    name: turbine
server:
  port: 8989
management:
  server:
    port: 8990
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka/
turbine:
  appConfig: service-user-ribbon
  clusterNameExpression: new String("default")
  combineHostPort: true

turbine.appConfig表示監(jiān)控的服務(wù)實(shí)例(通過serviceId;多個服務(wù)實(shí)例以逗號分隔)

項(xiàng)目的主類名EurekaHystrixDashboardTurbineApplication添加@EnableTurbine注解

@SpringBootApplication
@EnableTurbine
@EnableEurekaClient
public class EurekaHystrixDashboardTurbineApplication {

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

然后service-user項(xiàng)目pom添加hystrix依賴以及主類添加@EnableHystrix@EnableCircuitBreaker注解。

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableCircuitBreaker //Hystrix儀表盤 服務(wù)熔斷(ribbon 單服務(wù)實(shí)例監(jiān)控)
public class EurekaBussnissServiceUserApplication {

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

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.example</groupId>
    <artifactId>eureka-bussniss-service-user</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-bussniss-service-user</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <!--添加Hystrix依賴 斷路器容錯保護(hù)-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</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>

重啟相關(guān)的服務(wù)。同樣是在2001頁面輸入對應(yīng)的地址http://localhost:8989/turbine.stream 點(diǎn)擊Monitor Stream 我們也同樣是可以實(shí)現(xiàn)對服務(wù)實(shí)例的監(jiān)控。

1532507114614.png

1532507184545.png

如果aplication.yml中的appConfig是配置了多個的,就會對顯示相應(yīng)的儀表盤數(shù)據(jù)。

github 項(xiàng)目源碼

到這里Hystrix儀表盤監(jiān)控服務(wù)實(shí)例就基本實(shí)現(xiàn)了。接下來我們會用Fegin組件以聲明式服務(wù)調(diào)用的方式實(shí)現(xiàn)Ribbon+Hystrix(負(fù)載均衡+服務(wù)容錯保護(hù))的功能。

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

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

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