spring cloud hystrix
简介
hystrix通过服务隔离、熔断(也可以称为断路)、降级等手段控制依赖服务的延迟与失败。
netflix hystrix
本篇中主要讲解对spring cloud 对hystrix的集成,至于如何单独使用hystrix可以参考我分享的pdf.
spring cloud hystrix
引入依赖
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
spring-cloud-starter-hystrix
核心jar
spring-cloud-starter-hystrix-dashboard
监控jar
EnableCircuitBreaker
使用EnableCircuitBreaker
或者 EnableHystrix
表明Spring boot
工程启用hystrix,两个注解是等价的.
Application.Java
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
其中EnableHystrixDashboard
注解表示启动对hystrix的监控,后面会用到
随后模拟一个调用三方依赖服务
controller
-> service
-> dependency service
controller
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
service
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
dependency service
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
HystrixCommand
表明该方法为hystrix包裹,可以对依赖服务进行隔离、降级、快速失败、快速重试等等hystrix相关功能
该注解属性较多,下面讲解其中几个
- fallbackMethod 降级方法
- commandProperties 普通配置属性,可以配置HystrixCommand对应属性,例如采用线程池还是信号量隔离、熔断器熔断规则等等
- ignoreExceptions 忽略的异常,默认
HystrixBadRequestException
不计入失败 - groupKey() 组名称,默认使用类名称
- commandKey 命令名称,默认使用方法名
Dashboard
运行工程,可以访问 http://localhost:9090/hystrix.stream 获取dashboard信息,默认最大打开5
个终端获取监控信息,可以增加delay
参数指定获取监控数据间隔时间
直接访问hystrix.stream肯定是不明智的,官方提供监控hystrix-dashboard-#.#.#.war包,下载后放入tomcat中,得到如下界面
输入http://localhost:9090/hystrix.stream 点击 Monitor stream
进入Dashboard界面
访问 http://localhost:/9090/call 观察进入Dashboard变化
o