Spring cloud微服务 Hystrix熔断器
以下demo代码:https://github.com/wades2/HystrixtDemo
官网定义:Hystrix是一个延迟容错库。在分布式环境中,许多服务依赖项中的一些不可避免地会失败。Hystrix是一个库,可通过添加延迟容错和容错逻辑来帮助您控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点,阻止它们之间的级联故障以及提供后备选项来实现这一点,所有这些都可以提高系统的整体弹性。
因为在分布式系统中很多服务是相互依赖的,假如以下某个功能实现依赖于几个服务:
假如此时【家庭信息查询服务】由于请求过多或者服务器原因出现了大批量服务不可用,【个人信息查询服务】由于无法请求到【家庭信息查询服务】,一直阻塞,造成服务器压力越来越大,后续又有【人员信息查询】的请求来了。就好比堵车的公路上又来了一大批车;此时会每个依赖关系都会在某些时候不可避免地失败。如果主机应用程序未与这些外部故障隔离,则整个系统可能会崩溃。
此时Hystrix熔断器就发挥了作用:
1、通过第三方客户端库访问(通常通过网络)依赖关系,以防止和控制延迟和故障。
2、在复杂的分布式系统中停止级联故障。(在一段时间内侦测到许多类似的错误,会强迫其以后的多个调用快速失败,不再访问远程服务器)
3、快速失败并迅速恢复。(快速失败后【个人信息查询服务】不会由于阻塞过载,就不会导致雪崩,)
4、在可能的情况下,后退并优雅的降级。
5、实现近实时监控,警报和操作控制。(如果熔断器诊断【家庭信息查询服务】错误已经修正,应用程序会再次尝试调用这个服务。)
这样我们就在之前学习Euraka+ribbon的基础上加熔断机制:
首先在我们的EurekaCaller里面添加上需要的依赖:
【注:如果不清楚的朋友或者没有重头看的朋友,这里附上我demo的
git地址:https://github.com/wades2/EurekaDemo2,
完整的搭建教程在:https://blog.csdn.net/asd529735325/article/details/85044158,
如果你的Eureka和ribbon已经搭建好了可以直接跳过。】
我们的项目基本是这个样子的:
Eurekacaller是这个样子:
添加pom依赖引入需要的jar包:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency>
重写一下我们的Controller:
package com.example.demo.controller;import com.example.demo.service.Services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;@RestController
public class Controller {@Autowiredprivate Services service;@RequestMapping(value = "getUser/routine",method = RequestMethod.GET)public String routine(@RequestParam String id){String json=service.routineService(id);return json;}
}
然后是我们具体的service,负载的部分,我们的断熔就放在service这里,一旦请求超时或者失败,立即失败并触发降级方法,我这里的降级方法是:callback。
package com.example.demo.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.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;@Service
public class Services {@Autowiredprivate RestTemplate restTemplate;//实现的callback方法来实现短路保护@HystrixCommand(fallbackMethod = "callback")public String routineService(String id){System.out.println(id);String jsons = restTemplate.getForObject("http://Eureka-client/user/getUser2", String.class);return jsons;}public String callback(String id){String msg="出现错误id:"+id;return msg;}}
因为在前期我们没有扫描/user/getUser2所在的包路径,所以这个是肯定扫描不到的,访问一定是404not found的。
然后启动我们的5个启动类,访问http://localhost:8086/getUser/routine?id=52,我们可以看到访问到了callback页面:
这样一来,即使EurkaClient都出现了问题或者过载,我们也可以降级处理。那我们再试试如果超时会怎么样。
修改配置文件。,添加超时设置:
spring.application.name=Eureka_caller
server.port=8086
eureka.client.serviceUrl.defaultZone=http://user:123456@localhost:8083/eureka,http://user:123456@localhost:8082/eureka#hystrix配置
hystrix.metrics.enabled=true
#默认的超时时间设置
hystrix.metrics.polling-interval-ms=2000
修改下Ribbon请求的路径:
//实现的callback方法来实现短路保护@HystrixCommand(fallbackMethod = "callback")public String routineService(String id){System.out.println(id);String jsons = restTemplate.getForObject("http://Eureka-client/user/getUser", String.class);return jsons;}
修改对应Service提供者的Client具体实现类(也就是修改EurekaClient和EurekaClient2),使其沉睡时间大于我们的熔断请求延迟时间,模拟服务器过载阻塞:
package com.example.demo.controller;import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.converters.Auto;
import com.netflix.discovery.shared.Applications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("user")
public class UserController {@Autowiredprivate EurekaClient eurekaClients;@GetMapping("getUser")public String getUser() {//todo 得到eureka server的服务实例InstanceInfo info=eurekaClients.getNextServerFromEureka("Eureka-client",false);try{Thread.sleep(60000);}catch (Exception e){e.printStackTrace();}return "hello one";}}
我们看到请求后依然页面被拦截熔断了,因为等待时间60000毫秒远远大于了我们设置熔断等待时间:2000毫秒。
以上就是我们关于Hystrix熔断器的学习,各位看官大佬爷有什么问题请留言区评论,大家一起讨论解决。
Spring cloud微服务 Hystrix熔断器相关推荐
- 实战系列-Spring Cloud微服务中三把利器Feign、Hystrix、Ribbon
导语 在之前的分享中分享过关于Fegin的底层实现原理,以及Spring Cloud OpenFegin的启动原理.在这次的分享中主要总结一下Spring Cloud 微服务架构的三把利器.对于F ...
- spring cloud微服务治理eureka、hystrix、zuul代码例子
spring cloud微服务中台服务代码例子,包括eureka.hystrix.zuul https://github.com/birdstudiocn/spring-cloud-sample/tr ...
- spring cloud微服务分布式云架构 - Spring Cloud集成项目简介
Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Clo ...
- Spring Cloud微服务分布式云架构—集成项目简介
Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Clo ...
- Spring Cloud微服务之Feign——声明式服务间调用
目录 写在前面 服务间调用常见的方案 方案一:HttpClient 方案二:RestTemplate 方案三:Feign 框架版本问题及可能遇到的坑 搭建服务注册中心 搭建服务1--service-a ...
- Spring Cloud 微服务技术栈
Spring Cloud 简介 主要内容 微服务简介 SpringCloud 简介 SpringCloud 框架结构 SpringCloud 和 Dubbo 的对比 SpringCloud 版本号说明 ...
- Spring Cloud 微服务讲义
Spring Cloud 微服务讲义 第一部分 微服务架构 第 1 节 互联网应用架构演进 第 2 节 微服务架构体现的思想及优缺点 第 3 节 微服务架构中的核心概念 第二部分 Spring Clo ...
- spring cloud微服务分布式云架构 - Spring Cloud集成项目简介(三)
点击上面 免费订阅本账号! 本文作者:it菲菲 原文:https://yq.aliyun.com/articles/672242 点击阅读全文前往 Spring Cloud集成项目有很多,下面我们列举 ...
- (二)spring cloud微服务分布式云架构 - 整合企业架构的技术点
spring cloud本身提供的组件就很多,但我们需要按照企业的业务模式来定制企业所需要的通用架构,那我们现在需要考虑使用哪些技术呢? 下面我针对于spring cloud微服务分布式云架构做了以下 ...
最新文章
- 无脑博士的试管们java_计蒜客 无脑博士和他的试管们
- Linux驱动编程 step-by-step (五)主要的文件操作方法实现
- 【Qt】Qt工程管理
- SMG12232A2标准图形点阵型液晶显示模块的演示程序[C51编程语言]
- python socket udp并发_Python进阶----UDP协议使用socket通信,socketserver模块实现并发
- [剑指offer]面试题第[1]题[JAVA][二维数组中的查找][数组][二分]
- 在cygwin下安装CRF++
- 进程隐藏的各种方法 以及分析比较以及实现链接
- OpenMV 从入手到跑TensorFlow Lite神经网络进行垃圾分类
- 第一篇自用博客:git的操作(防忘记用的hh)封面是我最爱的歌手ikura强推她的每一首歌,简直就是行走的唱片啊有木有!
- js阿拉伯数字转中文数字
- 1.2、从“生日贺卡”看C++字符串的输入和输出
- oracle ORA-28000: the account is locked 28000. 00000
- 在win11系统下安装,配置Java17
- 网络舆情信息查找网站与怎么查的方法详解
- 基于黄金正弦与自适应融合的蜉蝣优化算法
- python实现非对称加密算法_Python使用rsa模块实现非对称加密与解密
- 阿布扎比将于2023年5月举办年度投资会议
- 计算机表演赛学前组游戏,第十七届计算机表演赛总决赛获奖名单公布
- cmos逻辑门传输延迟时间_用DSLogic 逻辑分析仪 解析出来的IIC协议太完美了!