Hystrix除了自带的熔断功能之外,还提供了一个实时监控的Hystrix Dashboard,这个服务会展现Hystrix代理的服务实时的metrics度量。

但是原生的Hystrix Dashboard依赖web的环境,而在我们一些场景下项目本身是没有web容器的,还好Spring Cloud提供了解决方案Spring Cloud Stream,让我们可以通过中间件将数据聚合展现到Hystrix Dashboard上面。
这里我采用RabbitMQ作为消息中间件传递Metric信息,在此之前Spring Cloud需要使用Netflix的Eureka来做服务发现(当然这个也是可选的)。
添加Hystrix
Spring Cloud Stream是数据流操作开发包,封装了与Redis,Rabbit,Kafka等发送接收消息的功能。
在我们普通服务client一端的pom.xml文件中引入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
在client一端的application.properties的设置如下:
spring.application.name=web-demo
server.port=9999
spring.rabbitmq.addresses=192.168.1.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=test
spring.rabbitmq.password=password
spring.rabbitmq.virtualHost=/test
spring.cloud.stream.rabbit.bindings.test.consumer.prefix=z
spring.cloud.stream.bindings.input.group=default
创建一个新的Controller,并将之前写好的一个Hystrix类CommandThatFailsFast放入其中:
@RestController
@RequestMapping("/main")
public class MainController {
Logger logger = LoggerFactory.getLogger(MainController.class);
private Random random = new Random();
@RequestMapping("index")
public String hello() {
// 用随机数来模拟错误, 这里让正确率高一些
return new CommandThatFailsFast(random.nextInt(100) < 95).execute();
}
}
这样就可以通过暴露的web接口来简单模拟,服务的请求了。
Turbine And HystrixDashboard
在另外一端,我们需要一个接收这些metrics,并汇聚展示的Dashboard项目。
这个项目中引入两个模块:turbine和hystrix-dashboard(也可以分成个服务)。
其中turbine是聚合metrics数据的组件,而hystrix-dashboard就是我们希望看到的展示数据组件了。
在pom.xml中引入dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
然后在配置(resource下的application.properties)中写入:
spring.application.name=hystrix-dashboard
eureka.client.service-url.defaultZone=http://localhost:8081/eureka/
turbine.cluster-name-expression=SAMPLE-HYSTRIX-AGGREGATE
turbine.app-config=SAMPLE-HYSTRIX-AGGREGATE
spring.rabbitmq.addresses=192.168.1.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=test
spring.rabbitmq.password=password
spring.rabbitmq.virtualHost=/test
spring.cloud.stream.rabbit.bindings.test.consumer.prefix=z
spring.cloud.stream.bindings.input.group=default
# 这是turbine的端口即暴露监控数据的端口,跟server.port不同
turbine.stream.port=8888
# 这个是服务的端口
server.port=8880
server端的Application程序代码如下:
@SpringBootApplication
@EnableTurbineStream
@EnableHystrixDashboard
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder().sources(Application.class).run(args);
}
}
Dashboard数据展现
在打开这几个服务之前先启动rabbitmq:
➜ ~ rabbitmq-server
RabbitMQ 3.5.6. Copyright (C) 2007-2015 Pivotal Software, Inc.
## ## Licensed under the MPL. See http://www.rabbitmq.com/
## ##
########## Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log
###### ## /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log
##########
Starting broker... completed with 10 plugins.
打开地址http://127.0.0.1:8880/hystrix.stream。

在Hystrix Dashboard的文字下方输入turbine服务的地址http://127.0.0.1:8880,点击Monitor Stream按钮,就能进入到监控界面。

我们可以使用wrk测试之前定义好的web接口:
➜ ~ wrk -t 10 -c 10 -d 5s http://127.0.0.1:9999/main/index
Running 5s test @ http://127.0.0.1:9999/main/index
10 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 17.01ms 31.95ms 321.80ms 89.54%
Req/Sec 163.63 107.89 554.00 70.39%
8049 requests in 5.09s, 1.47MB read
Requests/sec: 1580.03
Transfer/sec: 294.67KB
这个时候能明显地看到监控界面的变化:

通过上面的这些步骤,我们就可以在没有web容器的项目中使用Hystrix来发送监控数据到Hystrix Dashboard上了。