雪崩效应

在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应。服务雪崩效应是一种因 “服务提供者” 的不可用导致 “服务消费者” 的不可用, 并将不可用逐渐放大的过程。
如果下图所示:A 作为服务提供者,B 为 A 的服务消费者,C 和 D 是 B 的服务消费者。A 不可用引起了 B 的不可用,并将不可用像滚雪球一样放大到 C 和 D 时,雪崩效应就形成了。

断路器

Netflix 创建了一个名为 Hystrix 的库, 实现了断路器的模式。“断路器” 本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。

当然,在请求失败频率较低的情况下,Hystrix 还是会直接把故障返回给客户端。只有当失败次数达到阈值时,断路器打开并且不进行后续通信,而是直接返回备选响应。当然,Hystrix 的备选响应也是可以由开发者定制的。

Ribbon 整合 Hystrix

新建spring start project,导入依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  17. </dependency>
  18. </dependencies>

属性配置(application.yml)

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: service-hystrix-ribbon
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://admin:123456@localhost:8761/eureka/

开启hystrix功能,在启动类上加上@EnableHystrix注解

  1. package com.carry.springcloud;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  7. import org.springframework.cloud.netflix.hystrix.EnableHystrix;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.web.client.RestTemplate;
  10.  
  11. @EnableHystrix
  12. @EnableEurekaClient
  13. @SpringBootApplication
  14. public class ServiceHystrixRibbonApplication {
  15.  
  16. public static void main(String[] args) {
  17. SpringApplication.run(ServiceHystrixRibbonApplication.class, args);
  18. }
  19.  
  20. @Bean
  21. @LoadBalanced
  22. RestTemplate restTemplate() {
  23. return new RestTemplate();
  24. }
  25. }

添加fallback方法

  1. package com.carry.springcloud.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import org.springframework.web.client.RestTemplate;
  8.  
  9. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  10.  
  11. @RestController
  12. public class RibbonController {
  13.  
  14. @Autowired
  15. RestTemplate restTemplate;
  16.  
  17. @Value("${server.port}")
  18. String port;
  19.  
  20. @GetMapping("/getPoducerInfo")
  21. @HystrixCommand(fallbackMethod = "getPoducerInfoFallback")
  22. public String getPoducerInfo() {
  23. String result = this.restTemplate.getForObject("http://service-producer/getPortInfo", String.class);
  24. return result;
  25. }
  26.  
  27. public String getPoducerInfoFallback(){
  28. return "getPoducerInfo异常,端口:" + port;
  29. }
  30. }

测试

依次启动eureka-server、service-producer、service-hystrix-ribbon,如下图成功注册到eureka

访问localhost:9001/getPoducerInfo,结果轮询显示8080与8081,停掉8081

接着访问localhost:9001/getPoducerInfo,当访问到8081时出现以下结果,说明服务降级调用了fallback方法

Feign 整合 Hystrix

新建项目并导入依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-openfeign</artifactId>
  17. </dependency>
  18. </dependencies>

配置属性

  1. server:
  2. port: 9002
  3. spring:
  4. application:
  5. name: service-hystrix-feign
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://admin:123456@localhost:8761/eureka/
  10. feign:
  11. hystrix:
  12. enabled: true

启动类开启Feign支持

  1. package com.carry.springcloud;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6.  
  7. @EnableFeignClients
  8. @SpringBootApplication
  9. public class ServiceHystrixFeignApplication {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(ServiceHystrixFeignApplication.class, args);
  13. }
  14. }

添加Feign接口

  1. package com.carry.springcloud.api;
  2.  
  3. import org.springframework.cloud.openfeign.FeignClient;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5.  
  6. import com.carry.springcloud.fallback.ConsumerFeignFallback;
  7.  
  8. @FeignClient(name = "service-producer", fallback = ConsumerFeignFallback.class)
  9. public interface ConsumerFeignClient {
  10.  
  11. @GetMapping("/getPortInfo")
  12. public String produce();
  13. }

添加fallback类

  1. package com.carry.springcloud.fallback;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. import com.carry.springcloud.api.ConsumerFeignClient;
  6.  
  7. @Component
  8. public class ConsumerFeignFallback implements ConsumerFeignClient {
  9.  
  10. @Override
  11. public String produce() {
  12. return "服务调用失败!";
  13. }
  14.  
  15. }

控制层注入Feign接口

  1. package com.carry.springcloud.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6.  
  7. import com.carry.springcloud.api.ConsumerFeignClient;
  8.  
  9. @RestController
  10. public class ConsumerController {
  11.  
  12. @Autowired
  13. private ConsumerFeignClient feignClient;
  14.  
  15. @GetMapping("/getPoducerInfoByFeign")
  16. public String getPoducerInfoByFeign() {
  17. return feignClient.produce();
  18. }
  19.  
  20. }

测试

测试方法同上不再赘述

Spring Cloud学习笔记【四】断路器Hystrix的更多相关文章

  1. spring cloud学习笔记四 熔断器Hystrix

    我们知道分布式服务有这样一个特点,每一个微服务都有自己的业务,并且很多时候一个微服务的业务要依赖于其他微服务,如果这些相互关联的微服务中其中某个微服务请求失败时,就会导致其他调用它的微服务也会请求失败 ...

  2. 【Spring Cloud学习之六】断路器-Hystrix

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 一.服务雪崩1.什么是服务雪崩分布式系统中经常会出现某个基础服务不可用造成整个系统不 ...

  3. Spring Cloud 学习笔记(二)——Netflix

    4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...

  4. Spring Cloud学习笔记-006

    服务容错保护:Spring Cloud Hystrix 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调 ...

  5. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

  6. Feign详细使用-Spring Cloud学习第四天(非原创)

    文章大纲 一.Feign是什么二.Feign的基本实现三.Feign的继承特性四.Feign配置详解五.项目源码与参考资料下载六.参考文章   一.Feign是什么 前面几篇文章我们详细的介绍了Rib ...

  7. Spring Cloud 学习笔记(一)——入门、特征、配置

    [TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...

  8. Spring Cloud学习笔记-007

    声明式服务调用:Spring Cloud Feign Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两 ...

  9. Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)

    在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...

  10. Spring Cloud学习笔记【一】Eureka服务注册与发现

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

随机推荐

  1. .map(function(item)...)这个是按hashcode自动遍历的,怎么才能按照我想要的顺序遍历呢?

    上图是我前端的遍历代码.我的item上有一个name的字段,分别是营业执照,税务登记证和经营许可证,我怎么设置才能让函数每次遍历的时候按照这个顺序遍历,而不是item自带的顺序呢? .map(func ...

  2. SpringCloud学习笔记(5)----Spring Cloud Netflix之Eureka的服务认证和集群

    1. Eureka服务认证 1. 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...

  3. 3ds Max做的卡通狗教程

    使用软件::3ds Max 软件下载:http://www.xy3dsmax.com/xiazai.html 全教程完,学完记得交作业.如果本教程对您有所帮助,请推荐给你的朋友.

  4. React项目构建(利用webpack打包)

    引言 最近React作为当前最为火热的前端框架.最近也相继而出来相关ES7的新语法. 当然,在使用React开发web项目的时候,不得不提到的就是与之配套的相应的打包技术,之前上文已经简单的提到Rea ...

  5. Python的那些坑--------括号篇

    在Python中遇见了带不带括号的问题,我目前的是这三种,有问题请指出.如果有其他的,我后续会更新 一  函数带不带括号: def a(x): return x print(a) #不带括号调用的结果 ...

  6. 3D ShapeNets: A Deep Representation for Volumetric Shapes 代码遇到的问题

    遇到 Error using polygon2voxel_double Requested 515396075640x140711718551672x140719189273184 (17179869 ...

  7. 浅谈 MySQL的外键的作用

    MySQL中外键的介绍: MySQL外键必须使用存储引擎为  innDB  其中MySAM 和MEMORYH这两种引擎不支持 由数据库自身保证数据一致性,完整性,更可靠,因为程序很难100%保证数据的 ...

  8. 新手做2D手游该用哪些工具?

    全球手游行业规模将突破250亿美元,越来越多的开发者开始进入手游研发领域,而作为一名菜鸟,很多时候,如果没有其他开发者的建议,会走很多弯路.一开始进入游戏研发领域的时候,你很难知道该选择什么工具.什么 ...

  9. hadoop1.1.0的伪分布搭建步骤

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFuYnVyZW4wMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  10. PHPStorm打开文件所在目录

    很实用~