Hystrix仪表板无法正常工作

时间:2016-10-04 11:26:06

标签: java spring-boot hystrix

我在springboot应用程序中使用REST服务。我正在研究Hystrix以启用指标。因此,我在版本7的tomcat服务器中部署了hystrix仪表板war。我可以打开页面http://localhost:8080/hystrix-dashboard/。在我添加了流URL并单击了Monitor streams按钮后,它将我带到了监视器页面,我所能看到的就是消息"无法连接到命令metrix stream"。当我查看hystrix控制台时,我反复看到以下消息。请帮助我!

2016-10-04 07:23:28 INFO  com.netflix.turbine.discovery.InstanceObservable$1:289 [InstanceObservable$1] [run]: Hosts up:1, hosts down: 0
2016-10-04 07:23:28 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:460 [InstanceMonitor] [getNextStatsData]: no more data from c
onnection to 138.12.51.246
2016-10-04 07:23:28 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:477 [InstanceMonitor] [retryHostConnection]: Re-initing host
connection: 138.12.51.246 default
2016-10-04 07:23:30 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:460 [InstanceMonitor] [getNextStatsData]: no more data from c
onnection to 138.12.51.246
2016-10-04 07:23:30 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:477 [InstanceMonitor] [retryHostConnection]: Re-initing host
connection: 138.12.51.246 default
2016-10-04 07:23:31 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:460 [InstanceMonitor] [getNextStatsData]: no more data from c
onnection to 138.12.51.246
2016-10-04 07:23:31 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:477 [InstanceMonitor] [retryHostConnection]: Re-initing host
connection: 138.12.51.246 default
2016-10-04 07:23:32 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:460 [InstanceMonitor] [getNextStatsData]: no more data from c
onnection to 138.12.51.246
2016-10-04 07:23:32 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:477 [InstanceMonitor] [retryHostConnection]: Re-initing host
connection: 138.12.51.246 default
2016-10-04 07:23:33 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:460 [InstanceMonitor] [getNextStatsData]: no more data from c
onnection to 138.12.51.246
2016-10-04 07:23:33 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:477 [InstanceMonitor] [retryHostConnection]: Re-initing host
connection: 138.12.51.246 default
2016-10-04 07:23:35 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:460 [InstanceMonitor] [getNextStatsData]: no more data from c
onnection to 138.12.51.246
2016-10-04 07:23:35 INFO  com.netflix.turbine.monitor.instance.InstanceMonitor:477 [InstanceMonitor] [retryHostConnection]: Re-initing host
connection: 138.12.51.246 default
2016-10-04 07:23:36 INFO 

1 个答案:

答案 0 :(得分:2)

找到答案..我们需要在响应头中添加MIME类型作为text / event-stream。我在这里写的修复程序专门用于Springboot与Hystrix仪表板的集成。您需要做的就是,

1)在pom.xml中添加指标事件流依赖

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-metrics-event-stream</artifactId>
    <version>1.5.5</version>
</dependency>

2)在SpringApplication类中添加带有Bean注释的新servlet

@Bean
public ServletRegistrationBean servletRegistration() {
 ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet(), "/hystrix.stream"); 
 return registration;
}

3)  使用具有text / event-stream MIME类型的响应头返回响应。 即使如果你有另一种内容类型作为application / xml,也可以使用这个新内容类型。

HttpHeaders resHeaders = new HttpHeaders();
resHeaders.add("Content-Type", "application/xml; charset=utf-8");
resHeaders.add("Content-Type", "text/event-stream; charset=utf-8");
if(!isError){
return new ResponseEntity<String>(responseXml, resHeaders, HttpStatus.OK);
}
else{
return new ResponseEntity<com.test.pack.fault.Error>(error, resHeaders,HttpStatus.CREATED);
    }

4)在版本7或更高版本的Tomcat服务器中下载并部署Hystrix仪表板war文件并启动服务器。

5)使用网址http://localhost:port/hystrix-dashboard

打开信息中心

6)在hystrix主页中,将应用程序URL添加为http://localhost:port/contextPath(if any)/hystrix.stream。 然后单击添加流按钮并单击监视器流按钮。 hystrix.stream是我们在servlet注册中给出的url映射。