生成运行数据。
Hystrix 只监控 @HystrixCommand ,只要想对服务进行监控,就必须加 @HystrixCommand,没有降级方法也要加。
收集运行数据。
展示运行数据。
spring-cloud.s06.dashboard 在 pom 中添加依赖
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies> 添加配置文件
application.yml
server:
port: 35001
spring:
application:
name: hystrix-dashboard
profiles:
active: dev
eureka:
instance:
hostname: localhost
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
serviceUrl:
defaultZone: http://user:123123@localhost:34001/eureka/ 创建启动类 ..HystrixDashboard
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboard {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboard.class, args);
}
} http://localhost:35001/hystrix 可见控制台。 HystrixDashboard 只能展示被 @HystrixCommand 标记的方法的运行数据。这里使用 store-hystrix 项目提供。想要向外提供hystrix 的监控数据,还需要完成一些让工作。
完善项目 store-hystrix 的启动类 ..StoreHystrix ,增加
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); //监控实例
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); //servlet注册接口
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream"); //路径
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
} 这样,该项目的 /actuator/hystrix.stream 端点才可以输入数据。
http://localhost:31003/actuator/hystrix.stream 可以看到 ping 信息不停刷新。
http://localhost:31003/hystrix/isolation/thread ,再重新回到 http://localhost:31003/actuator/hystrix.stream 可以看到 data 一同刷新。
http://localhost:31003/actuator/hystrix.stream 输入到对话框,点击 Monitor Stream 后可以看到一个空的运行曲线。 接下来访问 http://localhost :31003/hystrix/isolation/thread 让标记 @HystrixCommand 的程序运行,可以看到监控数据的变化。
左边图标中,不同意义的数字用不同的颜色显示,具体的含义对照右边的说明。
可能产生异常的访问
http://localhost:31003/hystrix/cf/always-exception http://localhost:31003/hystrix/isolation/thread http://localhost:31003/hystrix/isolation/semaphore