依赖上个博客: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的更多相关文章

  1. Spring Cloud Netflix Hystrix介绍和使用

    前面我们搭建了具有服务降级功能的Hystrix客户端,现在我们来详细了解下Hystrix的一些功能. Hystrix的意思是豪猪,大家都知道,就是长满刺的猪...实际上,它表明了该框架的主要功能:自我 ...

  2. springCloud学习-消息总线(Spring Cloud Bus)

    1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...

  3. SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用

    1.  Spring Cloud Netflix Spring Cloud Netflix 是Spring Cloud 的核心子项目,是对Netflix公司一系列开源产品的封装.它为Spring Bo ...

  4. SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性

    1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...

  5. SpringCloud学习笔记(七):Hystrix断路器

    概述 什么时候需要断路器?熔断? 举个简单的例子:小明喜欢小美,可是小美没有电话,小美给了小明家里的座机,小明打给座机,这个时候小美的妈妈接到了,小明怕妈妈知道自己喜欢小美,就跟小美妈妈说让小美哥接电 ...

  6. springcloud(一):大话Spring Cloud

    研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...

  7. (转)springcloud(一):大话Spring Cloud

    http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html 研究了一段时间Spring Boot了准备向Spring ...

  8. springcloud(一):大话Spring Cloud(山东数漫江湖)

    研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...

  9. 【系统架构理论】一篇文章精通:Spring Cloud Netflix Eureka

    是官方文档的总结 http://spring.io/projects/spring-cloud-netflix#overview 讲解基于2.0.2版本官方文档 https://cloud.sprin ...

随机推荐

  1. 4、前端--浮动、定位、是否脱离文档流、溢出属性、z-index、透明度、JavaScript简介

    浮动 # ps:html代码时没有缩进一说的 全部写在一行也可以 """浮动主要就是用于页面布局的!!!""" # 浮动带来的负面影响 &q ...

  2. Solution -「ARC 104F」Visibility Sequence

    \(\mathcal{Description}\)   Link.   给定 \(\{x_n\}\),对于满足 \(h_i\in[1,x_i]\) 的序列 \(\{h_n\}\),定义序列 \(\{p ...

  3. 免费开源Blazor在线Ico转换工具

    行文目录 功能效果演示 实现说明 2.1 其他图片上传 2.2 核心代码:其他图片转Ico 2.3 转换后的Ico文件下载 总结 1. 功能效果演示 仓库地址:IcoTool 在线演示地址:https ...

  4. Linux mysql8.0.11安装

    准备:检查是否已安装过mysql,若有便删除(linux系统自带的) rpm -qa | grep mariadb rpm -e nodeps mariadb-libs-5.5.56-2.el7.x8 ...

  5. Typora的使用教程.md

    Typora的使用教程 原创:https://home.cnblogs.com/u/cn-zhouchao 2021.12.13 一.软件的介绍 Typora 是一款由 Abner Lee 开发的轻量 ...

  6. 巧用阿里云同步k8s.gcr镜像

    问题 谷歌云镜像仓库:k8s.gcr.io 镜像拉取不下来 阿里云操作配置 注册阿里云账号:点击注册 右上角点击"控制台" 点击左上角 选择"容器镜像服务" 第 ...

  7. 深度测评,商业智能BI、报表工具谁更好用?

    在很多人入门数据分析师或者投身大数据行业的时候,必然会听到的两个词就是"报表工具"和"BI商业智能"."BI"一词已被更广泛地知晓,但提起B ...

  8. 通过shell脚本批量操作mysql数据库

    创建建表语句 ============================================= 学生表:Student(Sno,Sname,Ssex,Sage,Sdept) ------(学 ...

  9. 详解 c# 克隆

    克隆方法是原型设计模式中必须使用的方式,它将返回一个与当前对象数据一致的对象.正如其名,犹如一个模子雕刻而出.克隆类型分为两种:浅克隆.深克隆. 浅复制就是仅复制类中的值类型成员 深复制就是复制类中的 ...

  10. 多态在C#中的应用

    C# 语言经过专门设计,以便不同库中的基类与派生类之间的版本控制可以不断向前发展,同时保持向后兼容.这具有多方面的意义.例如,这意味着在基类中引入与派生类中的某个成员具有相同名称的新成员在 C# 中是 ...