hystrix-dashboard是一款針對(duì)hystrix進(jìn)行實(shí)時(shí)監(jiān)控的工具基公,通過(guò)Hystrix Dashboard 我們可以在直觀地看到各Hystrix Command是請(qǐng)求響應(yīng)時(shí)間琴庵,請(qǐng)求成功率等數(shù)據(jù)伤溉。但是只使用hystrixDashboard的話辫秧,你只能看到單個(gè)應(yīng)用內(nèi)的服務(wù)信息束倍,這明顯不夠,我們需要一個(gè)工具能讓我們匯總系統(tǒng)內(nèi)部多個(gè)服務(wù)的數(shù)據(jù)并顯示到hystrix Dashboard上盟戏,這個(gè)工具就是Turbine.
hystrix Dasboard
我們?cè)谌蹟鄬?shí)例項(xiàng)目spring-cloud-consumer-hystrix的基礎(chǔ)上更改绪妹,重新命名為: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>
這三個(gè)包必須添加
2邮旷,啟動(dòng)類(lèi)
啟動(dòng)類(lèi)添加啟用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、測(cè)試
啟動(dòng)工程后訪問(wèn) http://localhost:9001/hystrix蝇摸,將會(huì)看到如下界面:
圖中會(huì)有一些提示:
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
大概意思就是如果查看默認(rèn)集群使用第一個(gè)url,查看指定集群使用第二個(gè)url,單個(gè)應(yīng)用的監(jiān)控使用最后一個(gè)婶肩,我們暫時(shí)只演示單個(gè)應(yīng)用的所以在輸入框中輸入: http://localhost:9001/hystrix.stream 办陷,輸入之后點(diǎn)擊 monitor,進(jìn)入頁(yè)面律歼。
如果沒(méi)有請(qǐng)求會(huì)先顯示Loading ...
民镜,訪問(wèn)http://localhost:9001/hystrix.stream 也會(huì)不斷的顯示ping。
請(qǐng)求服務(wù)http://localhost:9001/hello/neo险毁,就可以看到監(jiān)控的效果了殃恒,首先訪問(wèn)http://localhost:9001/hystrix.stream,顯示如下:
ping:
data: {"type":...}
data: {"type":...}
說(shuō)明已經(jīng)返回了監(jiān)控的各項(xiàng)結(jié)果
到監(jiān)控頁(yè)面就會(huì)顯示如下圖:
其實(shí)就是http://localhost:9001/hystrix.stream返回結(jié)果的圖形化顯示辱揭,Hystrix Dashboard Wiki上詳細(xì)說(shuō)明了圖上每個(gè)指標(biāo)的含義离唐,如下圖:
到此單個(gè)應(yīng)用的熔斷監(jiān)控已經(jīng)完成。
Turbine
在復(fù)雜的分布式系統(tǒng)中问窃,相同服務(wù)的節(jié)點(diǎn)經(jīng)常需要部署上百甚至上千亥鬓,很多時(shí)候,運(yùn)維人員希望你能夠把相同的服務(wù)節(jié)點(diǎn)狀態(tài)以一個(gè)整體集群的形式表現(xiàn)出來(lái)域庇,這樣可以更好地把握整個(gè)系統(tǒng)的狀態(tài)嵌戈。為此,Netflix提供了一個(gè)開(kāi)源項(xiàng)目(Turbine)來(lái)提供把多個(gè)hystrix.stream 的內(nèi)容聚合成為一個(gè)數(shù)據(jù)源提供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列表,表明監(jiān)控哪些服務(wù)
turbine.aggregator.clusterConfig :指定聚合哪些集群尉姨,多個(gè)使用”,”分割庵朝,默認(rèn)為default∮掷鳎可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問(wèn)
turbine.clusterNameExpression : 1. clusterNameExpression指定集群名稱九府,默認(rèn)表達(dá)式appName;此時(shí):turbine.aggregator.clusterConfig需要配置想要監(jiān)控的應(yīng)用名稱覆致;2. 當(dāng)clusterNameExpression: default時(shí)侄旬,turbine.aggregator.clusterConfig可以不寫(xiě),因?yàn)槟J(rèn)就是default煌妈;3. 當(dāng)clusterNameExpression: metadata[‘cluster’]時(shí)儡羔,假設(shè)想要監(jiān)控的應(yīng)用配置了eureka.instance.metadata-map.cluster: ABC,則需要配置璧诵,同時(shí)turbine.aggregator.clusterConfig: ABC
3益眉、啟動(dòng)類(lèi)
啟動(dòng)類(lèi)添加@EnableTurbine贮缕,激活對(duì)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畏浆、測(cè)試
在示例項(xiàng)目spring-cloud-consumer-hystrix基礎(chǔ)上修改為兩個(gè)服務(wù)的調(diào)用者spring-cloud-consumer-node1和spring-cloud-consumer-node2
spring-cloud-consumer-node1項(xiàng)目改動(dòng)如下: application.properties文件內(nèi)容
spring.application.name=node01
server.port=9001
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
spring-cloud-consumer-node2項(xiàng)目改動(dòng)如下: application.properties文件內(nèi)容
spring.application.name=node02
server.port=9002
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
HelloRemote類(lèi)修改:
@FeignClient(name= "spring-cloud-producer2", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {
@RequestMapping(value = "/hello")
public String hello2(@RequestParam(value = "name") String name);
}
對(duì)應(yīng)的HelloRemoteHystrix
和ConsumerController
類(lèi)跟隨修改身害,具體查看代碼
修改完畢后赞枕,依次啟動(dòng)spring-cloud-eureka澈缺、spring-cloud-consumer-node1坪创、spring-cloud-consumer-node1、hystrix-dashboard-turbine(Turbine)
打開(kāi)eureka后臺(tái)可以看到注冊(cè)了三個(gè)服務(wù):
[圖片上傳失敗...(image-e42495-1548076675425)]
訪問(wèn) http://localhost:8001/turbine.stream
返回:
: ping
data: {"reportingHostsLast10Seconds":1,"name":"meta","type":"meta","timestamp":1494921985839}
并且會(huì)不斷刷新以獲取實(shí)時(shí)的監(jiān)控?cái)?shù)據(jù)姐赡,說(shuō)明和單個(gè)的監(jiān)控類(lèi)似莱预,返回監(jiān)控項(xiàng)目的信息。進(jìn)行圖形化監(jiān)控查看项滑,輸入:http://localhost:8001/hystrix依沮,返回酷酷的小熊界面,輸入: http://localhost:8001/turbine.stream枪狂,然后點(diǎn)擊 Monitor Stream ,可以看到出現(xiàn)了倆個(gè)監(jiān)控列表