Hystrix熔断器

  • 一、Hystrix概念
  • 二、feign结合Hystrix使用
    • 1、在service的pom中添加依赖
    • 2、在service-user配置文件中添加hystrix配置
    • 3、在service-user的client包里面创建熔断器的实现类
    • 4、修改FileClient接口的注解
    • 5、测试熔断器效果

一、Hystrix概念

Hystrix 是一个供分布式系统使用,提供延迟和容错功能,保证复杂的分布系统在面临不可避免的失败时,仍能有其弹性。

比如系统中有很多服务,当某些服务不稳定的时候,使用这些服务的用户线程将会阻塞,如果没有隔离机制,系统随时就有可能会挂掉,从而带来很大的风险。SpringCloud使用Hystrix组件提供断路器、资源隔离与自我修复功能。

二、feign结合Hystrix使用

改造service-user模块

1、在service的pom中添加依赖

<!--Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency><!--Netflix hystrix实现断路器 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2、在service-user配置文件中添加hystrix配置

#开启熔断机制
feign.hystrix.enabled=true
# 设置hystrix超时时间,默认1000ms,默认:(方法上记得要加上@HystrixCommand,否则无效):
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

3、在service-user的client包里面创建熔断器的实现类

@Component //交给spring管理
public class CrmFileFeignClient implements FileClient{@Overridepublic ResultBean removeCrmFileById(Integer id) {return ResultBean.error().message("连接超时......");}
}

4、修改FileClient接口的注解

//@FeignClient("service-file")   //@FeignClient注解用于指定从哪个服务中调用功能 ,名称与被调用的服务名保持一致。
@FeignClient(name = "service-file",fallback = CrmFileFeignClient.class)
@Component  //交给spring管理
public interface FileClient {//根据文件 url 删除源文件@PostMapping(value = "/fileservice/crm-file/removeCrmFileById/{id}")  //这个路径映射,是全路径映射public ResultBean removeCrmFileById(@PathVariable("id") Integer id); //@PathVariable注解一定要指定参数名称,否则出错
}

5、测试熔断器效果

在服务调用方法里面添加返回值,打印输出

 ResultBean resultBean = fileClient.removeCrmFileById(id);if(resultBean.getCode() == 2001){System.out.println("hystrix启动,删除失败!");}


我们只启动service_user模块,service_file模块不启动,先创建一条数据,和之前一样再测试这个接口。

控制台日志输出

开始调用...
2020-05-15 19:39:04.545  INFO 11532 --- [-service-file-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: service-file.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2020-05-15 19:39:04.590  INFO 11532 --- [-service-file-1] c.netflix.loadbalancer.BaseLoadBalancer  : Client: service-file instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=service-file,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2020-05-15 19:39:04.597  INFO 11532 --- [-service-file-1] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2020-05-15 19:39:04.602  INFO 11532 --- [-service-file-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client service-file initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=service-file,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:org.springframework.cloud.alibaba.nacos.ribbon.NacosServerList@9d9d7fb
hystrix启动,删除失败!
调用结束...

Spring Cloud微服务之Hystrix服务熔断(十二)相关推荐

  1. spring cloud之Turbine断路器聚合监控(十二)

    一.博客背景 上一章讲解了针对一个微服务的断路器监控,但是微服务通常会是多个实例组成的一个集群. 倘若集群里的实例比较多,难道要挨个挨个去监控这些实例吗? 何况有时候,根据集群的需要,会动态增加或者减 ...

  2. Spring Cloud 系列之 Netflix Zuul 服务网关(三)

    本篇文章为系列文章,未读前几集的同学请猛戳这里: Spring Cloud 系列之 Netflix Zuul 服务网关(一) Spring Cloud 系列之 Netflix Zuul 服务网关(二) ...

  3. 实战系列-Spring Cloud微服务中三把利器Feign、Hystrix、Ribbon

    导语   在之前的分享中分享过关于Fegin的底层实现原理,以及Spring Cloud OpenFegin的启动原理.在这次的分享中主要总结一下Spring Cloud 微服务架构的三把利器.对于F ...

  4. spring cloud微服务治理eureka、hystrix、zuul代码例子

    spring cloud微服务中台服务代码例子,包括eureka.hystrix.zuul https://github.com/birdstudiocn/spring-cloud-sample/tr ...

  5. 一张图带你了解 Spring Cloud 微服务架构!

    点击上方"搜云库技术团队",选择"设为星标" 回复"1024"或"面试题"获取4T学习资料 Feign Eureka R ...

  6. 一张图了解 Spring Cloud 微服务架构

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:http://rrd.me/epgWJ Feign Eure ...

  7. spring cloud微服务分布式云架构 - Spring Cloud集成项目简介

    Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Clo ...

  8. Spring Cloud 微服务入门(二)--Spring Cloud 架构

    Spring Cloud整体核心架构:Rest服务,在Spring Cloud配置过程中,都是遵循Rest风格规范,在Rest处理中,必不可少两个对象端:服务的提供者(provider)和服务消费者( ...

  9. Spring Cloud微服务分布式云架构—集成项目简介

    Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Clo ...

  10. 这样的 Spring Cloud 微服务项目太牛了!

    最近有个学弟问我:为什么去面试的时候都要问一些微服务的问题?我听到时候也有点惊讶微服务器架构已经深入到各个企业,尽管有些企业不大,但他们也有微服务的想法,毕竟互联网公司发展快,真要到了爆发期如果现有的 ...

最新文章

  1. Flex使用cookie保存登状态
  2. 【博客搬家旧文】剑指offer [ java ] 面试题10 斐波那契数列
  3. 争分夺秒:阿里实时大数据技术全力助战双11
  4. 前端笔记-css实现web自适应(当像素小于xx时隐藏此div)
  5. 地推HTTP成长介绍
  6. 【通过操作指针,与指针做函数參数#39;实现字串在主串中出现的次数,然后将出现的部分依照要求进行替换 】...
  7. centos7 安装ftp服务
  8. 使用数据集(DataSet)、数据表(DataTable)、集合(Collection)传递数据
  9. matlab 连续相同数据库,MATLAB向量:防止来自相同范围的连续值
  10. spring-第五篇之spring容器中的bean
  11. 知产新观察 | 商家要敢于向知产流氓及恶意投诉说不!
  12. adc分辨率和精度的区别_STM32学习笔记—ADC采集数据常见问题
  13. 微信8.0自动发送炸弹python脚本
  14. 数据结构算法—非递归算法求二叉树的叶子结点(C语言)
  15. 利用Python,通过关键字获取漏洞平台最新漏洞信息
  16. 次世代3D建模高低模的搭配方式你知道吗?
  17. 从零开始学Java编程!南通java招聘
  18. elment-ui的el-select选择器blur事件失效 bug解决
  19. 贪官产生的本质是什么——谈谈人性与制度的博弈未来
  20. 云计算实验室建设方案分享

热门文章

  1. linux设置切换窗口特效,Linux_在Ubuntu下启用亮丽的透明玻璃窗口特效,本文将采用图文并茂的形式向 - phpStudy...
  2. 信息学奥赛一本通 1090:含k个3的数 | OpenJudge NOI 1.5 30
  3. 信息学奥数一本通(1004:字符三角形)
  4. 图论 —— 二分图 —— 匈牙利算法
  5. 与圆相关的计算(信息学奥赛一本通-T1014)
  6. 信息学奥赛一本通C++语言——1080:余数相同问题
  7. python 数据结构包_python 中numpy科学计算工具包——基础数据结构
  8. STM32那点事(3)_中断(下)
  9. 让老照片重现光彩(一):Bringing Old Photos Back to Life
  10. [材料力学]弯扭组合梁实验报告