那些不懂hystrix的秘密
一 前言
springcloud系列文章已经出到hystrix,中间知识追寻者跑去学了其它知识,回来感觉spingcloud系列出的也不少了;需要完全理解这篇文章对于初学者需要有一定的基础知识,如果看不懂请移步知识追寻者的springcloud专栏进行学习;学完本篇,你将获得学会使用Hstrix进行服务容错,学会Hystrix熔断监控;能学完本篇内容请留下赞呦!!!!
二 Hystrix入门
首先需要搭建工程hystrix-client 引入eureka和 hystrix依赖
2.1pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
2.2 service
在service层定义一个获得用户的方法getUser()用于提供服务,方法内容对用户名进行判断,如果是zszxz, 返回回用户说明,否则抛出异常;定义默认用户方法defaultUser()用于容错处理,在获得用户方法上面添加注释@HystrixCommand 并且将容错回调指向defaultUser()方法;有关注释详细使用请读者参考如下官方文档;
/**
* @Author lsc
* <p> </p>
*/
@Component
public class UserService {
// 为getUser方法添加容错回调处理
@HystrixCommand(fallbackMethod = "defaultUser")
public String getUser(String params){
if (params.equals("zszxz")){
return "the user is zszxz";
}else {
// 抛出异常时会直接调用fallbackMethod中指定的方法
throw new RuntimeException();
}
}
/* *
* @Author lsc
* <p> 出错回调处理</p>
* @Param [params]
* @Return java.lang.String
*/
public String defaultUser(String params){
return "it is the user thar is not exist in project";
}
}
2.3 controller
表现层直接调用服务层方法,参数为用户名username
/**
* @Author lsc
* <p> </p>
*/
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("user")
public String getUser(String username){
return userService.getUser(username);
}
}
2.4application.yml
配置文件中指定端口8094 , 并且指定应用名称为 hystrix-client , 后面是向eureka-server进行服务注册;
server:
port: 8094
spring:
application:
name: hystrix-client # 应用名称
eureka:
client:
service-url:
# 服务注册地址
defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
2.5 Hsytrix启动类
在启动类上添加注释@EnableHystrix ,表示启用 hystrix功能;
/**
* @Author lsc
* <p> </p>
*/
@SpringBootApplication
@EnableHystrix // 启用 hystrix
@EnableDiscoveryClient// 服务注册发现
public class HystrixApp {
public static void main(String[] args) {
SpringApplication.run(HystrixApp.class, args);
}
}
2.6 执行结果
启动eureka-server, eureka-client, hystrix-client工程;访问地址http://localhost:8094/user?user=zszxz;得出正确访问结果如下;

修改用户名参数不是zszxz 发现报的不是异常,而是defualtuser中默认的容错处理,说明使用hystrix成功;

三 Hstrix集成Feign
学会了简单使用hystrix, 但远远不够,通常Feign中自带了hystrix功能,这边需要使用Feign进行开启hystrix,然后进行容错处理;
3.1 eureka-client
在之前的eureka-client 表现层中添加一个 getFH() 方法用于Feign客户端调用;
/* *
* @Author lsc
* <p> feign-hystix 集成测试</p>
* @Param [username]
* @Return java.lang.String
*/
@GetMapping("zszxz/fh")
public String getFH(String username){
if (username.equals("zszxz")){
return "the user is zszxz on fh project";
}else {
throw new RuntimeException();
}
}
3.2 pom.xml
新建工程feign-hystrix 用于集成测试,依赖引入 openfeign 即可;
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
3.3service
service层使用FeignClient进行远程过程调用,并且指定 回调类,其内容在3.4节
/**
* @Author lsc
* <p> </p>
*/
@FeignClient( name = "eureka-client", value = "eureka-client", fallback = FHServiceFallback.class)
public interface FHService {
@GetMapping("zszxz/fh")
public String getFeign(String username);
}
3.4 fallback
回调函数实现FHService接口,并且在类上添加注解@Component 表示会被spirng IO容器扫描注入;
/**
* @Author lsc
* <p> </p>
*/
@Component
public class FHServiceFallback implements FHService {
@Override
public String getFeign(String username) {
return "sorry feilure,you can try it again";
}
}
3.5controller
controller层直接可以调用service层接口;
/**
* @Author lsc
* <p> feign 表现层 </p>
*/
@RestController
public class FHController {
@Autowired
FHService fhService;
@GetMapping("zszxz/feign")
public String getFeign(String username){
// 调用 getFeign方法
return fhService.getFeign(username);
}
}
3.6 application.yml
配置文件中指定端口,应用名称,关键是要开启feign自带的hystix 功能
server:
port: 8095
spring:
application:
name: hystrix-feign # 应用名称
eureka:
client:
service-url:
# 服务注册地址
defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
# 开启hystix
feign:
hystrix:
enabled: true
3.7FH启动类
启动类中使用注解 @EnableFeignClients 开启Feign功能;
/**
* @Author lsc
* <p> </p>
*/
@SpringBootApplication
@EnableFeignClients
public class FeignHystrixApp {
public static void main(String[] args) {
SpringApplication.run(FeignHystrixApp.class,args);
}
}
3.8 执行结果
正常访问结果正确,结果如下

输入用户名非zszxz,进行容错处理友好提示如下,说明feign集成使用hystix成功;

四 Hystrix Dashbord监控熔断器
学会使用hystix还是皮毛中的皮毛,我们还需要对hystrix进行容错监控;
4.1 feign-hystrix工程添加内容
在feign-hystrix 工程中添加 actuator , hystrix 依赖;
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
appication.yml配置文件中指定暴露端点;
management:
endpoints:
web:
exposure:
include: hystrix.stream
启动类中添加@EnableHystrix 注解表示开启Hystrix
/**
* @Author lsc
* <p> </p>
*/
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class FeignHystrixApp {
public static void main(String[] args) {
SpringApplication.run(FeignHystrixApp.class,args);
}
}
4.2 hystrix-dashboard
新建工程hystrix-dashboard 用于专门的容错监控;引入依赖如下;
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
application.yml 指定当前应用名称和端口;
server:
port: 8096
spring:
application:
name: hystrix-dashboard # 应用名称
eureka:
client:
service-url:
# 服务注册地址
defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
启动类上表名注解 @EnableHystrixDashboard 表示启动容错监控;
/**
* @Author lsc
* <p> </p>
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard//开启监控
public class HystixDashboard {
public static void main(String[] args) {
SpringApplication.run(HystixDashboard.class,args);
}
}
访问 http://localhost:8096/hystrix 出现一头豪猪图页面表示初步配置成功;

页面中的输入url有如下三种:
- 集群应用:http://turbine-hostname:port/turbine.stream
- 单个集群应用:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
- 单体应用:http://hystrix-app:port/actuator/hystrix.stream
在页面输入地址 http://localhost:8095/actuator/hystrix.stream 访问单体应用,页面会处于loading状态
当我们访问 http://localhost:8095/zszxz/feign 会出现仪表盘,其具体的监控指标内容意义请读者参考官方文档,不再详细说明。

五 turbine聚合监控
在第四节中读者已经学会如何使用hystix dashboard进行容错监控;但是缺点也很明显,那就是一个单体应用需要开启一个dashboard 页面;如果使用turbine进行集成管理,那么所有hystix应用都会显示在一个 dashboard上,方便我们查阅,排查;
5.1 hystrix-feign2
新建 hystrix-feign2 工程;修改配置文件端口为8098,应用名称为hystrix-feign2,其它内容跟 hystrix-feign 工程一样;
5.2 turbine-monitor
新建turbine-monitor工程,添加新依赖 turbine
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
application.xml 中内容如下 指定了监控的hystrix应用是hystrix-feign 和 hystrix-feign , 并且采用默认名称方式;
server:
port: 8097
spring:
application:
name: turbine-monitor # 应用名称
eureka:
client:
service-url:
# 服务注册地址
defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
turbine:
appConfig: hystrix-feign,hystrix-feign2
clusterNameExpression: "'default'"
启动类中添加新的注解 @EnableTurbine 表示开启turbine 功能;
/**
* @Author lsc
* <p> </p>
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableTurbine
@EnableHystrixDashboard
public class TurbineApp {
public static void main(String[] args) {
SpringApplication.run(TurbineApp.class, args);
}
}
5.3启动工程
- 访问 http://localhost:8097/hystrix 出现hystrix界面
- 在界面中输入http://localhost:8097/turbine.stream 访问进入loding状态
- 访问 http://localhost:8095/zszxz/feign 得出 ‘sorry feilure,you can try it again’
- 访问 http://localhost:8098/zszxz/feign 得出 ‘sorry feilure,you can try it again’
- 最后得出结果如下,发现两个应用的hystrix 都集中在一个页面上;
5.4 源码
关于源码请移步专栏说明即可获得知识追寻者springcloud全部学习内容;
那些不懂hystrix的秘密的更多相关文章
- [置顶] 请听一个故事------>你真的认为iPhone只是一部手机?苹果惊天秘密!!
在网上看到的一篇小说,感觉有点意思,转载过来大家一起围观下,作者很幽默很风趣. 导读:iPhone的隐藏功能!Jobs的军方身份!图灵服毒自杀的传奇故事!中兴华为的神秘背景! 你真的认为iPhone只 ...
- 你不知道你不懂javascript
过去几年我注意到技术圈一个很奇怪的现象,有太多程序员将那些他们只是有过非常浅显的了解, 但其实根本就不懂的技术写到他们的简历中,这个现象几乎每种语言都有,但这其中最严重的就要数javascript了. ...
- SpringCloud笔记六:Hystrix
目录 Hystrix是什么? Hystrix服务熔断 新建Hystrix项目 修改yml Maven的pom.xml添加hystrix引用 修改Controller Hystrix服务降级 修改api ...
- 【雷神源码解析】无基础看懂AAC码流解析,看不懂你打我
一 前言 最近在尝试学习一些视频相关的知识,随便一搜才知道原来国内有雷神这么一个真正神级的人物存在,尤其是在这里(传送门)看到他的感言更是对他膜拜不已,雷神这种无私奉献的精神应当被我辈发扬光大.那写这 ...
- 一文读懂SpringCloud与Eureka,Feign,Ribbon,Hystrix,Zuul核心组件间的关系
概述 毫无疑问,Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术.不过大多数讲解还停留在对Spring Cloud功能使用的层面,其底层的很多原理,很多人可能并不知晓 ...
- Hystrix 学习使用
说明: 每次调用创建一个新的HystrixCommand,把依赖调用封装在run()方法中 执行execute()/queue做同步或异步调用 请求接收后,会先看是否存在缓存数据,如果存在,则不会继续 ...
- JNI探秘-----你不知道的FileInputStream的秘密
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 设计模式系列结束,迎来了LZ ...
- 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]
洛谷传送门,BZOJ传送门 秘密消息Secret Message Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤5 ...
- 请听一个故事------>你真的认为iPhone只是一部手机?苹果惊天秘密!!
在网上看到的一篇小说,感觉有点意思,转载过来大家一起围观下,作者很幽默很风趣. 导读:iPhone的隐藏功能!Jobs的军方身份!图灵服毒自杀的传奇故事!中兴华为的神秘背景! 你真的认为iPhone只 ...
随机推荐
- java数组简介
数组(Array)是Java 语言中内置的一种基本数据存储结构,通俗的理解,就是一组数的集合,目的是用来一次存储多个数据.数组是程序中实现很多算法的基础,可以在一定程度上简化代码的书写. 备注: 数组 ...
- UVA 11107 Life Forms——(多字符串的最长公共子序列,后缀数组+LCP)
题意: 输入n个序列,求出一个最大长度的字符串,使得它在超过一半的DNA序列中连续出现.如果有多解,按照字典序从小到大输出所有解. 分析:这道题的关键是将多个字符串连接成一个串,方法是用不同的分隔符把 ...
- fatal: Not a git repository (or any of the parent directories)
当从github.com上面下载下了Firmware后,无意中删除了Firmware目录下的.git文件夹,再去编译就会出现: fatal: Not a git repository (or an ...
- tf.truncates_normal()
转载自:https://blog.csdn.net/uestc_c2_403/article/details/72235565 tf.truncated_normal(shape, mean, std ...
- Makefile记录
需要把sum.c编译汇编成可执行程序zzj zzj:sum.o gcc -o zzj sum.osum.o:sum.c gcc -c -o sum.o sum.cclean: rm -rf *.o z ...
- 微信小程序开发踩坑之旅
项目之始: 一.搭建新项目时出现了 page[pages/XXX/XXX] not found.May be caused by :1. Forgot to add page route in app ...
- 【Kubernetes】架构全图
K8s是什么 Kubernetes是Google开源的容器集群管理系统.它构建在Docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等一整套功能. K8s能做什么 ①容器的自 ...
- 29(30).socket网络基础
转载:https://www.cnblogs.com/linhaifeng/articles/6129246.html 一 客户端/服务器架构 1.硬件C/S架构(打印机) 2.软件C/S架构 互联网 ...
- 基于DDD的微服务设计和开发实战
你是否还在为微服务应该拆多小而争论不休?到底如何才能设计出收放自如的微服务?怎样才能保证业务领域模型与代码模型的一致性?或许本文能帮你找到答案. 本文是基于 DDD 的微服务设计和开发实战篇,通过借鉴 ...
- 使用Python完成SAP客户端的打开和系统登陆
最近小爬一直思忖着如何将以前写的一些半自动化程序转为全自动化,这其中就涉及到SAP的打开和登录过程.我们都知道,SAP原生的“脚本录制和回放”功能是在用户进入到某一个SAP”用户指定系统“后才可以启用 ...
