SpringCloud 中 Hystrix 的使用

北城余情 提交于 2019-12-10 18:47:54

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

1. 在 pom.xml 中添加依赖

<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 文件中添加     

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    

<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

<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>

    ④ 暴露监控端点 hystrix.stream

        在 application.properties 中添加

management.endpoints.web.exposure.include=hystrix.stream

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

8. Turbine 聚合集群数据

    ① 添加依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>

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

    ③ application.properties 配置文件中添加       

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 监控面板

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!