一、断路器简介。

在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。

为了解决这个问题,业界提出了断路器模型。

就是服务可不可用都得返回一个结果,这就是断路器。

Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的。

较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阈值(Hystric 是5秒20次) 断路器将会被打开。

断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。

在ribbon使用断路器是通过调用方法。Feign中断路器是自带的,需要在FeignClient的SchedualServiceHi接口的注解中加上fallback的指定类。

二、在ribbon使用断路器

改造serice-ribbon 工程的代码,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix的起步依赖:

  <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

在程序的启动类ServiceRibbonApplication 加@EnableHystrix注解开启Hystrix:

@EnableHystrix

package com.sun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class ServiceRibbonApplication { public static void main(String[] args) {
SpringApplication.run( ServiceRibbonApplication.class, args );
} @Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
} }

改造HelloService类,在hiService方法上加上@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回了一个字符串,字符串为”hi,”+name+”,sorry,error!”,代码如下:

package com.sun;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service
public class HelloService { @Autowired
RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
} public String hiError(String name) {
return "hi,"+name+",sorry,error!";
}
}

启动:service-ribbon 工程,当我们访问http://localhost:8764/hi?name=sun,浏览器显示:

hi sun ,i am from port:8763

此时关闭 service-hi 工程,当我们再访问http://localhost:8764/hi?name=sun,浏览器会显示:

hi,sun,sorry,error!

这就说明当 service-hi 工程不可用的时候,service-ribbon调用 service-hi的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。

三、Feign中使用断路器

Feign是自带断路器的,它没有默认打开。需要在配置文件中配置打开它,在application.yml配置文件加以下代码:

feign:
hystrix:
enabled: true #注意冒号后面的空格

基于service-feign工程进行改造,只需要在FeignClient的SchedualServiceHi接口的注解中加上fallback的指定类就行了:

package com.sun;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "service-hi", fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

SchedualServiceHiHystric需要实现SchedualServiceHi 接口,并注入到Ioc容器中,代码如下:

package com.sun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ServiceFeignApplication { public static void main(String[] args) {
SpringApplication.run( ServiceFeignApplication.class, args );
}
}

启动四servcie-feign工程,启动service-hi工程,浏览器打开http://localhost:8765/hi?name=sun,网页显示:

hi sun ,i am from port:8763

此时关闭 service-hi 工程,当我们再访问http://localhost:8765/hi?name=sun,网页显示:

sorry sun

这证明断路器起到作用了。

第四篇:断路器(Hystrix)的更多相关文章

  1. SpringCloud教程 | 第四篇:断路器(Hystrix)

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...

  2. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...

  3. SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...

  4. 【SpringCloud】第四篇:断路器(Hystrix)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  5. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f4-hystrix/ 本文出自方志朋的博客 在微服务架构中, ...

  6. Spring Cloud学习笔记【四】断路器Hystrix

    雪崩效应 在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应.服务雪崩效应是一种因 “服务提供者” 的不可用导致 “服务消 ...

  7. 【SpringCloud】第十一篇: 断路器监控(Hystrix Dashboard)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  8. 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f12-dash/ 本文出自方志朋的博客 在我的第四篇文章断路 ...

  9. SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...

  10. 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)

    转载请标明出处: 首发于:https://www.fangzhipeng.com/springcloud/2017/07/12/sc12-hystix-dashbd/ 本文出自方志朋的博客 最新Fin ...

随机推荐

  1. 【原】 The Linux Command Line- pwd,cd,ls,file,less

    pwd - print name of current working directory. cd - change directory ls - list directory contents fi ...

  2. ELK Redis高性能加速

    1.下载redis并安装好 wget http://download.redis.io/releases/redis-2.8.13.tar.gz tar zxf redis-.tar.gz cd re ...

  3. CSS 盒子投影

    box-shadow 属性可以设置盒子的投影效果.它的原理同文本投影一样.字体风格一节有介绍. 它有4个值,同时使用,也可以有选择地使用: 第一个值 设置阴影左右延伸长度,负值向左,正值向右 第二个值 ...

  4. 使用JavaMail发送邮件-从FTP读取图片并添加到邮件正文发送

    业务分析: 最近工作需要,需要从FTP读取图片内容,添加到邮件正文发送.发送邮件正文,添加附件采用Spring的MimeMessageHelper对象来完成,添加图片也将采用MimeMessageHe ...

  5. Sping Cloud hystrix.stream 自动发现-监控

    相关组件安装脚本 [root@java_gateway4 java_tps]# cat cront_install.sh #!/bin/bashyum install jq -ymkdir /home ...

  6. POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)

    昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...

  7. scrapy -->CrawlSpider 介绍

    scrapy -->CrawlSpider 介绍 1.首先,通过crawl 模板新建爬虫: scrapy genspider -t crawl lagou www.lagou.com 创建出来的 ...

  8. 386. Lexicographical Numbers 输出1到n之间按lexico排列的数字序列

    [抄题]: Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,1 ...

  9. Java比较两个时间的前后

    public static int compare_date(String DATE1, String DATE2) { DateFormat df = new SimpleDateFormat(&q ...

  10. git 删除本地所有分支

    除master的所有: git branch | grep -v "master" | xargs git branch -D   Git删除分支的指令:git branch -d ...