Spring Cloud 熔断器
Spring Cloud 熔断器
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以通过 RPC 相互调用,在 Spring Cloud 中可以用 RestTemplate + Ribbon 和 Feign 来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证 100% 可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet 容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的 “雪崩” 效应。
即为了避免一个故障导致请求积压阻塞而挤垮整个系统,当有一个服务不可用时,比如到出现5秒内出现N次无法访问,则熔断器打开不再往后请求而直接返回。
Hystrix
Netflix 开源了 Hystrix 组件,实现了熔断器模式,Spring Cloud 对这一组件进行了整合。
ribbon中使用hystrix
pom中增加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
启动类中增加hystrix注解
@EnableHystrix
package com.outlook.liufei32.spring.cloud.web.admin.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class WebAdminRibbonApplication {
public static void main( String[] args ) { SpringApplication.run(WebAdminRibbonApplication.class, args);
}
}
在service中的方法增加@HystrixCommand注解并在配置返回方法(fallbackMethod = "hiError")
package com.outlook.liufei32.spring.cloud.web.admin.ribbon.service;
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 AdminService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String sayHi( String message ) {
return restTemplate.getForObject("http://SPRING-CLOUD-SERVICE-ADMIN/hi?message=" + message, String.class);
}
public String hiError(String message) {
return "Hi,your message is :\"" + message + "\" but request error.";
}
}
feign中使用hystrix
打开feign的熔断器配置
Feign 是自带熔断器的,但默认是关闭的。需要在配置文件中配置打开它,在配置文件增加以下代码:
feign:
hystrix:
enabled: true
启动类中增加hystrix注解
package com.outlook.liufe32.spring.cloud.web.admin.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class WebAdminFeignApplication {
public static void main( String[] args ) {
SpringApplication.run(WebAdminFeignApplication.class, args);
}
}
servie接口中增加fallback类
service接口:
package com.outlook.liufe32.spring.cloud.web.admin.feign.service;
import com.outlook.liufe32.spring.cloud.web.admin.feign.service.hystrix.AdminServiceHystrix;
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 = "spring-cloud-service-admin",fallback = AdminServiceHystrix.class)//指定fallback类来设置熔断器
public interface AdminService {
@RequestMapping(value = "hi", method = RequestMethod.GET)
public String sayHi( @RequestParam(value = "message") String message );
}
service接口实现类,这个实现类override的方法就是feign熔断返回的方法
package com.outlook.liufe32.spring.cloud.web.admin.feign.service.hystrix;
import com.outlook.liufe32.spring.cloud.web.admin.feign.service.AdminService;
import org.springframework.stereotype.Component;
@Component
public class AdminServiceHystrix implements AdminService {
@Override
public String sayHi( String message ) {
return "Hi,your message is :\"" + message + "\" but request error.";
}
}
本博客为Swagger-Ranger的笔记分享,文章会持续更新
文中源码地址: https://github.com/Swagger-Ranger
欢迎交流指正,如有侵权请联系作者确认删除: liufei32@outlook.com
Spring Cloud 熔断器的更多相关文章
- 6、Spring Cloud -熔断器Hystrix
6.1.什么是Hystrix 在分布式系统中.服务与服务之间的依赖错综复杂,一种不可避免的情况就是某些服务 出现故障,导致依赖于它们的其他服务出现远程调度的线程阻塞. Hystrix是Netfli ...
- 【Spring Cloud】 总结
一.Spring Cloud简介 简单来说,Spring Cloud 就是致力于分布式系统.微服务等的一套在目前非常火热的框架.但它的本身也是一系列框架的有序集合(由多个模块组成). 相比较于Dubb ...
- spring boot 2.0.3+spring cloud (Finchley)4、熔断器Hystrix
在分布式系统中服务与服务之间的依赖错综复杂,一种不可避免的情况就是某些服务会出现故障,导致依赖于他们的其他服务出现远程调度的线程阻塞.某个服务的单个点的请求故障会导致用户的请求处于阻塞状态,最终的结果 ...
- 一起来学Spring Cloud | 第五章:熔断器 ( Hystrix)
在微服务项目中,一个系统可以分割成很多个不同的服务模块,不同模块之间我们通常需要进行相互调用.springcloud中可以使用RestTemplate+Ribbon和Feign来调用(工作中基本都是使 ...
- Spring Cloud Hystrix Dashboard熔断器-Turbine集群监控(六)
序言 上一篇说啦hystrix的使用方法与配置还有工作流程及为何存在,我去,上一篇这么屌,去看看吧,没这么屌的话,我贴的有官方文档,好好仔细看看 hystrix除啦基本的熔断器功能之外,还可以对接口的 ...
- spring cloud学习笔记四 熔断器Hystrix
我们知道分布式服务有这样一个特点,每一个微服务都有自己的业务,并且很多时候一个微服务的业务要依赖于其他微服务,如果这些相互关联的微服务中其中某个微服务请求失败时,就会导致其他调用它的微服务也会请求失败 ...
- spring cloud微服务快速教程之(四)熔断器(Hystrix)及其工具(Dashboard、Turbine)
0-为什么需要熔断器 在分布式系统中,各个服务相互调用相互依赖,如果某个服务挂了,很可能导致其他调用它的一连串服务也挂掉或者在不断等待中耗尽服务器资源,这种现象称之为雪崩效应: 未来防止系统雪崩,熔断 ...
- spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 当超大并发量并发访问一个服务接口时,服务器会崩溃 ,不仅导致这个接口无法 ...
- Spring Cloud Hystrix 1(熔断器简介)
在分布式框架中当某个服务单元发生故障之后通过断路器的故障监控向调用方返回一个错误响应,而不是长期等待这样就不会使得线程因调用故障服务被长时间占用不放,避免了故障在分布式系统中的蔓延 针对上述问题,Sp ...
随机推荐
- poj1733 Parity game[带权并查集or扩展域]
地址 连通性判定问题.(具体参考lyd并查集专题该题的转化方法,反正我菜我没想出来).转化后就是一个经典的并查集问题了. 带权:要求两点奇偶性不同,即连边权为1,否则为0,压缩路径时不断异或,可以通过 ...
- boost库安装和使用
1. 下载最新的boost库:http://www.boost.org/本文使用的是boost_1_66_0.tar.gz, 2. Boost库安装步骤: > 解压下载文件,例如下载文件在~/D ...
- bzoj 2865 字符串识别 —— 后缀数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2865 唯一出现的子串就是每个后缀除去和别的后缀最长的 LCP 之外的前缀: 所以用这个更新一 ...
- openStack高可用性和灾备方案
1. 基础知识 1.1 高可用 (High Availability,简称 HA) 高可用性是指提供在本地系统单个组件故障情况下,能继续访问应用的能力,无论这个故障是业务流程.物理设施.IT软/硬件的 ...
- simple demo of Handlebars.js & jquery.js
simple demo of Handlebars.js & jquery.js <html> <head> <script src="jquery-1 ...
- Power OFF and ON USB device in linux (ubuntu)
Power OFF and ON USB device in linux (ubuntu) http://loginroot.com/power-off-and-on-usb-device-in-li ...
- Ubuntu 16 Mysql 安装配置
安装Mysql apt-get update; apt-get install mysql-server apt-get install mysql-client //安装过程中会提示修改密码 ...
- PHP注释-----PHPDOC
用过IDE或看过其他源码的小伙伴们应该都见过类似下面这样的注释 /** * 递归获取所有游戏分类 * @param int $id * @return array */ 看得多了就大概知道了一些规 ...
- Unity中场景异步加载
引入命名空间 using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; using Syst ...
- 小a和uim之大逃离(luogu P1373 dp)
小a和uim之大逃离(luogu P1373 dp) 给你一个n*m的矩阵,其中元素的值在1~k内.限制只能往下和往右走,问从任意点出发,到任意点结束,且经过了偶数个元素的合法路径有多少个.在此题中, ...