17,熔斷監控hystrix Dashboard和turbine

hystrix-dashboard是一款針對hystrix進行實時監控的工具,通過Hystrix Dashboard 我們可以在直觀地看到各Hystrix Command是請求響應時間,請求成功率等數據。但是隻使用hystrixDashboard的話,你只能看到單個應用內的服務信息,這明顯不夠,我們需要一個工具能讓我們彙總系統內部多個服務的數據並顯示到hystrix Dashboard上,這個工具就是Turbine.
hystrix Dasboard


我們在熔斷實例項目spring-cloud-consumer-hystrix的基礎上更改,重新命名爲:spring-cloud-consumer-hystrix-dashboard。
1、添加依賴

         <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>RELEASE</version>
        </dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    <version>1.3.0.RELEASE</version>
</dependency>
                                    
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

這三個包必須添加

2,啓動類

啓動類添加啓用Hystrix Dashboard和熔斷器

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ConsumerApplication {

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



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

3、測試

啓動工程後訪問 http://localhost:9001/hystrix,將會看到如下界面:

圖中會有一些提示:

Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream
Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
Single Hystrix App: http://hystrix-app:port/hystrix.stream

大概意思就是如果查看默認集羣使用第一個url,查看指定集羣使用第二個url,單個應用的監控使用最後一個,我們暫時只演示單個應用的所以在輸入框中輸入: http://localhost:9001/hystrix.stream ,輸入之後點擊 monitor,進入頁面。

如果沒有請求會先顯示Loading ...,訪問http://localhost:9001/hystrix.stream 也會不斷的顯示ping。

請求服務http://localhost:9001/hello/neo,就可以看到監控的效果了,首先訪問http://localhost:9001/hystrix.stream,顯示如下:

ping: 

data: {"type":...}

data: {"type":...}

說明已經返回了監控的各項結果

到監控頁面就會顯示如下圖:

其實就是http://localhost:9001/hystrix.stream返回結果的圖形化顯示,Hystrix Dashboard Wiki上詳細說明了圖上每個指標的含義,如下圖:

到此單個應用的熔斷監控已經完成。

Turbine

在複雜的分佈式系統中,相同服務的節點經常需要部署上百甚至上千,很多時候,運維人員希望你能夠把相同的服務節點狀態以一個整體集羣的形式表現出來,這樣可以更好地把握整個系統的狀態。爲此,Netflix提供了一個開源項目(Turbine)來提供把多個hystrix.stream 的內容聚合成爲一個數據源提供Dashboard展示

1、添加依賴

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.taotao</groupId>
    <artifactId>service-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-turbine</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
    </properties>

    <dependencies>
        <!--Turbine-->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-turbine -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
                <version>1.4.4.RELEASE</version>
        </dependency>

        <!--Hystrix Dashboard-->
        <dependency>
    <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.3.0.RELEASE</version>
        </dependency>
        <dependency>
    <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-turbine</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-hystrix-dashboard</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>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

2、配置文件

spring.application.name=service-turbine
server.port=8766
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka
turbine.aggregator.cluster-config=default
turbine.app-config=service-ribbon
turbine.cluster-name-expression="default"
turbine.combine-host-port=true

turbine.appConfig :配置Eureka中的serviceId列表,表明監控哪些服務
turbine.aggregator.clusterConfig :指定聚合哪些集羣,多個使用”,”分割,默認爲default。可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問
turbine.clusterNameExpression : 1. clusterNameExpression指定集羣名稱,默認表達式appName;此時:turbine.aggregator.clusterConfig需要配置想要監控的應用名稱;2. 當clusterNameExpression: default時,turbine.aggregator.clusterConfig可以不寫,因爲默認就是default;3. 當clusterNameExpression: metadata[‘cluster’]時,假設想要監控的應用配置了eureka.instance.metadata-map.cluster: ABC,則需要配置,同時turbine.aggregator.clusterConfig: ABC
3、啓動類

啓動類添加@EnableTurbine,激活對Turbine的支持

@EnableTurbine
@SpringBootApplication
public class ServiceTurbineApplication {

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



}

到此Turbine(hystrix-dashboard-turbine)配置完成

到此Turbine(hystrix-dashboard-turbine)配置完成

4、測試
在示例項目spring-cloud-consumer-hystrix基礎上修改爲兩個服務的調用者spring-cloud-consumer-node1和spring-cloud-consumer-node2

spring-cloud-consumer-node1項目改動如下: application.properties文件內容

spring.application.name=node01
server.port=9001
feign.hystrix.enabled=true

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

spring-cloud-consumer-node2項目改動如下: application.properties文件內容

spring.application.name=node02
server.port=9002
feign.hystrix.enabled=true

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

HelloRemote類修改:

@FeignClient(name= "spring-cloud-producer2", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {

    @RequestMapping(value = "/hello")
    public String hello2(@RequestParam(value = "name") String name);

}

對應的HelloRemoteHystrixConsumerController類跟隨修改,具體查看代碼

修改完畢後,依次啓動spring-cloud-eureka、spring-cloud-consumer-node1、spring-cloud-consumer-node1、hystrix-dashboard-turbine(Turbine)

打開eureka後臺可以看到註冊了三個服務:

[圖片上傳失敗...(image-e42495-1548076675425)]

訪問 http://localhost:8001/turbine.stream

返回:

: ping
data: {"reportingHostsLast10Seconds":1,"name":"meta","type":"meta","timestamp":1494921985839}

並且會不斷刷新以獲取實時的監控數據,說明和單個的監控類似,返回監控項目的信息。進行圖形化監控查看,輸入:http://localhost:8001/hystrix,返回酷酷的小熊界面,輸入: http://localhost:8001/turbine.stream,然後點擊 Monitor Stream ,可以看到出現了倆個監控列表

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