随着springboot和springcloud版本的不断更新,不同版本之间不兼容真是太坑了,教程好多都是用之前的版本,说不定哪个依赖引错了都会导致报错,真是心累。
环境及版本
jdk1.8
spring boot 2.1.1.RELEASE
spring cloud Greenwich.RC1
在需要熔断的项目上引入依赖,我看的教程上没有版本号,引入的时候会报错,加上版本号即可
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在启动类上添加注解
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
然后在浏览器中输入localhost:端口号/hystrix,就可以看见小狮几啦
如果监控单个应用的话需要在上面的框框中输入http://localhost:8081/actuator/hystrix.stream
因为版本问题,所以填写这个url,不同的版本填的url不同,主要区别在actuator。
点击Monitor Stream就可以看见仪表盘页面了,但是我当时访问出错了,提示Unable to connect to Command Metric Stream
后台服务也显示404,翻了一些博客,找到了一个解决办法。https://blog.youkuaiyun.com/ddxd0406/article/details/79643059
在启动类里添加一个servlet
@SpringBootApplication
@MapperScan("com.springcloud.learn.blog_viewer_content.mapper")
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class BlogViewerContentApplication {
public static void main(String[] args) {
SpringApplication.run(BlogViewerContentApplication.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;
}
}
我试了一下,能解决问题