微服务架构中,根据业务划分成若干个服务,各单元应用间通过服务注册与订阅的方式互相依赖,依赖通过远程调用的方式执行,该方式难以避免因网络或自身原因而出现故障或者延迟,从而并不能保证服务的100%可用,此时若有大量的网络涌入,会形成任务累计,导致服务瘫痪,甚至导致服务“雪崩”。

- Hystrix

1.Netflix 已经为我们创建了 Hystrix 库来实现服务降级、服务熔断、线程隔离、请求缓存、请求合并以及服务监控(Hystrix Dashboard)等强大功能,在微服务架构中,多层服务调用是非常常见的。

正常情况

2.较底层的服务中的服务故障可能导致级联故障,当对特定的服务的调用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开,故障百分比大于circuitBreaker.errorThresholdPercentage(默认值:> 50%)时metrics.rollingStats.timeInMilliseconds(默认10秒),断路打开后,开发人员可以回退机制。

异常情况

官方文档:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_circuit_breaker_hystrix_clients

- 准备工作

1.启动Consul
2.创建 battcn-provider 和 battcn-consumer

- battcn-provider

- pom.xml

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

- ProviderApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ProviderApplication { @Value("${spring.application.name}")
String applicationName; public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
} @GetMapping("/hello")
public String hello() {
return "My Name's :" + applicationName + " Email:1837307557@qq.com";
}
}

- bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8765 spring:
application:
name: battcn-provider
cloud:
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
prefer-ip-address: true

- battcn-consumer

- pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependencies>
<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-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

- ConsumerApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//@EnableDiscoveryClient
//@SpringBootApplication
//@EnableCircuitBreaker
/**
* SpringCloudApplication 一个注解顶上面三个,有兴趣的可以点进去看源码
*/
@SpringCloudApplication
public class ConsumerApplication { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}

- HiController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 为了偷懒,就写一个文件了
*/
@RestController
public class HiController { static Logger LOGGER = LoggerFactory.getLogger(HiController.class);
@Autowired
HiService hiService; @GetMapping("/hi")
public String hi() {
return hiService.hello();
} @Service
class HiService {
@Autowired
RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "ribbonFallback")
public String hello() {
return restTemplate.getForObject("http://battcn-provider/hello", String.class);
} public String ribbonFallback() {
return "My Name's :ribbonFallback Email:1837307557@qq.com";
}
}
}

- bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8766 spring:
application:
name: battcn-consumer
cloud:
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
prefer-ip-address: true

- 测试

启动:battcn-provider

启动:battcn-consumer

访问:http://localhost:8500/ 显示如下代表服务注册成功

查看注册中心

访问:http://localhost:8766/hi

1
2
3
My Name's :battcn-provider Email:1837307557@qq.com	#正确情况

My Name's :ribbonFallback Email:1837307557@qq.com	#关闭battcn-provider后结果

- 源码

1.当我们开启Hystrix 的时候 Hystrix 会为我们注入 HystrixCommandAspect 切面,操作所有带HystrixCommand 注解,随后就是通过反射与Cglib创建代理然后发送请求,不管服务是否健壮都会先进入AOP切面然后才会执行后续操作(打脸轻点…)

源码

- 监控

1.在 ConsumerApplication 中添加 @EnableHystrixDashboard 的注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@SpringCloudApplication
@EnableHystrixDashboard //多了个开启监控注解
public class ConsumerApplication { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
} }

2.在 pom.xml 中添加如下配置

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

3.访问:http://localhost:8766/hystrix

效果图

4.访问N次:http://localhost:8766/hi

效果图

我们可以看到请求成功,失败,等信息

断路器Hystrix(Ribbon)的更多相关文章

  1. SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)

    前言 本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识. SpringCloud Hystrix Hystrix 介绍 Netfl ...

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

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

  3. SpringCloud IDEA 教学 (四) 断路器(Hystrix)

    写在开始 在SpringCloud项目中,服务之间相互调用(RPC Remote Procedure Call —远程过程调用),处于调用链路底层的服务产生不可用情况时,请求会产生堆积使得服务器线程阻 ...

  4. SpringCloud断路器(Hystrix)和服务降级案列

    断路器(Hystrix) 为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自 ...

  5. 【Spring Cloud学习之六】断路器-Hystrix

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 一.服务雪崩1.什么是服务雪崩分布式系统中经常会出现某个基础服务不可用造成整个系统不 ...

  6. 断路器Hystrix(Feign)

    上一篇中我们讲了 断路器Hystrix(Ribbon) 本章讲解Feign+Hystrix已经Request请求传递,各种奇淫技巧…. - Hystrix Hystrix支持回退概念:当 断路器 打开 ...

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

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

  8. 004声明式服务调用Feign & 断路器Hystrix

    1.POM配置 和普通Spring Boot工程相比,添加了Eureka Client.Feign.Hystrix依赖和Spring Cloud依赖管理 <dependencies> &l ...

  9. 断路器Hystrix与Turbine集群监控-Spring Cloud学习第三天(非原创)

    文章大纲 一.Hystrix基础介绍二.断路器Hystrix简单使用三.自定义Hystrix请求命令四.Hystrix的服务降级与异常处理五.Hystrix的请求缓存与请求合并六.Hystrix仪表盘 ...

  10. 断路器-Hystrix的深入了解

    前言 高可用相关的技术以及架构,对于大型复杂的分布式系统,是非常重要的.而高可用架构中,非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用的服务,从而足以应对分布式系统环境中的各种各样的 ...

随机推荐

  1. (Java实现)蓝桥杯Excel地址

    历届试题 Excel地址 原题地址 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 Excel单元格的地址表示很有趣,它使用字母来表示列号. 比如, A表示第1列, B表示第2列, ...

  2. Java实现 LeetCode 86 分割链表

    86. 分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1 ...

  3. Java中TreeSet的详细用法

    第1部分 TreeSet介绍 TreeSet简介 TreeSet 是一个有序的集合,它的作用是提供有序的Set集合.它继承于AbstractSet抽象类,实现了NavigableSet, Clonea ...

  4. Java实现 蓝桥杯 历届真题 数字拆分

    正整数可以表示为若干正整数的累加和. 如,对于正整数n=6,可以分划为: 5+1 4+2 4+1+1 3+3 3+2+1 3+1+1+1 2+2+2 2+2+1+1 2+1+1+1+1 1+1+1+1 ...

  5. java实现 蓝桥杯 算法训练 操作格子

    问题描述 有n个格子,从左到右放成一排,编号为1-n. 共有m次操作,有3种操作类型: 1.修改一个格子的权值, 2.求连续一段格子权值和, 3.求连续一段格子的最大值. 对于每个2.3操作输出你所求 ...

  6. java实现人民币金额大写

    在与财务相关的应用中,经常会用到人民币金额的大写,比如发票的打印程序. 本题的任务是:从键盘输入一个十亿以内的正整数(int类型),把它转换为人民币金额大写(不考虑用户输入错误的情况). 比如,用户输 ...

  7. 关于Synchronized的偏向锁,轻量级锁,重量级锁,锁升级过程,自旋优化,你该了解这些

    前言 相信大部分开发人员,或多或少都看过或写过并发编程的代码.并发关键字除了Synchronized(如有不懂请移至传送门,关于Synchronized的偏向锁,轻量级锁,重量级锁,锁升级过程,自旋优 ...

  8. Sublime Text 3.1 注册码

    加入到hosts文件: 127.0.0.1 www.sublimetext.com 127.0.0.1 license.sublimehq.com hosts 文件的位置: Windows : c:/ ...

  9. Excel 中将大于511的十进制数转换成二进制

    If number < -512 or if number > 511, DEC2BIN returns the #NUM! error value. 所以不能直接使用DEC2BIN函数. ...

  10. 纯 HTML/CSS 高仿 Win10 加载动画

    自己做的超高仿Win10加载动画(应该是全网最像的 HTML 实现了),自己想用就拿去用吧 转圈加载 在线演示 HTML: <div class="loading"> ...