最近在学习Spring Cloud微服务时,配置熔断器仪表盘Hystrix Dashboard时,出现一些问题,无法看到图形化得页面,经过多次试验后发现是因为新旧版本暴露的终端路径不一致导致的。
首先是构建的服务结构是两个Eureka Server服务互相注册,负责服务发现和服务注册,暴露端口1111和1112,两个服务提供者HELLO-SERVICE通过Ribbon进行客户端负载均衡,一个服务消费者RIBBON-CONSOMER通过Hystrix熔断器请求服务提供者。
然后新建一个SpringBoot项目hystrix-dashboard用于展示hystrix仪表盘。
服务消费者RIBBON-CONSOMER配置如下:
启动类,通过注解开启了Eureka发现客户端和熔断器:
pom.xml内容如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.springboot
ribbon-consumer
0.0.1-SNAPSHOT
ribbon-consumer
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
2.1.1.RELEASE
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
2.1.1.RELEASE
org.springframework.boot
spring-boot-starter-test
test
log4j
log4j
1.2.17
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
Hystrix仪表盘服务启动类通过注解开启hystrix仪表盘,如下:
所有服务启动后,访问服务消费者RIBBON-CONSOMER:
然后访问仪表盘服务地址:http://localhost:2001/hystrix,
输入服务消费者开发的终端地址:http://localhost:9000/hystrix.stream,显示如下:
在hystrix-dashboard项目的终端日志中看到如下内容:
我访问的明明是http://localhost:9000/hystrix.stream,为什么会变成http://localhost:9000/actuator/hystrix.stream,查了一下资料,发现不同版本开发的终端路径发生了变化。因此这里需要配置新的终端路径。可以有两种配置方式,一种是在属性文件中配置,一种是在启动类中通过代码配置。
修改服务消费者RIBBON-CONSOMER的application.properties,增加端点路径:
management.endpoints.web.exposure.include=hystrix.stream,health,info
修改RibbonConsomerApplication类,设置HystrixMetricsStreamServlet的映射路径,这里可以设置成/hystrix.stream,也可以设置成/actuator/hystrix.stream,相应的仪表盘的访问路径要和这里的设置一致。
下面重新访问服务消费者RIBBON-CONSOMER开放的端点路径:http://localhost:9000/actuator/hystrix.stream,这次可以看到提示打开文件:
其中的内容就是请求的内容:
现在我们想要查看图表形式的数据就可以访问http://localhost:2001/hystrix,输入服务消费者提供的端点地址:
点击Monitor Stream,内容如下:
再次通过服务消费者访问服务提供者,仍然是加载状态,百思不得其解。。
后来无意中更换浏览器,使用Chrom成功,原来是Edge浏览器的问题。。