SpringCloud趁热总结--熔断仪表盘

本文详细介绍了如何在SpringBoot2.1.1.RELEASE和SpringCloudGreenwich.RC1环境下,正确配置熔断功能。通过添加特定版本的依赖,如spring-cloud-starter-hystrix和spring-cloud-starter-hystrix-dashboard,并在启动类中使用@EnableHystrix等注解,可实现熔断监控。同时,文章解决了因版本不一致导致的URL配置错误问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

随着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;
    }
}

我试了一下,能解决问题
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值