前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud 中 Hystrix 的使用

SpringCloud 中 Hystrix 的使用

作者头像
北漂的我
发布2019-12-16 17:20:53
7400
发布2019-12-16 17:20:53
举报
文章被收录于专栏:北漂的我北漂的我

1. 在 pom.xml 中添加依赖

代码语言:javascript
复制
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2. 在启动类上添加注解 @EnableHystrix

3. 在 Controller 的方法上添加注解 @HystrixCommand(fallbackMethod = "defaultCallHello")

在 Controller 中定义 defaultCallHello 方法, 作为回退调用方法.

4. @HystrixCommand 注解的配置详解

@HystrixCommand 的配置除了 fallbackMethod 还有很多配置

相关的可配置项可以参考 com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager 类

execution.isolation.strategy 用来指定隔离策略, THREAD 线程隔离, SEMAPHORE 信号量隔离

execution.isolation.thread.timeoutInMilliseconds 用于配置HystrixCommand执行的超时时间

execution.timeout.enabled 用来配置是否启用 execution.isolation.thread.timeoutInMilliseconds 配置, 默认是 true

execution.isolation.thread.interruptOnTimeout 用来配置HystrixCommand 超时后, 是否中断它, 默认是 true

execution.isolation.semaphore.maxConcurrentRequests 用来配置使用 信号量策略时最大的并发请求数

fallback.isolation.semaphore.maxConcurrentRequests 用来配置如果并发数达到这个值, 请求会被拒绝和抛出异常, 并且fallback不会被调用, 默认 10

fallback.enabled 用来配置当执行失败或者请求被拒绝时, 是否会尝试调用 hystrixCommand.getFallback(), 默认是 true

circuitBreaker.enabled 用来跟踪 circuit 的健康性, 如果未达标则让 request 短路, 默认值 true

circuitBreaker.requestVolumeThreshold 用来设置一个 rolling window 内最小的请求数. 如果设置20, 那么当一个rolling window的时间内(比如一个 rolling window 是 10秒) 收到19个请求, 即使19个请求全都失败也不会触发 circuit break, 默认值 20

未完待续............

5. Feign 整合 Hystrix 服务容错

如同之前一样在启动类上加上 @EnableHystrix

在 application.properties 文件中添加

代码语言:javascript
复制
feign.hystrix.enabled=true

在 FeignClient 接口类上的 @FeignClient 注解中添加 fallback, fallback 类就是 FeignClient 接口的一个实现类

6. FallbackFactory 的整合

通过 Feign 整合 Hystrix 已经能够实现服务不可用进行回退, 如果你想知道触发回退的原因可以使用 FallbackFactory 来实现回退功能.

fallback 和 fallbackFactory 的区别就是 fallbackFactory 可以获取异常原因. S

7. Hystrix 监控

当前环境使用的是 SpringBoot 2.x , SpringCloud 版本是 Greenwich.SR2

① 引入依赖 spring-cloud-starter-netflix-hystrix-dashboard

代码语言:javascript
复制
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

② 在启动类上添加注解 @EnableHystrixDashboard, 但是值得注意的是在这之前 @EnableHystrix 注解是必须有的.

访问 http://ip-yuming:port/hystrix

③ 引入依赖 spring-boot-starter-actuator

代码语言:javascript
复制
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
代码语言:javascript
复制
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

④ 暴露监控端点 hystrix.stream

在 application.properties 中添加

代码语言:javascript
复制
management.endpoints.web.exposure.include=hystrix.stream

访问 http://ip-yuming:port/actuator/hystrix.stream

8. Turbine 聚合集群数据

① 添加依赖

代码语言:javascript
复制
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>

② 启动类上添加注解 @EnableTurbine , @EnableDiscoveryClient

③ application.properties 配置文件中添加

代码语言:javascript
复制
eureka.client.service-url.defaultZone=http://xiaohaibo:10010@localhost:8761/eureka/
turbine.app-config=fsh-substitution,fsh-house
turbine.aggregator.cluster-config=default
turbine.cluster-name-expression=new String("default")

④ 访问 http://ip-yuming:port/turbine.stream, 就有反应

然后访问 hystrix 监控面板

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档