这几天在学习Spring Cloud的Hystrix组件,想把Hystrix Dashboard运行起来看看,谁知过程中遇到各种各样的坑,费了个把小时排查问题,终于解决。
一、Spring Boot版本和Spring Cloud版本
Spring Boot == 2.3.7
Spring Cloud == Hoxton.SR9
二、pom.xml中的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.18</version>
</dependency>
三、启动类
package com.macroice.cloud.hystrixservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrix
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixServiceApplication.class, args);
}
}
四、配置
(1)编写配置类,加载HystrixMetricsStreamServlet,并映射到特定地址。
package com.macroice.cloud.hystrixservice.config;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HystrixDashboardConfig {
@Bean
public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("hystrixMetricsStreamServlet");
return registrationBean;
}
}
如果不加载HystrixMetricsStreamServlet,Hystrix Dashboard页面显示:
日志显示:
2020-12-24 17:40:33.901 WARN 41704 --- [nio-8401-exec-8] ashboardConfiguration$ProxyStreamServlet : Failed opening connection to http://localhost:8401/hystrix.stream : 404 : HTTP/1.1 404
(2)配置application.yml,加入以下内容:
hystrix:
dashboard:
proxy-stream-allow-list: "localhost"
如果不配置application.yml,Hystrix Dashboard页面仍然会显示“Unable to connect to Command Metric Stream.”,但日志显示:
2020-12-24 17:47:14.970 WARN 40000 --- [nio-8401-exec-8] ashboardConfiguration$ProxyStreamServlet : Origin parameter: http://localhost:8401/hystrix.stream is not in the allowed list of proxy host names. If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.
五、结果
完成上述配置,即可看到Hystrix Dashboard正常的页面: