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 ...
随机推荐
- SYSTEM表空间满,解决方法
SYSTEM表空间是Oracle创建数据库时候自动创建的,每个Oracle数据库都会有SYSTEM表空间,而且SYSTEM表空间总是要保持在联机模式下,因为其包含了数据库运行所要求的基本信息,如:数据 ...
- 框架5--nginx安装部署 上(web服务)
目录 框架5--nginx安装部署(web服务) 1.练习 2.昨日问题 3.今日内容 4.什么是web服务 5.web服务器软件 6.部署Nginx 7.平滑增加Nginx模块 8.Nginx的命令 ...
- 《PHP程序员面试笔试宝典》——如何应对自己不会回答的问题?
如何巧妙地回答面试官的问题? 本文摘自<PHP程序员面试笔试宝典> 在面试的过程中,对面试官提出的问题求职者并不是都能回答出来,计算机技术博大精深,很少有人能对计算机技术的各个分支学科了如 ...
- head 插件 Content-Type header [application/x-www-form-urlencoded] is not supported
{ "error": "Content-Type header [application/x-www-form-urlencoded] is not supported& ...
- Spring Cloud之服务注册中心搭建Eureka Server服务注册中⼼
Spring Cloud并不与Spring MVC类似是一个开源框架,而是一组解决问题的规范(个人理解).解决哪些问题呢?如下: 1)服务管理:⾃动注册与发现.状态监管 2)服务负载均衡 3)熔断 4 ...
- Vue 源码解读(5)—— 全局 API
目标 深入理解以下全局 API 的实现原理. Vue.use Vue.mixin Vue.component Vue.filter Vue.directive Vue.extend Vue.set V ...
- Java线程池进阶
线程池是日常开发中常用的技术,使用也非常简单,不过想使用好线程池也不是件容易的事,开发者需要不断探索底层的实现原理,才能在不同的场景中选择合适的策略,最大程度发挥线程池的作用以及避免踩坑. 一.线程池 ...
- [杂记]对RSA算法的数学原理的一点思考
- TypeScript初识
Typescript 英文官网:https://www.typescriptlang.org/ 中文官网:https://www.tslang.cn/ 介绍 TypeScript 是一种强类型的编程语 ...
- 智能化管理工具:商业智能BI
BI(商业智能)是在ERP等信息化管理工具的基础上提出的,是基于信息技术构建的智能化管理工具,它实时地对ERP.CRM.SCM等管理工具生成的企业数据进行各种分析,并给出报告,帮助管理者认识企业和市 ...