Spring Cloud之Hystrix
在微服务架构中,存在那么多的服务单元,若一个单元出现故障(由于网络原因或者自身原因),就很容易因依赖关系而引发故障的蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构更加不稳定。为了解决这样的问题,产生了断路器等一系列的服务保护机制。(A服务调用B服务,B服务由于自身处理逻辑等原因造成响应缓慢,会导致A服务线程被挂起,以等待B服务执行,在高并发情况下,这些挂起的线程会导致后面调用A服务的请求被阻塞,最终导致A服务也不可用)。
加入断路器后,当服务不可用时,通过断路器的故障监控,会直接执行回调函数,直接返回一串字符串,而不是等待响应超时,这样就不会使得线程调用故障服务被长时间占用不释放,从而避免了故障在分布式系统中的蔓延。
本节内容在上节内容基础上,阅读此节之前,先看上节Spring Cloud之Eureka、Ribbon
一、无断路器示例
启动上节的eureka-server、service-hello(8081/8082)、ribbon-consumer工程
在未加入断路器之前,关闭8081实例,发送GET请求到http://localhost:9000/ribbon-consumer,轮询8081/8082,当轮询到8081后(因为8081实例被关)会得到下面输出:
二、加入断路器示例
在ribbon-consumer工程的pom.xml引入下面依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency>
在ribbon-consumer工程的 主类ConsumerApplication中使用@EnableCircuitBreaker注解开启断路器功能
package com.stonegeek;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;/*** Created by StoneGeek on 2018/5/28.* 博客地址:http://www.cnblogs.com/sxkgeek*/ @EnableCircuitBreaker @EnableDiscoveryClient @SpringBootApplication public class RibbonConsumerApplication {@Bean@LoadBalancedRestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(RibbonConsumerApplication.class, args);} }
改造服务消费方式,新增HelloService类,注入RestTemplate实例,然后将在ConsumerController中对RestTemplate的使用迁移到helloService函数中,最后,在helloService函数上增加@HystrixCommand注解来指定回调方法:
package com.stonegeek.service;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;import java.util.logging.Logger;/*** Created by StoneGeek on 2018/5/29.* 博客地址:http://www.cnblogs.com/sxkgeek*/ @Service public class HelloService {private final Logger logger =Logger.getLogger(String.valueOf(getClass()));@AutowiredRestTemplate restTemplate;@HystrixCommand(fallbackMethod = "helloFallback")public String helloService(){//加logger更清晰的看出执行时间long start =System.currentTimeMillis();String result=restTemplate.getForEntity("http://SERVICE-HELLO/hello",String.class).getBody();long end=System.currentTimeMillis();logger.info("Spend time:"+(end-start));return result;}public String helloFallback(){return "error";}}
修改RibbonConsumerApplication.class
package com.stonegeek.controller;import com.stonegeek.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;/*** Created by StoneGeek on 2018/5/28.* 博客地址:http://www.cnblogs.com/sxkgeek*/ @RestController public class ConsumerController {/*** @Author: StoneGeek* @Date: 2018/5/29* @Description:之前不加断路器代码*/ // @Autowired // RestTemplate restTemplate; // // @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET) // public String helloConsumer(){ // return restTemplate.getForEntity("http://SERVICE-HELLO/hello",String.class).getBody(); // }/*** @Author: StoneGeek* @Date: 2018/5/29* @Description: 加了断路器代码*/@AutowiredHelloService helloService;@RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)public String helloConsumer(){return helloService.helloService();}}
此时重新来验证一下断路器实现的服务回调逻辑,此时断开8081实例,当服务消费者轮询到8081时,不再是之前的错误内容,Hystrix服务回调生效
三、模拟服务阻塞来验证断路器回调
Hystrix默认超时时间是2000毫秒
我们对service-hello的/hello接口做一些修改(重点是Thread.sleep()函数的使用):
package com.stonegeek.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.logging.Logger;/*** Created by StoneGeek on 2018/5/27.* 博客地址:http://www.cnblogs.com/sxkgeek*/ @RestController public class HelloController {private final Logger logger =Logger.getLogger(String.valueOf(getClass()));@Autowiredprivate DiscoveryClient client;/*** @Author: StoneGeek* @Date: 2018/5/29 * @Description:不加线服务塞的代码*/ // @RequestMapping(value = "hello",method = RequestMethod.GET) // public String index(){ // ServiceInstance instance=client.getLocalServiceInstance(); // logger.info("/hello, host:"+instance.getHost()+", service_id:"+instance.getServiceId()+"service_port:"+instance.getPort()); // return "hello world "; // }/*** @Author: StoneGeek* @Date: 2018/5/29* @Description: 加了Thread.sleep(3000)的服务阻塞代码,由于Hystrix默认超时时间为2000毫秒,* 所以这里采用了0至3000的随机数以让处理过程有一定概率发生超时来触发断路器*/@RequestMapping(value = "/hello",method = RequestMethod.GET)public String index() throws InterruptedException {ServiceInstance instance=client.getLocalServiceInstance();int sleepTime=new Random().nextInt(3000);logger.info("sleepTime:"+sleepTime);Thread.sleep(sleepTime);logger.info("sleepTime:"+sleepTime+"/hello, host:"+instance.getHost()+", service_id:"+instance.getServiceId()+"service_port:"+instance.getPort());return "hello world ";} }
重新启动service-hello和ribbon-consumer模块,连续访问http://localhost:9000/ribbon-consumer几次,当RIBBON-CONSUMER的控制台输出的Spend time大于2000的时候,网页就会返回error,即服务消费者因调用的服务超时从而触发熔断请求,并调用回调逻辑返回结果
此时Spring Could Hystrix的断路器就配置完成了!!!
转载于:https://www.cnblogs.com/sxkgeek/p/9105951.html
Spring Cloud之Hystrix相关推荐
- Spring Cloud中Hystrix仪表盘与Turbine集群监控
Hystrix仪表盘,就像汽车的仪表盘实时显示汽车的各项数据一样,Hystrix仪表盘主要用来监控Hystrix的实时运行状态,通过它我们可以看到Hystrix的各项指标信息,从而快速发现系统中存在的 ...
- Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失(续)
前言 上篇文章<Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失>我们对ThreadLocal数据丢失进行了详细的分析,并通过代码的方式复现了这个问题. ...
- Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?
导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...
- Spring Cloud之Hystrix服务容错
Spring Cloud之Hystrix服务容错 Hystrix的概述 Hystrix的使用 相关依赖 Eureka注册中心 服务提供者 服务消费者 执行测试 @HystrixCommand详解 服务 ...
- Spring Cloud中Hystrix的请求合并
在微服务架构中,我们将一个项目拆分成很多个独立的模块,这些独立的模块通过远程调用来互相配合工作,但是,在高并发情况下,通信次数的增加会导致总的通信时间增加,同时,线程池的资源也是有限的,高并发环境会导 ...
- Spring Cloud中Hystrix仪表盘与Turbine集群监控 1
Hystrix仪表盘,就像汽车的仪表盘实时显示汽车的各项数据一样,Hystrix仪表盘主要用来监控Hystrix的实时运行状态,通过它我们可以看到Hystrix的各项指标信息,从而快速发现系统中存在的 ...
- Spring Cloud Netfix Hystrix(断路器)
一.灾难性雪崩 造成灾难性雪崩效应的原因,可以简单归结为下述三种: 服务提供者(Application Service)不可用.如:硬件故障.程序BUG.缓存击穿.并发请求量过大等. 重试加大流量.如 ...
- Spring Cloud 之 Hystrix
Spring Cloud 之 Hystrix 1. Feign+Hystrix实现RPC调用保护 2. Spring Cloud Hystrix失败回退 3. Spring Cloud Hystrix ...
- spring cloud使用hystrix实现断路保护机制
断路保护机制:即容错性,在微服务架构中,系统之间都是相互依赖的,如果一个系统出现了异常,就会出现如下情况: 断路保护机制就是为了解决这种情况. hystrix的大致原理如下: spring cloud ...
最新文章
- VMware vMotion 配置要求
- 没有工作怎么申请贷款?
- ios android 交互 区别,很多人不承认:iOS的返回交互,对比Android就是反人类。
- 主机不支持php5.4,GoDaddy Linux主机不再支持PHP5.3版本 | Godaddy美国主机中文指南
- link url下载php,php脚本生成google play url的下载链接,下载apk并自动反编译后获取android版本号...
- angular2 学习笔记 ( unit test 单元测试 )
- 深度神经网络基本问题的原理详细分析和推导
- 乌鲁木齐大雾弥漫 局地能见度不足200米
- 鸿蒙官方编辑器 DevEco Studio 2.0.12.201使用流程
- DW-办公自动化02(Excel)
- markdown模板(个人使用)
- GitHub 上最火的开源项目 —— Java 篇
- 「3D视觉技术交流群」精华帖与关键问题
- 3GPP中的各种缩写
- 浏览器怎么打开本地jsp文件
- 退出华为的时候,我在想些什么
- 只有年轻人才能做数据分析师吗?
- linux nginx 内存占用,nginx内存占用过高
- 理解Servlet和Servlet容器、Web容器等概念
- 首次试用 NeoSwiff ,用C#开发FLASH版的多国语言翻译
热门文章
- 【转】G40-70、G50-70联想小新笔记本SR1000随机Linux改Windows 7系统操作指导
- CSS 解决td里面内容太多把表格弄变形的原因,设置 自动换行。
- 使用Azure portal Create Virtual Machine
- scanf printf sprintf fprintf
- js编程思路--给网站定义一个全局的js对象,放到window对象中
- oracle 大页配置,ORACLE 启用大页内存
- 绝地求生 android版支持蓝牙吗,绝地求生怎么蓝牙耳机设置听脚步 | 手游网游页游攻略大全...
- 时间同步软件 windows_电脑上好用的便利贴软件,PC端便签软件
- java 添加联系人,java – 如何将联系人添加到组android
- java 自定义注解_两步实现Java自定义注解