Spring Cloud熔断监控Hystrix Dashboard和Turbine

Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间、请求成功率等数据。但是只使用Hystrix Dashboard只能看到单个应用内的服务信息,我们需要一个工具能够汇总系统内多个服务的数据并显示到Hystrix Dashboard上,这个工具就是Turbine

Hystrix Dashboard

添加依赖

在spring-cloud-consumer项目的pom.xml中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

启动类

启动类添加启用Hystrix Dashboard和熔断器

1
2
3
4
5
6
7
8
9
10
11
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ConsumerApplication {

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

###测试

启动工程后访问 http://localhost:9001/hystrix ,将会看到如下界面:

hystrix-dashboard

图中会有一些提示:

1
2
3
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。

注:在Spring Cloud 2.0后要解决访问/hystrix.stream 404的问题,要在application.properties中加上

1
2
management.endpoints.web.exposure.include=hystrix.stream
management.endpoints.web.base-path=/

请求服务http://localhost:9001/hello/Joe.Ye,即可看到监控的效果,访问http://localhost:9001/hystrix.stream,显示如下:

1
2
3
4
5
ping: 

data: {"type":...}

data: {"type":...}

说明已经返回了监控的各项结果

到监控页面就会显示如下图:

hystrix-dashboard

其实就是http://localhost:9001/hystrix.stream返回结果的图形化显示,Hystrix Dashboard Wiki上详细说明了图上每个指标的含义,如下图:

hystrix-dashboard

到此单个应用的熔断监控已经完成。

Turbine

在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开源项目(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。

新建module: hystrix-dashboard-turbine

添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<dependencies>
<!-- 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.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-netflix-turbine -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

配置文件

1
2
3
4
5
6
7
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的支持

1
2
3
4
5
6
7
8
9
10
@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class DashboardApplication {

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

}

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

测试

先有单点监控,再有集群监控,因此首先创建服务调用者node1和node2的Hystrix Dashboard

新增两个服务的调用者spring-cloud-consumer-node1和spring-cloud-consumer-node2

spring-cloud-consumer-node1

(1)pom.xml依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.6.RELEASE</version>
</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>
<!--https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

(2)application.properties配置

1
2
3
4
5
6
7
spring.application.name=node01
server.port=9001
feign.hystrix.enabled=true
management.endpoints.web.exposure.include=hystrix.stream
management.endpoints.web.base-path=/

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

(3)启动

1
2
3
4
5
6
7
8
9
10
11
12
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ConsumerNode1Application {

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

}

(4)消费接口及Controller

1
2
3
4
5
6
@FeignClient(name= "spring-cloud-producer", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {

@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name);
}
1
2
3
4
5
6
7
8
@Component
public class HelloRemoteHystrix implements HelloRemote {

@Override
public String hello(String name) {
return "Hello " + name + ", This message send failed!";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class ConsumerController {

@Autowired
HelloRemote helloRemote;

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

}

spring-cloud-consumer-node2

(1)pom.xml依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.6.RELEASE</version>
</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>
<!--https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

(2)application.properties配置

1
2
3
4
5
6
7
spring.application.name=node02
server.port=9002
feign.hystrix.enabled=true
management.endpoints.web.exposure.include=hystrix.stream
management.endpoints.web.base-path=/

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

(3)启动

1
2
3
4
5
6
7
8
9
10
11
12
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ConsumerNode2Application {

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

}

(4)消费接口及Controller

1
2
3
4
5
6
@FeignClient(name= "spring-cloud-producer2", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {

@RequestMapping(value = "/hello")
public String hello2(@RequestParam(value = "name") String name);
}
1
2
3
4
5
6
7
8
@Component
public class HelloRemoteHystrix implements HelloRemote {

@Override
public String hello2(String name) {
return "Hello " + name + ", This message send failed!";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class ConsumerController {

@Autowired
HelloRemote helloRemote;

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

}

启动调用测试

(1)依次启动spring-cloud-eurekaspring-cloud-consumer-node1spring-cloud-consumer-node2hystrix-dashboard-turbine(Turbine),打开eureka后台可以看到注册了三个服务:

turbine

(2)访问:http://localhost:8001/turbine.stream

返回:

1
2
3
4
5
6
7
: ping
data: {"rollingCountFallbackSuccess":2,"rollingCountFallbackFailure":0,"propertyValue_circuitBreakerRequestVolumeThreshold":40,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":20000,"latencyTotal_mean":0,"rollingMaxConcurrentExecutionCount":2,"type":"HystrixCommand","rollingCountResponsesFromCache":0,"rollingCountBadRequests":0,"rollingCountTimeout":0,"propertyValue_executionIsolationStrategy":"THREAD","rollingCountFailure":2,"rollingCountExceptionsThrown":0,"rollingCountFallbackMissing":0,"threadPool":"spring-cloud-producer2","latencyExecute_mean":0,"isCircuitBreakerOpen":false,"errorCount":2,"rollingCountSemaphoreRejected":0,"group":"spring-cloud-producer2","latencyTotal":{"0":0,"99":0,"100":0,"25":0,"90":0,"50":0,"95":0,"99.5":0,"75":0},"requestCount":2,"rollingCountCollapsedRequests":0,"rollingCountShortCircuited":0,"propertyValue_circuitBreakerSleepWindowInMilliseconds":10000,"latencyExecute":{"0":0,"99":0,"100":0,"25":0,"90":0,"50":0,"95":0,"99.5":0,"75":0},"rollingCountEmit":0,"currentConcurrentExecutionCount":0,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":20,"errorPercentage":200,"rollingCountThreadPoolRejected":0,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_requestCacheEnabled":true,"rollingCountFallbackRejection":0,"propertyValue_requestLogEnabled":true,"rollingCountFallbackEmit":0,"rollingCountSuccess":0,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":20,"propertyValue_circuitBreakerErrorThresholdPercentage":100,"propertyValue_circuitBreakerForceClosed":false,"name":"HelloRemote#hello2(String)","reportingHosts":2,"propertyValue_executionIsolationThreadPoolKeyOverride":"null","propertyValue_executionIsolationThreadTimeoutInMilliseconds":2000,"propertyValue_executionTimeoutInMilliseconds":2000}

data: {"currentCorePoolSize":20,"currentLargestPoolSize":6,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":20000,"currentActiveCount":0,"currentMaximumPoolSize":20,"currentQueueSize":0,"type":"HystrixThreadPool","currentTaskCount":6,"currentCompletedTaskCount":6,"rollingMaxActiveThreads":2,"rollingCountCommandRejections":0,"name":"spring-cloud-producer2","reportingHosts":2,"currentPoolSize":6,"propertyValue_queueSizeRejectionThreshold":10,"rollingCountThreadsExecuted":2}

: ping
data: {"reportingHostsLast10Seconds":4,"name":"meta","type":"meta","timestamp":1553394455337}

(3)访问:http://localhost:9001/hystrix.streamhttp://localhost:9002/hystrix.stream

返回:

1
2
3
4
5
ping: 

data: {"type":"HystrixCommand","name":"HelloRemote#hello(String)","group":"spring-cloud-producer","currentTime":1553394278182,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountBadRequests":0,"rollingCountCollapsedRequests":0,"rollingCountEmit":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackEmit":0,"rollingCountFallbackFailure":0,"rollingCountFallbackMissing":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"rollingMaxConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":1000,"propertyValue_executionTimeoutInMilliseconds":1000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1,"threadPool":"spring-cloud-producer"}

data: {"type":"HystrixThreadPool","name":"spring-cloud-producer","currentTime":1553394278182,"currentActiveCount":0,"currentCompletedTaskCount":2,"currentCorePoolSize":10,"currentLargestPoolSize":2,"currentMaximumPoolSize":10,"currentPoolSize":2,"currentQueueSize":0,"currentTaskCount":2,"rollingCountThreadsExecuted":0,"rollingMaxActiveThreads":0,"rollingCountCommandRejections":0,"propertyValue_queueSizeRejectionThreshold":5,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"reportingHosts":1}

并且会不断刷新以获取实时的监控数据,说明和单个的监控类似,返回监控项目的信息。进行图形化监控查看,输入:http://localhost:8001/hystrix ,返回酷酷的小熊界面,输入:http://localhost:8001/turbine.stream ,然后点击 Monitor Stream,可以看到出现了俩个监控列表

turbine

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :