Spring cloud微服务 Hystrix熔断器学习教程
以下demo代码:https://github.com/wades2/HystrixtDemo
官网定义:Hystrix是一个延迟容错库。在分布式环境中,许多服务依赖项中的一些不可避免地会失败。Hystrix是一个库,可通过添加延迟容错和容错逻辑来帮助您控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点,阻止它们之间的级联故障以及提供后备选项来实现这一点,所有这些都可以提高系统的整体弹性。
因为在分布式系统中很多服务是相互依赖的,假如以下某个功能实现依赖于几个服务:

假如此时【家庭信息查询服务】由于请求过多或者服务器原因出现了大批量服务不可用,【个人信息查询服务】由于无法请求到【家庭信息查询服务】,一直阻塞,造成服务器压力越来越大,后续又有【人员信息查询】的请求来了。就好比堵车的公路上又来了一大批车;此时会每个依赖关系都会在某些时候不可避免地失败。如果主机应用程序未与这些外部故障隔离,则整个系统可能会崩溃。
此时Hystrix熔断器就发挥了作用:
1、通过第三方客户端库访问(通常通过网络)依赖关系,以防止和控制延迟和故障。
2、在复杂的分布式系统中停止级联故障。(在一段时间内侦测到许多类似的错误,会强迫其以后的多个调用快速失败,不再访问远程服务器)
3、快速失败并迅速恢复。(快速失败后【个人信息查询服务】不会由于阻塞过载,就不会导致雪崩,)
4、在可能的情况下,后退并优雅的降级。
5、实现近实时监控,警报和操作控制。(如果熔断器诊断【家庭信息查询服务】错误已经修正,应用程序会再次尝试调用这个服务。)
这样我们就在之前学习Euraka+ribbon的基础上加熔断机制:
首先在我们的EurekaCaller里面添加上需要的依赖:
【注:如果不清楚的朋友或者没有重头看的朋友,这里附上我demo的
git地址:https://github.com/wades2/EurekaDemo2,
完整的搭建教程在:https://blog.csdn.net/asd529735325/article/details/85044158,
如果你的Eureka和ribbon已经搭建好了可以直接跳过。】
我们的项目基本是这个样子的:

Eurekacaller是这个样子:

添加pom依赖引入需要的jar包:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
重写一下我们的Controller:
package com.example.demo.controller;
import com.example.demo.service.Services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@RestController
public class Controller {
@Autowired
private Services service;
@RequestMapping(value = "getUser/routine",method = RequestMethod.GET)
public String routine(@RequestParam String id){
String json=service.routineService(id);
return json;
}
}
然后是我们具体的service,负载的部分,我们的断熔就放在service这里,一旦请求超时或者失败,立即失败并触发降级方法,我这里的降级方法是:callback。
package com.example.demo.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.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
@Service
public class Services {
@Autowired
private RestTemplate restTemplate;
//实现的callback方法来实现短路保护
@HystrixCommand(fallbackMethod = "callback")
public String routineService(String id){
System.out.println(id);
String jsons = restTemplate.getForObject("http://Eureka-client/user/getUser2", String.class);
return jsons;
}
public String callback(String id){
String msg="出现错误id:"+id;
return msg;
}
}
因为在前期我们没有扫描/user/getUser2所在的包路径,所以这个是肯定扫描不到的,访问一定是404not found的。
然后启动我们的5个启动类,访问http://localhost:8086/getUser/routine?id=52,我们可以看到访问到了callback页面:

这样一来,即使EurkaClient都出现了问题或者过载,我们也可以降级处理。那我们再试试如果超时会怎么样。
修改配置文件。,添加超时设置:
spring.application.name=Eureka_caller
server.port=8086
eureka.client.serviceUrl.defaultZone=http://user:123456@localhost:8083/eureka,http://user:123456@localhost:8082/eureka
#hystrix配置
hystrix.metrics.enabled=true
#默认的超时时间设置
hystrix.metrics.polling-interval-ms=2000
修改下Ribbon请求的路径:
//实现的callback方法来实现短路保护
@HystrixCommand(fallbackMethod = "callback")
public String routineService(String id){
System.out.println(id);
String jsons = restTemplate.getForObject("http://Eureka-client/user/getUser", String.class);
return jsons;
}
修改对应Service提供者的Client具体实现类(也就是修改EurekaClient和EurekaClient2),使其沉睡时间大于我们的熔断请求延迟时间,模拟服务器过载阻塞:
package com.example.demo.controller;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.converters.Auto;
import com.netflix.discovery.shared.Applications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private EurekaClient eurekaClients;
@GetMapping("getUser")
public String getUser() {
//todo 得到eureka server的服务实例
InstanceInfo info=eurekaClients.getNextServerFromEureka("Eureka-client",false);
try{
Thread.sleep(60000);
}catch (Exception e){
e.printStackTrace();
}
return "hello one";
}
}
我们看到请求后依然页面被拦截熔断了,因为等待时间60000毫秒远远大于了我们设置熔断等待时间:2000毫秒。
以上就是我们关于Hystrix熔断器的学习,各位看官大佬爷有什么问题请留言区评论,大家一起讨论解决。
Spring cloud微服务 Hystrix熔断器学习教程的更多相关文章
- Spring Cloud 微服务四:熔断器Spring cloud hystrix
前言:在微服务架构中,一般都是进程间通信,有可能调用链都比较长,当有底层某服务出现问题时,比如宕机,会导致调用方的服务失败,这样就会发生一连串的反映,造成系统资源被阻塞,最终可能造成雪崩.在sprin ...
- Spring Cloud微服务学习笔记
Spring Cloud微服务学习笔记 SOA->Dubbo 微服务架构->Spring Cloud提供了一个一站式的微服务解决方案 第一部分 微服务架构 1 互联网应用架构发展 那些迫使 ...
- spring cloud微服务快速教程之(七) Spring Cloud Alibaba--nacos(一)、服务注册发现
0.前言 什么是Spring Cloud Alibaba? Spring Cloud Alibaba 是阿里开源的,致力于提供微服务开发的一站式解决方案.此项目包含开发分布式应用微服务的必需组件,方便 ...
- 一张图了解Spring Cloud微服务架构
Spring Cloud作为当下主流的微服务框架,可以让我们更简单快捷地实现微服务架构.Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟.经得起实际考验的服务框架组合起来 ...
- 在阿里云容器服务上开发基于Docker的Spring Cloud微服务应用
本文为阿里云容器服务Spring Cloud应用开发系列文章的第一篇. 一.在阿里云容器服务上开发Spring Cloud微服务应用(本文) 二.部署Spring Cloud应用示例 三.服务发现 四 ...
- Spring Cloud 微服务六:调用链跟踪Spring cloud sleuth +zipkin
前言:随着微服务系统的增加,服务之间的调用关系变得会非常复杂,这给运维以及排查问题带来了很大的麻烦,这时服务调用监控就显得非常重要了.spring cloud sleuth实现了对分布式服务的监控解决 ...
- Spring Cloud 微服务五:Spring cloud gateway限流
前言:在互联网应用中,特别是电商,高并发的场景非常多,比如:秒杀.抢购.双11等,在开始时间点会使流量爆发式地涌入,如果对网络流量不加控制很有可能造成后台实例资源耗尽.限流是指通过指定的策略削减流量, ...
- Spring Cloud微服务初探
学习初衷 因为加了不少优秀的知识星球,结交了更多的小伙伴,加了更多的群,每每在自我介绍的时候,都说自己是Android & Java攻城狮. 然鹅,有的小伙伴就来问了,你是搞Java的,那对S ...
- spring cloud微服务实践七
在spring cloud 2.x以后,由于zuul一直停滞在1.x版本,所以spring官方就自己开发了一个项目 Spring Cloud Gateway.作为spring cloud微服务的网关组 ...
随机推荐
- 21.Longest Palindromic Substring(最长回文子串)
Level: Medium 题目描述: Given a string s, find the longest palindromic substring in s. You may assume ...
- 14.Diameter of Binary Tree(二叉树的直径)
Level: Easy 题目描述: Given a binary tree, you need to compute the length of the diameter of the tree. ...
- element el-tree循环遍历树形结构,并动态赋值disabled属性
凌晨3点,功夫不负有心人,已经累趴,效果终于出来: 贴上代码: <style scoped> .form { width: 50%; } </style> <templa ...
- ubuntu开机自启动服务管理
安装sysv-rc-conf sudo apt-get install sysv-rc-conf 执行下面,查看服务情况 sudo sysv-rc-conf 启动服务有以下两种方式 update-rc ...
- Mybatis学习笔记(九) —— Mybatis逆向工程
一.什么是Mybatis逆向工程? 简单的解释就是通过数据库中的单表,自动生成java代码. 我们平时在使用Mabatis框架进行Web应用开发的过程中,需要根据数据库表编写对应的Pojo类和Mapp ...
- inner join、left join、right join、full join
A表 a1 b1 c1 01 数学 95 02 语文 90 03 英语 80 B表 a2 b2 01 张三 02 李四 04 王五 SQL语句:select A.*,B.* from A inner ...
- CodeForces-Zuhair and Strings(思维+枚举)
Given a string ss of length nn and integer kk (1≤k≤n1≤k≤n). The string ss has a level xx, if xx is l ...
- HDU 3652 区间有13并且这样整除13 的数量(数位DP)
题目:求1-n的范围里含有13且能被13整除的数字的个数. 分析: dfs(len, num, mod, flag) mod记录数字对13取余后的值 len表示当前位数 num==0 不含13且上一位 ...
- delete ELK index
Go to tab “Dev Tools”4. On the left console type:GET _cat/indices?v&s=store.size:descand execute ...
- web服务器Nginx环境下如何实现安全证书https的配置
https跟http的关系 https没出现之前,我们网站大多数都是http开头,http全名超文本传输协议,客户端据此获取服务器上的超文本内容.超文本内容则以HTML为主,客户端拿到HTML内容后可 ...