SpringCloud之断路器监控(Hystrix Dashboard)(九)

断路器断路器模式源于Martin Fowler的Circuit Breaker一文。“断路器”本身是一种开关装置,用于在电路上保护线路过载,当线路中有电器发生短路时,“断路器”能够及时的切断故障电路,防止发生过载...

断路器

断路器模式源于Martin Fowler的Circuit Breaker一文。“断路器”本身是一种开关装置,用于在电路上保护线路过载,当线路中有电器发生短路时,“断路器”能够及时的切断故障电路,防止发生过载、发热、甚至起火等严重后果。

在分布式架构中,断路器模式的作用也是类似的,当某个服务单元发生故障(类似用电器发生短路)之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。

断路器监控

在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面

改造项目

复制项目 spring-cloud-ribbon-consumer-hystrix,修改名称 spring-cloud-ribbon-consumer-hystrix-dashboard 在它的基础上进行改造。 Feign的改造和这一样。

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

<dependency>    
<groupId>
org.springframework.cloud
</groupId>    
<artifactId>
spring-cloud-starter-hystrix
</artifactId>
</dependency>
<dependency> 
<groupId>
org.springframework.boot
</groupId>    
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
 
<dependency>   
<groupId>
org.springframework.cloud
</groupId>    
<artifactId>
spring-cloud-starter-hystrix-dashboard
</artifactId>
</dependency>

开启 HD

修改 RibbonConsumerApplication.java 类

在程序的入口 RibbonConsumerApplication类,加上 @EnableHystrix注解开启断路器,这个是必须的,并且需要在程序中声明断路点 @HystrixCommand;加上 @EnableHystrixDashboard注解,开启 HystrixDashboard,欢迎大家一起学习研究相关技术愿意了解源码的朋友直接求求交流分享技术:2147775633

package io.ymq.example.ribbon.consumer.hystrix;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
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;
@EnableHystrix
@EnableDiscoveryClient
@EnableHystrixDashboard
@SpringBootApplication
public class RibbonConsumerApplication {
   @LoadBalanced
   @Bean
   RestTemplate restTemplate() {
       return new RestTemplate();
   public static void main(String[] args) {
       SpringApplication.run(RibbonConsumerApplication.class, args);

声明断路点

声明断路点 @HystrixCommand(fallbackMethod="defaultStores")

package io.ymq.example.ribbon.consumer.hystrix;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
* 描述:调用提供者的 `home` 方法
* @author yanpenglei
@RestController
public class ConsumerController {
   @Autowired
   private RestTemplate restTemplate;
   @HystrixCommand(fallbackMethod = "defaultStores")
   @GetMapping(value = "/hello")
   public String hello() {
       return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody();
   public String defaultStores() {
   return "feign + hystrix Dashboard ,提供者服务挂了";

@HystrixCommand 表明该方法为 hystrix包裹,可以对依赖服务进行隔离、降级、快速失败、快速重试等等 hystrix相关功能 该注解属性较多,下面讲解其中几个

fallbackMethod 降级方法

commandProperties 普通配置属性,可以配置 HystrixCommand对应属性,例如采用线程池还是信号量隔离、熔断器熔断规则等等

ignoreExceptions 忽略的异常,默认 HystrixBadRequestException不计入失败

groupKey() 组名称,默认使用类名称

commandKey 命令名称,默认使用方法名

更多详细源码参考来源:http://minglisoft.cn/honghu/technology.html


  • 发表于 2018-09-07 14:58
  • 阅读 ( 454 )
  • 分类:Java基础

1 条评论

请先 登录 后评论
不写代码的码农
IT达人

212 篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除