在复杂的分布式系统中,可能有几十个服务相互依赖,这些服务由于某些原因,例如机房的不可靠性、网络服务商的不可靠性等,导致某个服务不可用 。 如果系统不隔离该不可用的服务,可能会导致整个系统不可用。Hystrix 提供了熔断器功能,能够阻止分布式系统中出现联动故障。Hystrix 是通过隔离服务的访问点阻止联动故障的,并提供了故障的解决方案,从而提高了整个分布式系统的弹性。

使用 Hystrix

在 Ribbon 使用 Hystrix

添加依赖包

<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
</dependency>

修改启动类

@EnableHystrix // 启用 Hystrix 熔断器
@EnableEurekaClient
@SpringBootApplication
public class EurekaRibbonClientApp {
public static void main(String[] args){
SpringApplication.run(EurekaRibbonClientApp.class, args);
}
}

修改 Service

@Service
public class RibbonService {
@Autowired
RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hiError") // 启用 Hystrix 熔断器
public String hi(String name){
return restTemplate.getForObject("http://eureka-client/hi?name=" + name, String.class);
} public String hiError(String name){
return "error! sorry, " + name;
}
}

测试

  1. 启动 eureka-server
  2. 启动 eureka-ribbon-client

访问 http://localhost:8021/hi?name=victor 可以看到返回的是 hiError 方法执行的结果

在 Feign 使用 Hystrix

application.yml 添加

feign:
hystrix:
enabled: true

添加熔断处理类

@Component
public class HiHystrix implements EurekaClientFeign { @Override
public String sayHi(String name) {
return "error! sorry, " + name;
}
}

修改 FeignClient

@FeignClient(value = "eureka-client", configuration = FeignConfig.class
, fallback = HiHystrix.class)
public interface EurekaClientFeign { @GetMapping("/hi")
String sayHi(@RequestParam(value = "name") String name);
}

测试

  1. 启动 eureka-server
  2. 启动 eureka-feign-client

访问 http://localhost:8031/hi?name=victor 可以看到返回的是 hiError 方法执行的结果

使用 Hystrix Dashboard 监控熔断器状态

在 Ribbon 使用 Hystrix Dashboard

添加依赖包

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

修改启动类

@EnableHystrix // 启用 Hystrix 熔断器
@EnableHystrixDashboard // 启用 Hystrix Dashboard
@EnableEurekaClient
@SpringBootApplication
public class EurekaRibbonClientApp {
public static void main(String[] args){
SpringApplication.run(EurekaRibbonClientApp.class, args);
}
}

application.yml 添加

management:
endpoints:
web:
exposure:
include: "*"

测试

  1. 启动 eureka-server
  2. 启动 eureka-ribbon-client

先访问 http://localhost:8021/hi?name=victor,然后访问 http://localhost:8021/actuator/hystrix.stream 浏览器上会显示熔断器的数据指标

访问 http://localhost:8021/hystrix,然后在界面输入 http://localhost:8021/actuator/hystrix.stream 点击 [Monitor Stream] 按钮可以看到熔断器的各种数据指标

在 Feign 使用 Hystrix Dashboard

添加依赖包

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-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-netflix-hystrix-dashboard</artifactId>
</dependency>

与 Ribbon 相比需要添加 spring-cloud-starter-netflix-hystrix 依赖

修改启动类

@EnableFeignClients // 开启 Feign Client
@EnableHystrixDashboard // 启用 Hystrix Dashboard
@EnableCircuitBreaker // 启用 Hystrix 熔断器
@EnableEurekaClient
@SpringBootApplication
public class EurekaFeignClientApp {
public static void main(String[] args){
SpringApplication.run(EurekaFeignClientApp.class, args);
}
}

与 Ribbon 相比需要添加 @EnableCircuitBreaker 注解

application.yml 添加

management:
endpoints:
web:
exposure:
include: "*"

测试

  1. 启动 eureka-server
  2. 启动 eureka-feign-client

先访问 http://localhost:8031/hi?name=victor,然后访问 http://localhost:8031/actuator/hystrix.stream 浏览器上会显示熔断器的数据指标

访问 http://localhost:8031/hystrix,然后在界面输入 http://localhost:8031/actuator/hystrix.stream 点击 [Monitor Stream] 按钮可以看到熔断器的各种数据指标

使用 Turbine 聚合监控

新建 spring-cloud-eureka-turbine-client Module

pom

<parent>
<artifactId>spring-cloud-parent</artifactId>
<groupId>com.karonda</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-eureka-turbine-client</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

application.yml

server:
port: 8041 eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/ spring:
application:
name: turbine-client
turbine:
aggregator:
cluster-config: default
app-config: ribbon-client, feign-client
cluster-name-expression: new String("default")

启动类

@SpringBootApplication
@EnableHystrixDashboard // 启用 Hystrix Dashboard
@EnableTurbine // 启用 Hystrix Turbine
public class EurekaTurbineClientApp {
public static void main(String[] args){
SpringApplication.run(EurekaTurbineClientApp.class, args);
}
}

测试

  1. 启动 eureka-server
  2. 启动 eureka-ribbon-client
  3. 启动 eureka-feign-client
  4. 启动 eureka-turbine-client
  5. 访问 http://localhost:8021/hi?name=victor
  6. 访问 http://localhost:8031/hi?name=victor

访问 http://localhost:8021/hystrixhttp://localhost:8031/hystrix 然后在界面输入 http://localhost:8041/turbine.stream 点击 [Monitor Stream] 按钮可以看到 eureka-ribbon-client 和 eureka-feign-client 熔断器的各种数据指标

完整代码:GitHub

本人 C# 转 Java 的 newbie, 如有错误或不足欢迎指正,谢谢

Spring Cloud 学习 (四) Hystrix & Hystrix Dashboard & Turbine的更多相关文章

  1. spring cloud学习(五)断路器 Hystrix

    断路器 Hystrix 断路器模式 (云计算设计模式) 断路器模式源于Martin Fowler的Circuit Breaker一文. 在分布式环境中,其中的应用程序执行访问远程资源和服务的操作,有可 ...

  2. spring cloud学习(四) Fegin 的使用

    Feign 的使用 什么是Feign? Feign : Declarative REST clients. Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创 ...

  3. spring cloud学习(四) 动态路由

    Zuul的主要功能是路由和过滤器.路由功能是微服务的一部分,zuul实现了负载均衡. 1.1 新建模块zuul pom.xml <?xml version="1.0" enc ...

  4. Spring Cloud(四):服务容错保护 Hystrix【Finchley 版】

    Spring Cloud(四):服务容错保护 Hystrix[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  分布式系统中经常会出现某个基础服务不可用 ...

  5. Spring Cloud第八篇 | Hystrix集群监控Turbine

    ​ 本文是Spring Cloud专栏的第八篇文章,了解前七篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  6. Spring Cloud第六篇 | Hystrix仪表盘监控Hystrix Dashboard

    本文是Spring Cloud专栏的第六篇文章,了解前五篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud ...

  7. spring cloud 2.x版本 Hystrix Dashboard断路器教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  8. spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法

    turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...

  9. 【Spring Cloud笔记】 断路器-hystrix

    在微服务架构中,一个微服务的超时失败可能导致瀑布式连锁反映,Spring Cloud Netflix 的断路器Hystrix通过自主反馈,防止了这种情况发生.下面介绍简单的断路器使用方法. [step ...

随机推荐

  1. AQS源码深入分析之共享模式-你知道为什么AQS中要有PROPAGATE这个状态吗?

    本文基于JDK-8u261源码分析 本篇文章为AQS系列文的第二篇,前文请看:[传送门] 第一篇:AQS源码深入分析之独占模式-ReentrantLock锁特性详解 1 Semaphore概览 共享模 ...

  2. Redis 和 Memcached 有什么区别?Redis 的线程模型是什么?为什么单线程的 Redis 比多线程的 Memcached 效率要高得多?

    面试题 redis 和 memcached 有什么区别?redis 的线程模型是什么?为什么 redis 单线程却能支撑高并发? 面试官心理分析 这个是问 redis 的时候,最基本的问题吧,redi ...

  3. windows编译openssl(64位)一游

    编译openssl,一套标准流程: (环境:  win10 64位os, vs2019) 需要的工具:perl     nasm   openssl源码包 1  安装perl 2  下载nasm,将n ...

  4. 「APIO2015」巴邻旁之桥 Palembang Bridges

    贪心 先转化一下题意 首先如果一个人的家和办公室在河同一侧那么建桥的时候不用去考虑它,最终把答案加上即可 在河两侧的家和办公室互换不影响答案,那么可以把这个抽象到一个区间$[l,r]$,距离就是$|l ...

  5. Java_基础(一)

    注释 单行注释: // 多行注释: /*开头, */结尾, 可跨行, 可嵌入 public static void main(String[] args/* 哈哈 */) 文档注释: /** 开头, ...

  6. Android Studio导入github项目源码步骤

    1.从github上将源码下载下来 2.打开AS,新建一个新项目(我选择了EmptyActivity) 3.先不要在AS 中打开源码,来整理源码 在源码的目录下面,将project下的build.gr ...

  7. php之简单工厂模式

    <?php /** * Created by PhpStorm. * User: 小狗蛋儿 * Date: 2017/11/13 * Time: 22:21 */ abstract class ...

  8. leetcode75:search-a-2d-matrix

    题目描述 请写出一个高效的在m*n矩阵中判断目标值是否存在的算法,矩阵具有如下特征: 每一行的数字都从左到右排序 每一行的第一个数字都比上一行最后一个数字大 例如: 对于下面的矩阵: [ [1, 3, ...

  9. codeforces 1425E,一万种情况的简单题

    大家好,欢迎阅读codeforces专题. 我们今天选中的是codeforces 1425场比赛的E题,这是一场印尼多校联合的ICPC的练习赛.ACM赛制,难度也比较近似.我们今天选择的是其中的一道M ...

  10. Sublime Text 3 安装插件与快捷键总结

    ublime Text 3 是一个了不起的软件.首先,它是一个干净,实用,可以快速的编写代码编辑器.它不仅具有令人难以置信的内置功能(多行编辑和VIM模式),而且还支持插件,代码片段和其他许多东西.很 ...