springcloud(五):熔斷監控Hystrix Dashboard和Turbine

Hystrix Dashboard

咱們在熔斷示例項目spring-cloud-consumer-hystrix的基礎上更改,從新命名爲:spring-cloud-consumer-hystrix-dashboard。java

一、添加依賴

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix</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-actuator</artifactId>
</dependency>

這三個包必須添加node

二、啓動類

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

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

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

Turbine

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

一、添加依賴

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

二、配置文件

spring.application.name=hystrix-dashboard-turbine
server.port=8001
turbine.appConfig=node01,node02
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  • 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

三、啓動類

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

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class DashboardApplication {

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

}

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

四、測試

在示例項目spring-cloud-consumer-hystrix基礎上修改成兩個服務的調用者spring-cloud-consumer-node1和spring-cloud-consumer-node2分佈式

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

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類修改:ui

@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-node一、spring-cloud-consumer-node一、hystrix-dashboard-turbine(Turbine)

打開eureka後臺能夠看到註冊了三個服務:

訪問 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 ,能夠看到出現了倆個監控列表

願意瞭解框架技術或者源碼的朋友直接求求交流分享技術:貳一四七七七五六叄叄

相關文章
相關標籤/搜索