微服务架构中,根据业务划分成若干个服务,各单元应用间通过服务注册与订阅的方式互相依赖,依赖通过远程调用的方式执行,该方式难以避免因网络或自身原因而出现故障或者延迟,从而并不能保证服务的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实现 LeetCode 710 黑名单中的随机数(黑白名单)

    710. 黑名单中的随机数 给定一个包含 [0,n ) 中独特的整数的黑名单 B,写一个函数从 [ 0,n ) 中返回一个不在 B 中的随机整数. 对它进行优化使其尽量少调用系统方法 Math.ran ...

  2. (Java实现) 活动选择

    活动选择的类似问题都可以这么写 import java.util.ArrayList; public class huodongxuanze { /** * //算法导论中活动选择问题动态规划求解 * ...

  3. Java实现 蓝桥杯VIP 算法提高 研究兔子的土豪

    试题 算法提高 研究兔子的土豪 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 某天,HWD老师开始研究兔子,因为他是个土豪 ,所以他居然一下子买了一个可以容纳10^18代兔子的巨大 ...

  4. Java实现 蓝桥杯 算法训练 纪念品分组

    问题描述 元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作.为使得参加晚会的同学所获得的纪念品价值 相对均衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品,并且每组纪念品的价 ...

  5. Java实现 蓝桥杯 算法训练 数据交换

    试题 算法训练 数据交换 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 编写一个程序,输入两个整数,分别存放在变量x和y当中,然后使用自己定义的函数swap来交换这两个变量的值. ...

  6. Java实现无向图的欧拉回路判断问题

    1 问题描述 Problem Description 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路? Input 测试输入包含若干测试 ...

  7. Dedecms升级php版本{dede:field.body/}不解析,文章内容不显示

    Dedecms升级php7后发布文章后,发现前端显示的文章内容都是空白,只能显示标题.关键词.描述等. 第一种方法: 把{dede:field.body /}删除,使用 下面的sql 标签代码替换: ...

  8. Edge浏览器现已支持Tampermonkey(油猴)

    Tampermonkey,Greasemonkey,这种扩展可以让我们的浏览器自动运行我们自己定义的脚本,然后就出现了相关网站(比如https://greasyfork.org/zh-CN)让大家在上 ...

  9. Sequence in the Pocket【思维+规律】

    Sequence in the Pocket 题目链接(点击) DreamGrid has just found an integer sequence  in his right pocket. A ...

  10. Oracle连接Db2

    因为有个业务场景需要访问客户DB2数据库的数据,我们使用的Oracle,百度一下是有方法的,本来以为很简单,没想到搞了一天,因为数据库版本都太老,使用的也少走了不少弯路,在此记录下来给需要的人借鉴. ...