记录学习Hystrix Dashboard过程中出现的问题

这几天在学习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正常的页面:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值