**
错误描述:
在打开DashBoard 监控视图的时候出现404,如下:
ashboardConfiguration$ProxyStreamServlet : Failed opening connection
to http://localhost:7001/actuator/hystrix.stream?delay=2000 : 404 :
HTTP/1.1 404
解决方法:
注入ServletRegistrationBean 这个bean即可;
package com.warrior.hiservice;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.beans.factory.annotation.Value;
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.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.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
public class HiServiceApplication {
public static void main(String[] args) {
SpringApplication.run( HiServiceApplication.class, args );
}
@RequestMapping("/hi")
@HystrixCommand(fallbackMethod = "hiError") //设置断路点
public String home(String name) {
if(name==null||name.isEmpty()){
int i=1/0;
}
return "hi ," + name ;
}
//错误时调用这个
public String hiError(String name) {
return "name不能为空";
}
//这个bean主要是解决/hystrix.stream视图无法打开的问题;
@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;
}
}