最近在使用 spring-cloud-starter-netflix-hystrix-dashboard 时,发现一个坑,当我访问页面: http://localhost:8081/ribbon-api/hystrix 后,配置如下图:
点击 Monitor Stream 进入新的页面,但页面无数据显示,且打开 F12 看到 JS 报错,如下图:
看到报错第一时间想到是 JS 不兼容,但是为了保险起见,先排除代码问题,访问: http://localhost:8081/ribbon-api/actuator/hystrix.stream 可看到如下界面:
从上图可以看出,代码是没有问题的,因为有监控到数据。所以,开始着手改 JS 错误,找到 maven 仓库所在位置并找到 spring-cloud-netflix-hystrix-dashboard jar 包所在位置,解压 jar 包,并修改 monitor.ftlh 文件(文件中搜索报错代码得知),如下图:
打开 idea 中项目左侧的External Libraries 查看是否有这个文件,有些同学说找不到,是找错包了,我截图的包名中没有starter,请注意!!!
# 将上面这行修改为下面那行,共2处
$(window).load(function() {
$(window).on('load', function() {
然后将修改后的文件重新压缩成 jar 包,cmd 窗口使用如下命令:
# 这是在 spring-cloud-netflix-hystrix-dashboard-2.2.3.RELEASE 目录下进行压缩
# 得到jar包后需拷贝到上一级目录,也就是原jar包所在目录
jar -cvf spring-cloud-netflix-hystrix-dashboard-2.2.3.RELEASE.jar *
查看新 jar 包的 SHA1 值,使用如下命令:
# 如需查看 MD5 或 SHA256 ,将 SHA1 替换即可
certutil -hashfile spring-cloud-netflix-hystrix-dashboard-2.2.3.RELEASE.jar SHA1
执行结果如下图:
将 SHA1 值替换原 spring-cloud-netflix-hystrix-dashboard-2.2.3.RELEASE.jar.sha1 文件中的值,并保存。
重新运行代码,再次访问页面,可看到下图:
打开 F12 再无 JS 报错,问题解决,特记录并分享之。
还有另一种办法,那就是版本降级,原理是降低了依赖的 JS 版本,配置如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>