千家信息网

SpringCloud怎么实现断路器监控

发表于:2024-10-14 作者:千家信息网编辑
千家信息网最后更新 2024年10月14日,这篇文章主要介绍"SpringCloud怎么实现断路器监控",在日常操作中,相信很多人在SpringCloud怎么实现断路器监控问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家
千家信息网最后更新 2024年10月14日SpringCloud怎么实现断路器监控

这篇文章主要介绍"SpringCloud怎么实现断路器监控",在日常操作中,相信很多人在SpringCloud怎么实现断路器监控问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"SpringCloud怎么实现断路器监控"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、Hystrix Dashboard简介

在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的

可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作

为断路器状态的一个组件,提供了数据监控和友好的图形化界面。

二、准备工作

本文的的来源于第一篇文章的栗子,在它的基础上进行改造。

三、开始改造service-hi

在pom的工程文件引入相应的依赖:

                    org.springframework.cloud            spring-cloud-starter-netflix-eureka-client                            org.springframework.boot            spring-boot-starter-web                            org.springframework.boot            spring-boot-starter-actuator                            org.springframework.cloud            spring-cloud-starter-netflix-hystrix                            org.springframework.cloud            spring-cloud-starter-netflix-hystrix-dashboard                    

其中,这三个依赖是必须的,缺一不可。

在程序的入口ServiceHiApplication类,加上@EnableHystrix注解开启断路器,这个是必须的,并且需要在程序中声明断路点

HystrixCommand;加上@EnableHystrixDashboard注解,开启HystrixDashboard

@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClient@RestController@EnableHystrix@EnableHystrixDashboard@EnableCircuitBreakerpublic class ServiceHiApplication {    /**     * 访问地址 http://localhost:8762/actuator/hystrix.stream     * @param args     */    public static void main(String[] args) {        SpringApplication.run( ServiceHiApplication.class, args );    }    @Value("${server.port}")    String port;    @RequestMapping("/hi")    @HystrixCommand(fallbackMethod = "hiError")    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {        return "hi " + name + " ,i am from port:" + port;    }    public String hiError(String name) {        return "hi,"+name+",sorry,error!";    }}

运行程序: 依次开启eureka-server 和service-hi.

四、Hystrix Dashboard图形展示
打开http://localhost:8762/actuator/hystrix.stream,可以看到一些具体的数据:

打开localhost:8762/hystrix 可以看见以下界面:

在界面依次输入:http://localhost:8762/actuator/hystrix.stream 、2000 、miya;点确定。

在另一个窗口输入: http://localhost:8762/hi?name=forezp

重新刷新hystrix.stream网页,你会看到良好的图形化界面:

到此,关于"SpringCloud怎么实现断路器监控"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0