springcloud之spring2.0版本hystrix-dashboard Unable to connect to Command Metric Stream解决办法

首先我要吐槽!!!今天一天被spring坑死了妈的!怎么一升级就改依赖呢!!!!

(但是妈妈还是爱你的)

先说一下我的版本:

springcloud之spring2.0版本hystrix-dashboard Unable to connect to Command Metric Stream解决办法_第1张图片

倔强的我不愿意降版本,所以就只能硬着头皮解决

使用hystrix-dashboard的时候一直是这样的:

springcloud之spring2.0版本hystrix-dashboard Unable to connect to Command Metric Stream解决办法_第2张图片

在网上搜各种解决办法,有的说是依赖包没导全,有的说是注解没写全。。我都查了,一个没差,我的pom.xml长这样:



    4.0.0

    com.example
    service-ribbon
    0.0.1-SNAPSHOT
    jar

    service-ribbon
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Finchley.RELEASE
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix-dashboard
        

        
            com.netflix.hystrix
            hystrix-core
            RELEASE
        

        
            com.netflix.hystrix
            hystrix-core
            RELEASE
        
        
            org.springframework.cloud
            spring-cloud-commons
            2.0.0.RELEASE
        
        
            org.springframework.cloud
            spring-cloud-netflix-core
            2.0.0.RELEASE
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


eureka,hystrix,hystrix-dashboard,actuator一个没少

再看我的启动类:

package com.example.serviceribbon;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@EnableDiscoveryClient

@EnableEurekaClient
@SpringBootApplication
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

该enable的也enable了

关键是我不用dashboard是可以正常观察到熔断的,所以问题不出在hystrix的配置上,应该跟dashboard相关

然后我又去搜了很多资料,其实网上跟这个有关的真的蛮少的,大部分还是用的2.0以下的版本

但我还是倔强的不愿意降版本!

终于找到了一个可行的方法!https://blog.csdn.net/ddxd0406/article/details/79643059

这位大神通过看源码的方式,发现要配一个servlet,机智到不行!

于是我的启动类变成了这样!

package com.example.serviceribbon;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@EnableDiscoveryClient

@EnableEurekaClient
@SpringBootApplication
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

兴奋!似乎解决了!

但是打开dashboard,发现还是GG!

又是哪里出错了呢?

又翻了好多博客,找到了答案:https://blog.csdn.net/qq_20094989/article/details/79530995

如果使用springboot2.0 和spring cloud Finchley.M8 版本搭建 使用(/actuator/hystrix.stream  而不是/hystrix.stream 为插入点)

哭了好么!!于是应该是这样的!

springcloud之spring2.0版本hystrix-dashboard Unable to connect to Command Metric Stream解决办法_第3张图片

 

 

然后我们再启动一下ribbon,看到的是这样!

springcloud之spring2.0版本hystrix-dashboard Unable to connect to Command Metric Stream解决办法_第4张图片

然后访问http://localhost:8764/hi?name=mercy

回到dashboard这一页:

springcloud之spring2.0版本hystrix-dashboard Unable to connect to Command Metric Stream解决办法_第5张图片

成了哈哈哈哈!

 

 

 

转自:https://www.cnblogs.com/x1mercy/p/9291348.html

你可能感兴趣的:(springcloud之spring2.0版本hystrix-dashboard Unable to connect to Command Metric Stream解决办法)