在分布式架构中,断路器模式的作用也是类似的,当某个服务单元发生故障(类似用电器发生短路)之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。

Netflix Hystrix

在Spring Cloud中使用了Hystrix 来实现断路器的功能。Hystrix是Netflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。

下面我们来看看如何使用Hystrix。

准备工作

在开始加入断路器之前,我们先拿之前构建两个微服务为基础进行下面的操作,主要使用下面几个工程

    • eureka-server工程:服务注册中心,端口1111
    • compute-service工程:服务单元,端口2222
    • eureka-ribbon:通过ribbon实现的服务单元,依赖compute-service的服务,端口3333
    • eureka-feign:通过feign实现的服务单元,依赖compute-service的服务,端口3333
    • Ribbon中引入Hystrix

      • 依次启动eureka-server、compute-service、eureka-ribbon工程
      • 访问http://localhost:1111/可以看到注册中心的状态
      • 访问http://localhost:3333/add,调用eureka-ribbon的服务,该服务会去调用compute-service的服务,计算出10+20的值,页面显示30
      • 关闭compute-service服务,访问http://localhost:3333/add,我们获得了下面的报错信息
      Whitelabel Error Page
      
      This application has no explicit mapping for /error, so you are seeing this as a fallback.
      
      Sat Jun 25 21:16:59 CST 2016
      There was an unexpected error (type=Internal Server Error, status=500).
      I/O error on GET request for "http://COMPUTE-SERVICE/add?a=10&b=20": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
      • pom.xml中引入依赖hystrix依赖
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId>
      </dependency>
      • 在eureka-ribbon的主类RibbonApplication中使用@EnableCircuitBreaker注解开启断路器功能:
      @SpringBootApplication
      @EnableDiscoveryClient
      @EnableCircuitBreaker
      public class RibbonApplication { @Bean
      @LoadBalanced
      RestTemplate restTemplate() {
      return new RestTemplate();
      } public static void main(String[] args) {
      SpringApplication.run(RibbonApplication.class, args);
      } }
      • 改造原来的服务消费方式,新增ComputeService类,在使用ribbon消费服务的函数上增加@HystrixCommand注解来指定回调方法。
      @Service
      public class ComputeService { @Autowired
      RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "addServiceFallback")
      public String addService() {
      return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
      } public String addServiceFallback() {
      return "error";
      } }
      • 提供rest接口的Controller改为调用ComputeService的addService
      @RestController
      public class ConsumerController { @Autowired
      private ComputeService computeService; @RequestMapping(value = "/add", method = RequestMethod.GET)
      public String add() {
      return computeService.addService();
      } }

      更多关于Hystrix的使用可参考How To Use

      Feign使用Hystrix

      注意这里说的是“使用”,没有错,我们不需要在Feigh工程中引入Hystix,Feign中已经依赖了Hystrix,我们可以在未做任何改造前,尝试下面你的操作:

      • 依次启动eureka-server、compute-service、eureka-feign工程
      • 访问http://localhost:1111/可以看到注册中心的状态
      • 访问http://localhost:3333/add,调用eureka-feign的服务,该服务会去调用compute-service的服务,计算出10+20的值,页面显示30
      • 关闭compute-service服务,访问http://localhost:3333/add,我们获得了下面的报错信息
      Whitelabel Error Page
      
      This application has no explicit mapping for /error, so you are seeing this as a fallback.
      
      Sat Jun 25 22:10:05 CST 2016
      There was an unexpected error (type=Internal Server Error, status=500).
      add timed-out and no fallback available.

      如果您够仔细,会发现与在ribbon中的报错是不同的,看到add timed-out and no fallback available这句,或许您已经猜到什么,看看我们的控制台,可以看到报错信息来自hystrix-core-1.5.2.jar,所以在这个工程中,我们要学习的就是如何使用Feign中集成的Hystrix。

      • 使用@FeignClient注解中的fallback属性指定回调类
      @FeignClient(value = "compute-service", fallback = ComputeClientHystrix.class)
      public interface ComputeClient { @RequestMapping(method = RequestMethod.GET, value = "/add")
      Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b); }
      • 创建回调类ComputeClientHystrix,实现@FeignClient的接口,此时实现的方法就是对应@FeignClient接口中映射的fallback函数。
      @Component
      public class ComputeClientHystrix implements ComputeClient { @Override
      public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
      return -9999;
      } }
      • 再用之前的方法验证一下,是否在compute-service服务不可用的情况下,页面返回了-9999。
      • 关于Feign的更多使用方法可参考:Feign

Spring Cloud构建微服务架构(三)断路器的更多相关文章

  1. Spring Cloud构建微服务架构(三)消息总线

     注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...

  2. Spring Cloud构建微服务架构

    Dalston版本 由于Brixton和Camden版本的教程已经停止更新,所以笔者计划在2017年上半年完成Dalston版本的教程编写(原计划完成Camden版本教程,但由于写了两篇Dalston ...

  3. 《Spring Cloud构建微服务架构》系列博文示例

    SpringCloud-Learning   源码下载地址:http://download.csdn.net/detail/k21325/9650968     本项目内容为Spring Cloud教 ...

  4. Spring Cloud构建微服务架构(五)服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: 我们使用Spring Cloud Netflix中的Eureka实现了服务 ...

  5. Spring Cloud构建微服务架构 - 服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: alt 我们使用Spring Cloud Netflix中的Eureka实 ...

  6. Spring Cloud构建微服务架构(二)服务消费者

    Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides clie ...

  7. Spring Cloud构建微服务架构:服务网关(路由配置)【Dalston版】

    转载:http://blog.didispace.com/spring-cloud-starter-dalston-6-2/ 原创  2017-08-26  翟永超  Spring Cloud 被围观 ...

  8. Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台

    Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台: https://gitee.com/leecho/cola-cloud

  9. 第1章 Spring Cloud 构建微服务架构(一)服务注册与发现

      一.Spring Cloud 简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总 ...

  10. Spring Cloud构建微服务架构(一)服务注册与发现

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...

随机推荐

  1. Mongo读书笔记2 -- 数据类型

      Mongo有多种类型的collection, 默认的是随着document 数目的增多自动增大; 还有一些collection被称为capped collection, 只能包含固定数目的docu ...

  2. Font Awesome使用简介

      Font awesome是一种用字体来实现图标的CSS插件. 使用方法: 到http://fortawesome.github.io/Font-Awesome/ 下载代码包. 代码包解压到本地后, ...

  3. [Node.js]33. Level 7: Persisting Questions

    Let's go back to our live-moderation app and add some persistence, first to the questions people ask ...

  4. css选择器顺序的小技巧

    在线演示 本地下载 css的选择器的顺序其实很有意思,如果应用的好的话,可以做一些简单的逻辑出来,配合上css3,就可以尽可能的脱离js了. 这里的演示是一个带有hover事件的四张照片,效果来自一个 ...

  5. jQuery和CSS3超酷表单美化插件

     这是一款效果很精美炫酷的jQuery和CSS3联系方式表单美化插件.大多数站点上都有让用户填写的联系方式表单,一个设计良好的表单可以大大的提升用户的体验度.该表单美化插件在原生HTML表单的基础上进 ...

  6. ArcGIS Pro体验03——主界面

    主界面和Office类似,应该上手比较快.新建Map后,加载了美国地图,毕竟是ESRI公司嘛.除项目(PROJECT),另外还有六个菜单面板,分别是地图(MAP).插入(INSERT).分析(ANAL ...

  7. ssh2的application.xml配置文件配置详解

    ssh2的application.xml配置文件配置详解   1.导入其他的配置文件.在ssh项目中可以导入其他的配置文件,导入的格式为: <import resource="clas ...

  8. HDU 2883 kebab(最大流)

    HDU 2883 kebab 题目链接 题意:有一个烧烤机,每次最多能烤 m 块肉.如今有 n 个人来买烤肉,每一个人到达时间为 si.离开时间为 ei,点的烤肉数量为 ci,每一个烤肉所需烘烤时间为 ...

  9. JavaScript toString、String和stringify方法区别

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. eclipse 配置多个tomcat

      eclipse 配置多个tomcat CreateTime--2018年4月23日15:32:28 Author:Marydon windows-->Preferences-->Ser ...