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

断路器

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

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

断路器监控

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

改造项目

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

在 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 类app

在程序的入口 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")ide

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相关功能 该注解属性较多,下面讲解其中几个spring-boot

fallbackMethod 降级方法

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

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

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

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

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

相关文章
相关标签/搜索