Spring Cloud 整合了仪表盘组件Hystrix Dashboard,它主要用来实时监控Hystrix 的各项指标信息。通过Hystrix Bashboard反馈的实时信息,可以帮助我们快速发现系统中存在的问题,从而及时地采取应对措施。
- Java 8
- SpringBoot 2.2.10
- SpringCloud Hoxton.SR8
- RabbitMQ 3.8.9
1. 构建一个Hyxtrix Dashboard来对服务实现监控。
- 创建一个标准的Spring Boot 工程,命名为 hystrix-dashboard。
- 编辑pom.xml,具体依赖内容如下:
<dependencies>
<!-- 添加springcloud eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 添加springboot actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 添加 hystrix dashboard-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
- 为应用添加主类 HystrixDashboardApplication
/**
* 开启服务发现客户端
*/
@EnableDiscoveryClient
/**
* 开启监控
*/
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
- application.yml 添加配置信息
server:
port: 2001
spring:
application:
name: hystrix-dashboard
eureka:
client:
service-url:
#注册地址
defaultZone: http://root:root@localhost:8082/eureka/
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
hystrix:
dashboard:
proxy-stream-allow-list: "localhost"
启动服务,浏览器请求访问:http://localhost:2001/hystrix,可以看到Hystrix Dashboard监控界面。
Hystrix Dashboard支持三种不同的架空方式,
- 默认的集群监控:https://turbine-hostname:port/turbine.stream
- 指定的集群监控:https://turbine-hostname:port/turbine.stream?cluster=[clusterName]
- 单体应用的监控:https://hystrix-app:port/actuator/hystrix.stream
2. Turbine 集群监控(与RabbitMQ 集成)
引入Turbie聚合服务的监控信息,可以将收集的监控信息输出到RabbitMQ消息代理上,然后Turbine服务再从消息代理中异步获取监控信息,最后将这些信息聚合并输出到Hystrix Bashboard中。
构建一个新的应用以实现基于消息代理的Turbine聚合服务,具体如下。
- 创建一个标准的Spring Boot 工程,命名为 turbine-amqp。
- 编辑pom.xml,具体依赖内容如下:
<dependencies>
<!-- 添加springcloud eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加springboot actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 添加 turbine amqp -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine-stream</artifactId>
</dependency>
<!-- 添加 stream ribbit -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
</dependencies>
- 为应用添加主类 TurbineAmqpApplication
@EnableTurbineStream
@EnableDiscoveryClient
@SpringBootApplication
public class TurbineAmqpApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineAmqpApplication.class,args);
}
}
- application.yml 添加配置信息
#设置服务端口
server:
port: 2002
spring:
application:
#服务名
name: turbine-amqp
#RabbitMQ相关配置
rabbitmq:
host: 127.0.0.1
port: 5672
username: test
password: 123456
eureka:
client:
service-url:
#指定服务注册中心的地址
defaultZone: http://root:root@localhost:8082/eureka/
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
turbine:
# 需要聚合的服务名称,多个服务使用逗号(,)分隔
app-config: ribbon-consumer
# 指定服务所属集群
cluster-name-expression: new String('default')
# 以主机名和端口号区分服务
combine-host-port: true
对服务消费者 ribbon-consumer 做一些修改,使其监控信息能够输出到RabbitMQ上。
- 编辑pom.xml,添加依赖内容如下:
<!-- 添加 hystrix stream -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
</dependency>
<!-- 添加 stream rabbit -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
- 编辑application.ym,添加RabbitMQ内容如下:
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: test
password: 123456
完成以上配置之后,启动 RabibtMQ,eureka-server、hello-service、ribbin-consumer、hystrix-dashboard、
turbine-amqp。
对ribbin-consumer发送请求:http://localhost:9000/ribbon-consumer。
访问hystrix-dashboard:http://localhost:2001/hystrix 。
在hystrix-dashboard 文本框输入:http://localhost:2002,Titile:turbine-amqp,单击Moniter Strean按钮。界面如下。
打开RabbitMQ,切换到Exchanges栏,可以看到存在连个不同名字的exchanges,如下图。
这是由于服务向RabbitMQ发送数据到 Exchangs 和 Turbine 从RabbitMQ 获取数据的Exchangs 名字不一样,Turbine不能接受服务发送的监控数据,需要将exchangs 名为hystrixStreamOutput 绑定turbineStreamInput,单击hystrixStreamOutput进入绑定,如下图:
向ribbin-consumer 重新发送请求:http://localhost:9000/ribbon-consumer ,在hystrix-dashboard 文本框输入:http://localhost:2002,可看到监控的信息,如下图。
至此,Dashboard/Turbine断路器监控 搭建完成。
问题:ashboardConfiguration$ProxyStreamServlet : Origin parameter: http://localhost:2002 is not in the allowed list of proxy host names. If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.
解决:hystrix-dashboard 服务 application.yml 添加如下:
hystrix:
dashboard:
proxy-stream-allow-list: "localhost"