SpringBoot下HystrixDashboard出现Http401状态码导致“hystrix dashboard Unable to connect to Command”数据无法出现的解决办法

一. 问题描述

笔者在用SpringBoot 2.1.4.RELEASE + SpringCloud Greenwich.SR5学习Hystrix时,在Hystrix Dashboard页面出现了一个常见错误:hystrix dashboard Unable to connect to Command Metric Stream,截图如下:

在这里插入图片描述
而我直接在浏览器中输入hystrix.stream地址:http://localhost:20081/actuator/hystrix.stream 是能访问到接口内容的
在这里插入图片描述

二. 原因分析

1. SpringBoot2.0版本问题排查

首先审查了下,项目需要的security及hystrix相关依赖都已添加:

        <!-- spring security依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
   
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <!-- dashboard可视化面板 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

一开始我以为是SpringBoot2.0以上版本出现的:url增加/actuator、需要配置Servlet映射问题(参见此博客)。

@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;
    }

但发现此配置没有用,依然会报错。思考到我之前就可以通过直接输入地址:http://localhost:20081/actuator/hystrix.stream 访问到接口内容,那应该不会是SpringBoot2.0的问题,换其他思路

2. 看后台服务报错

回看到后台服务的日志信息,发现后台服务报错与上文博客中的情况不同,为Http 401错误,日志如下

2021-11-13 20:36:30.002  WARN 22544 --- [io-20081-exec-7] ashboardConfiguration$ProxyStreamServlet : Failed opening connection to http://localhost:20081/actuator/hystrix.stream : 401 : HTTP/1.1 401 
2021-11-13 20:36:30.007  WARN 22544 --- [o-20081-exec-10] ashboardConfiguration$ProxyStreamServlet : Failed opening connection to http://localhost:20081/actuator/hystrix.stream : 401 : HTTP/1.1 401 
2021-11-13 20:36:45.476  INFO 22544 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

HTTP 401错误是指用户权限认证出现问题。这提醒了我,本次实验项目pom中的Security模块是很早之前练习Eureka时就已经引入的包,当时还做了一些自定义配置。
会不会是当时的自定义配置影响了接口的调用?查到相关代码如下:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //所有的请求,都需要经过HTTP Basic认证
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .httpBasic();
    }
}

这里就看到了问题原因所在:Security设置的项目接口都需要HTTPBasic输入用户名和密码进行登录认证。但Dashboard页面框架是作为get参数来加载hystrix.stream后台接口,此时后台系统无法认证相关接口用户登录态问题,则报错401

三. 解决办法

因为需要Dashboard自己轮询http://localhost:20081/actuator/hystrix.stream 这个接口,所以需要让后台服务将此接口进行登录态放行,修改此方法:


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //所有的请求,都需要经过HTTP Basic认证
        http.authorizeRequests()
                .antMatchers("/actuator/hystrix.stream").permitAll()        //放行/actuator/hystrix.stream
                .anyRequest().authenticated()
                .and()
                .httpBasic();
    }

只增加了一行代码.antMatchers("/actuator/hystrix.stream").permitAll()。完成后重启,发现Dashboard成功出现监控数据
在这里插入图片描述

四. 总结

遇到问题需要对症下药,查到原因才能更好的解决

PS:

最开始测试时,我在打开浏览器访问本项目其他接口时,也做过用户认证,cookie中记下了我的登录态。因此后来直接用浏览器打开http://localhost:20081/actuator/hystrix.stream 接口时没有弹出登录接口,以至于疏忽了Security安全认证的配置
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot提供了与Hystrix的集成,以便在微服务架构中实现容错和故障保护。要整合Hystrix,首先需要在Spring Boot项目的pom.xml文件中添加HystrixHystrix Dashboard的依赖。 ```xml <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> ``` 接下来,在Spring Boot应用程序的启动类上添加`@EnableCircuitBreaker`注解,以启用Hystrix断路器功能。 ```java import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableCircuitBreaker public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 然后,在需要应用Hystrix的方法上使用`@HystrixCommand`注解,以定义回退逻辑。 ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @RestController public class YourController { @GetMapping("/yourEndpoint") @HystrixCommand(fallbackMethod = "fallbackMethod") public String yourMethod() { // Your method logic here // ... } public String fallbackMethod() { // Fallback logic here // ... } } ``` 最后,启动应用程序并访问Hystrix Dashboard的URL(默认为`http://localhost:8080/hystrix`),在输入框中输入Hystrix流的URL(默认为`/actuator/hystrix.stream`),然后点击"Monitor Stream"按钮,即可监控Hystrix断路器的状态。 这就是Spring Boot整合Hystrix的基本步骤。通过使用Hystrix,可以实现对微服务的容错和故障保护,提高系统的可靠性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭Albert

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值