解决spring cloud 2.x 中hystrix没有/actuator/hystrix.stream路径而报错Unable to connect to Command Metric Stream

首先,在启动类上添加以下注解:

@SpringCloudApplication
@EnableHystrixDashboard

或者(因为@SpringCloudApplication包括@SpringBootApplication@EnableDiscoveryClient@EnableCircuitBreaker)

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableHystrixDashboard

然后,再在启动类中添加以下代码:

	@Bean
	public ServletRegistrationBean getServlet(){
		HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
		ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
		registrationBean.setLoadOnStartup(1);
		registrationBean.addUrlMappings("/actuator/hystrix.stream");
		registrationBean.setName("HystrixMetricsStreamServlet");

		return registrationBean;
	}
  • 出现 Unable to connect to Command Metric Stream
    问题的原因是:我们开启监控的断路器流Hystrix
    Stream路径http://localhost:9999/actuator/hystrix.stream,既不是spring
    boot的默认路径,也不是hystrix的默认路径,所以要增加ServletRegistrationBean来制定断路器的指标流Servlet。
  • 首次成功启动页面也有可能会显示loading,这代表还没有访问我们要监控的服务,这时我们可以多次请求要访问的服务就可以看到指标数据。

你可能感兴趣的:(解决spring cloud 2.x 中hystrix没有/actuator/hystrix.stream路径而报错Unable to connect to Command Metric Stream)