Spring Cloud Netfilx出现Unable to connect to Command Metric Stream.问题。
监控不到可能出现的原因:
1.监控服务确保都加入监控引入actuator依赖
2.服务端启动类需要加入ServletRegistrationBean处理
3.请求方法需要加入熔断器hystrix
4.注意版本差异
- 服务端加入监控依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 服务端启动类代码
package com;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableEurekaClient
@MapperScan("com.mapper")
@EnableCircuitBreaker
public class AppMysqlDB1Run {
public static void main(String[] args) {
SpringApplication.run(AppMysqlDB1Run.class);
}
//需加入该关键代码
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
}
- 创建Dashboard项目
<dependencies>
<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-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
- 启动类代码
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class AppActuatorRun {
public static void main(String[] args) {
SpringApplication.run(AppActuatorRun.class);
}
}
- 启动Dashboard,进入http://localhost:8085/hystrix/
- 填入服务端链接如:http://localhost:8081/actuator/hystrix.stream,Delay默认2000就行,title随意
- 进入后有请求会看到该页面,没请求就一直出现loding长时间后就会Unable to connect to Command Metric Stream.
- 请求一定要是hystrix配置熔断的方法才有效,不然监控不到