依赖上个博客: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. Docker Explore the application

    https://docs.docker.com/docker-for-mac/#explore-the-application   Open a command-line terminal and t ...

  2. nodejs串行有关联

    var async = require('async'); //串行无关联series//串行有关联waterfallasync.waterfall([ function(cb) { setTimeo ...

  3. php发送邮件 (phpmailer)

    1.首先下载phpMailer文件官方文件https://sourceforge.net/projects/phpmailer/: 还有class.smtp.php. 2.去配置一下发送邮件的服务器, ...

  4. JAVA8学习——深入浅出方法引用(学习过程)

    方法引用:method reference 先简单的看一下哪里用到了方法引用: public class MethodReferenceTest { public static void main(S ...

  5. (翻译) CAP 理论 FAQ

    CAP 理论 FAQ 0. 关于这个文档 没有其它比CAP理论更引人注意的话题了, 这个FAQ的目的, 是说明对于CAP, 当前哪些是已知的, 并帮助那些刚接触这个理论的人快速了解, 并解决一些错误的 ...

  6. zabbix监控多个nginx vhost网站状态码

    需求 假设一台服务器运行了N个vhost网站,如何确定在大流量并发时候找到是哪个网站的问题呢? 这似乎是每个运维都会遇到的问题,方法有很多比如:1.看nginx日志大小确定访问量.2.通过前端代理确定 ...

  7. 轩辕展览-VR虚拟展厅设计的好处和优势是什么?

    yu情仍在继续,实体展厅很糟糕,在过去两年之中,越来越多的实体展厅因闲置而关闭,线上VR虚拟展厅设计逐渐走出圈子,凭借云展示的优势和国家政策的支持,登上展示和销售的旗帜. 产品线上展厅的优势是什么1. ...

  8. Nacos2.X源码阅读总结

    前言 Nacos是一个Alibaba出品的高性能微服务时代产出的组件,集注册和配置中心为一体.那么Nacos为什么这么高性能呢?总结以下几点: 1:基于阿里自研的distro协议进行Nacos把不同节 ...

  9. 【C#基础概念】字节顺序(大端、小端)

    字节顺序,又称端序或尾序(英語:Endianness),在计算机科学领域中,指電腦記憶體中或在数字通信链路中,组成多字节的字的字节的排列顺序. 例如假设上述变量x类型为int,位于地址0x100处,它 ...

  10. 【缓存】CPU高速缓存 之MESI 性协议 Gif 动画

    CPU缓存架构 不同的CPU厂商的架构也有些不同,在这里只介绍流行的缓存架构 缓存一致性可以分为三个点: 在进程每个写入运算时都立刻采取措施保证资料一致性 每个独立的运算,假如它造成资料值的改变,所有 ...