springcloud学习04- 断路器Spring Cloud Netflix Hystrix
依赖上个博客:https://www.cnblogs.com/wang-liang-blogs/p/12072423.html
1.断路器存在的原因
引用博客 https://blog.csdn.net/zhou199252/article/details/80745151 的说明
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。为了解决这个问题,业界提出了断路器模型。
2.在项目中使用断路器
2.1.在eureka-customer的pom文件中加入相关的jar包
<!--断路器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
2.2.在eureka-customer/EurekaCustomerApplication.java中增加@EnableHystrix注解表示开启断路器
package com.springcloud.eurekacustomer; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class EurekaCustomerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaCustomerApplication.class, args);
} @Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
} }
2.3.在service中加入断路器所需的fallback方法
package com.springcloud.eurekacustomer; 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; @Service
public class HelloService { @Autowired
RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "helloError")
public String helloService(){
String rltStr = restTemplate.getForObject("http://server-provider/hello",String.class);
rltStr = rltStr + " form service-provider by service-customer.";
return rltStr;
} public String helloError(){
return "eureka-provider dowm";
} }
2.4.用postman测试
关闭eureka-provider的服务,用postman调用eureka的接口localhost:8887/cushello,结果如下:

这时候就会不调用服务eureka-provider而是调用一个eureka-customer的一个对服务宕机时的一个统一处理,不会导致一些的网络超时的错误抛出,发送“雪崩”现象。
springcloud学习04- 断路器Spring Cloud Netflix Hystrix的更多相关文章
- Spring Cloud Netflix Hystrix介绍和使用
前面我们搭建了具有服务降级功能的Hystrix客户端,现在我们来详细了解下Hystrix的一些功能. Hystrix的意思是豪猪,大家都知道,就是长满刺的猪...实际上,它表明了该框架的主要功能:自我 ...
- springCloud学习-消息总线(Spring Cloud Bus)
1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...
- SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用
1. Spring Cloud Netflix Spring Cloud Netflix 是Spring Cloud 的核心子项目,是对Netflix公司一系列开源产品的封装.它为Spring Bo ...
- SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性
1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...
- SpringCloud学习笔记(七):Hystrix断路器
概述 什么时候需要断路器?熔断? 举个简单的例子:小明喜欢小美,可是小美没有电话,小美给了小明家里的座机,小明打给座机,这个时候小美的妈妈接到了,小明怕妈妈知道自己喜欢小美,就跟小美妈妈说让小美哥接电 ...
- springcloud(一):大话Spring Cloud
研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...
- (转)springcloud(一):大话Spring Cloud
http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html 研究了一段时间Spring Boot了准备向Spring ...
- springcloud(一):大话Spring Cloud(山东数漫江湖)
研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...
- 【系统架构理论】一篇文章精通:Spring Cloud Netflix Eureka
是官方文档的总结 http://spring.io/projects/spring-cloud-netflix#overview 讲解基于2.0.2版本官方文档 https://cloud.sprin ...
随机推荐
- Maven下Java、JavaWeb约定标准项目结构
1.Maven Java 项目结构: 2.Maven JavaWeb 项目结构: 注意:webapp下必须要有WEB-INF文件夹,WEB-INF文件夹下必须要有web.xml 跟 classes文件 ...
- python内置模块之re模块
内容概要 re模块常用方法 findall search match re模块其他方法 split sub subn compile finditer findall 对无名分组优先展示 re实战之爬 ...
- CentOS7利用yum缓存搭建本地源
CentOS7利用yum缓存搭建本地源 环境说明 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 ( ...
- Android SugarORM(1)
Android Sugar ORM (1) Android Sugar ORM比我之前用过的ORM都要简单许多, 其目的是简化与Android中SQLite数据库的交互, 优点如下: 消除了编写SQL ...
- 防世界之Web_NewsCenter
题目: 打开实验环境一看,就一个搜索框,emmm试下有没有SQL注入点,SQL注入步骤传送门https://www.cnblogs.com/shacker/p/15917173.html 爆出数据, ...
- C#操作WMI指南
WMI应用(一个系统自带的测试WMI语句的工具) 1. 开始-运行-输入:wbemtest 回车2. 单击"连接", 输入:root\cimv2 回车; 或者ROOT\Securi ...
- JabRef:将bibtex格式的参考文献导入EndNote的转换软件
我在写小论文的时候,一直用的都是Overleaf在线latex编辑应用: https://www.overleaf.com/login 这个我感觉还是蛮好用的.只需要从期刊或者出版社的官网下载到lat ...
- input框限制输入金额
HTML: <input type="tel" class="capital mui-input-clear" value="0.00" ...
- 浏览器无插件播放rtsp流解决方案
1. 安装 FFmpeg 参考 CentOS下安装FFmpeg,特别详细. 我遇到的错误和解决办法: 缺少lame ffmpeg+libmp3lame库源码安装教程(CentOS) make ffmp ...
- Python:获取某一月的天数
import calendarcalendar.monthlen(2021,6)30calendar.monthrange(2021,6)(1, 30) calendar.monthrange( ye ...