在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型.断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标.Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面.
二. 具体实现本案例在前一篇博客的代码基础之上进行改造.
请参考:https://blog.csdn.net/syc000666/article/details/96097567
我们改造ribbon_consumer服务消费者这个模块.
1. 修改pom.xml文件添加熔断器面板依赖,同时也需要添加健康监测依赖包.
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.boot
spring-boot-starter-actuator
2. 修改入口类,添加新的注解
在程序的入口ServiceRibbonApplication类,添加@EnableHystrix注解开启断路器,这个是必须的,并且需要在程序中声明断路点HystrixCommand,然后再加上@EnableHystrixDashboard注解,开启HystrixDashboard.
package com.syc.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;
@SpringBootApplication
@EnableDiscoveryClient //注意:必须是@EnableDiscoveryClient注解,不能是@EnableEurekaClient,否则无法实现负载均衡.
//@EnableEurekaClient
@EnableHystrix
@EnableCircuitBreaker //开启服务熔断功能
@EnableHystrixDashboard
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
//创建负载均衡组件
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
3. 修改配置文件
在application.yml中需要添加如下配置,否则不能获取端点信息.
#actuator设置
management:
endpoints:
web:
exposure:
include: "*" #暴露所有端点,默认是info和health
endpoint:
health:
show-details: always #默认是never
三. 启动项目,查看效果
此时启动的项目如下:
此时打开 http://localhost:8764/actuator/hystrix.stream,可以看到一些具体的数据.
打开http://localhost:8764/hystrix 可以看见以下界面:
然后在界面上依次输入http://localhost:8764/actuator/hystrix.stream 、2000 、yyg,点击确定.
在另一个窗口输入http://localhost:8764/hello?name=一一哥
重新刷新hystrix.stream网页,你会看到良好的图形化界面: