spring cloud 在初次使用 hystrix dashboard儀表盤的時候很容易出現hystrix dashboard Unable to connect to Command Metric Stream錯誤java
以下圖:web
首先查看依賴時候添加全spring
<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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
其次啓動程序註解是否添加springboot
@EnableCircuitBreaker
@EnableHystrixDashboard
若是都沒問題那麼檢查下springboot 版本若是是2.0則須要添加 ServletRegistrationBean 由於springboot的默認路徑不是 "/hystrix.stream",只要在本身的項目裏配置上下面的servlet就能夠了app
@Bean public ServletRegistrationBean getServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
修改完成重啓服務問題便會解決ide
分析:spring-boot
首先,查看源碼中 類 HystrixStreamEndpoint 從中能夠看到 new EndpointServlet(HystrixMetricsStreamServlet.class)ui
package org.springframework.cloud.netflix.hystrix;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; import java.util.Map; import java.util.function.Supplier; import org.springframework.boot.actuate.endpoint.web.EndpointServlet; import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpoint; @ServletEndpoint( id = "hystrix.stream" ) public class HystrixStreamEndpoint implements Supplier<EndpointServlet> { private final Map<String, String> initParameters; public HystrixStreamEndpoint(Map<String, String> initParameters) { this.initParameters = initParameters; } public EndpointServlet get() { return (new EndpointServlet(HystrixMetricsStreamServlet.class)).withInitParameters(this.initParameters); } }
而後,咱們再查看 HystrixMetricsStreamServlet 這個類 在方法的註釋中咱們能夠看到this
有提示 「Adding the following to web.xml」atom
而在springboot中是採用bean的形式配置就能夠解決問題了
package com.netflix.hystrix.contrib.metrics.eventstream; import com.netflix.config.DynamicIntProperty; import com.netflix.config.DynamicPropertyFactory; import com.netflix.hystrix.contrib.sample.stream.HystrixSampleSseServlet; import com.netflix.hystrix.metric.consumer.HystrixDashboardStream; import com.netflix.hystrix.serial.SerialHystrixDashboardData; import rx.Observable; import rx.functions.Func1; import java.util.concurrent.atomic.AtomicInteger; /** * Streams Hystrix metrics in text/event-stream format. * <p> * Install by: * <p> * 1) Including hystrix-metrics-event-stream-*.jar in your classpath. * <p> * 2) Adding the following to web.xml: * <pre>{@code * <servlet> * <description></description> * <display-name>HystrixMetricsStreamServlet</display-name> * <servlet-name>HystrixMetricsStreamServlet</servlet-name> * <servlet-class>com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet</servlet-class> * </servlet> * <servlet-mapping> * <servlet-name>HystrixMetricsStreamServlet</servlet-name> * <url-pattern>/hystrix.stream</url-pattern> * </servlet-mapping> * } </pre> */ public class HystrixMetricsStreamServlet extends HystrixSampleSseServlet { private static final long serialVersionUID = -7548505095303313237L; /* used to track number of connections and throttle */ private static AtomicInteger concurrentConnections = new AtomicInteger(0); private static DynamicIntProperty maxConcurrentConnections = DynamicPropertyFactory.getInstance().getIntProperty("hystrix.config.stream.maxConcurrentConnections", 5); public HystrixMetricsStreamServlet() { this(HystrixDashboardStream.getInstance().observe(), DEFAULT_PAUSE_POLLER_THREAD_DELAY_IN_MS); } /* package-private */ HystrixMetricsStreamServlet(Observable<HystrixDashboardStream.DashboardData> sampleStream, int pausePollerThreadDelayInMs) { super(sampleStream.concatMap(new Func1<HystrixDashboardStream.DashboardData, Observable<String>>() { @Override public Observable<String> call(HystrixDashboardStream.DashboardData dashboardData) { return Observable.from(SerialHystrixDashboardData.toMultipleJsonStrings(dashboardData)); } }), pausePollerThreadDelayInMs); } @Override protected int getMaxNumberConcurrentConnectionsAllowed() { return maxConcurrentConnections.get(); } @Override protected int getNumberCurrentConnections() { return concurrentConnections.get(); } @Override protected int incrementAndGetCurrentConcurrentConnections() { return concurrentConnections.incrementAndGet(); } @Override protected void decrementCurrentConcurrentConnections() { concurrentConnections.decrementAndGet(); } }