鱼C论坛

 找回密码
 立即注册
查看: 1908|回复: 1

[技术交流] SpringCloud之断路器监控(Hystrix Dashboard)(九)

[复制链接]
发表于 2018-9-7 15:07:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

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

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

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

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

  1. <dependency>   
  2. <groupId>
  3. org.springframework.cloud
  4. </groupId>   
  5. <artifactId>
  6. spring-cloud-starter-hystrix
  7. </artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>
  11. org.springframework.boot
  12. </groupId>   
  13. <artifactId>
  14. spring-boot-starter-actuator
  15. </artifactId>
  16. </dependency>

  17. <dependency>   
  18. <groupId>
  19. org.springframework.cloud
  20. </groupId>   
  21. <artifactId>
  22. spring-cloud-starter-hystrix-dashboard
  23. </artifactId>
  24. </dependency>
复制代码

开启 HD
修改 RibbonConsumerApplication.java 类

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

  1. package io.ymq.example.ribbon.consumer.hystrix;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.cloud.netflix.hystrix.EnableHystrix;
  7. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  8. import org.springframework.context.annotation.Bean;
  9. .
  10. .
  11. import org.springframework.web.client.RestTemplate;
  12. @EnableHystrix
  13. @EnableDiscoveryClient
  14. @EnableHystrixDashboard
  15. @SpringBootApplication
  16. public class RibbonConsumerApplication {
  17.    @LoadBalanced
  18.    @Bean
  19.    RestTemplate restTemplate() {
  20.        return new RestTemplate();
  21.    public static void main(String[] args) {
  22.        SpringApplication.run(RibbonConsumerApplication.class, args);   
复制代码
                                    
声明断路点
声明断路点 @HystrixCommand(fallbackMethod="defaultStores")

  1. package io.ymq.example.ribbon.consumer.hystrix;
  2. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Component;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import org.springframework.web.client.RestTemplate;
  8. * 描述:调用提供者的 `home` 方法
  9. * @author yanpenglei
  10. @RestController
  11. public class ConsumerController {
  12.    @Autowired
  13.    private RestTemplate restTemplate;
  14.    @HystrixCommand(fallbackMethod = "defaultStores")
  15.    @GetMapping(value = "/hello")
  16.    public String hello() {
  17.        return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody();
  18.    public String defaultStores() {
  19.    return "feign + hystrix Dashboard ,提供者服务挂了";
复制代码

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

fallbackMethod 降级方法

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

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

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

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

更多详细源码参考来源:http://minglisoft.cn/honghu/technology.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-9-7 15:08:39 | 显示全部楼层
喜欢的朋友可以持续关注更新文章!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 17:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表