Hystrix为我们提供了资源隔离,熔断降级的功能。
降级测试
因使用了 Feign ,其本身依赖与 Hystrix,这里便不需修改依赖文件;否则添加依赖。
修改配置文件,启用 Hystrix,这里为了测试将 timeout 时间修改较小。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| hystrix: threadpool: hello: # 对hello服务进行配置 coreSize: 20 # 修改其核心线程熟练为20 command: hello: circuitBreaker: requestVolumeThreshold: 20 # 修改阈值为20,测试熔断可修改更小 execution: timeout: enabled: true # 执行超时开启 isolation: thread: timeoutInMilliseconds: 1000 # 线程超时设定为 1000
feign: hystrix: enabled: true # 开启 hystrix
|
1 2 3 4 5 6 7 8 9 10 11 12
| @Component public class HelloServiceHystrix implements HelloService {
...
@Override public String sleep() { return "sleep failed"; } ... }
|
在注解中指定降级类,失败时会去执行同名方法
1 2 3 4 5 6
| @FeignClient(name = "spring-cloud-producer", path = "/hello", fallback = HelloServiceHystrix.class) public interface HelloService { ... }
|
开启consumer, enreka, producer(可不开启),访问 http://localhost:9001/sleep。

看到这里不是抛出的错误信息而是我们在降级方法中返回的信息。
Hystrix Dashboard
hystrix 的相关信息(健康度)可以通过 spring boot actuator 暴露出来,然后通过 hystrix dashboard 可视化查看监控信息。
- 开启 actuator, 暴露 hystrix.stream
在依赖文件中添加相关组件:
1 2 3 4 5 6 7 8
| <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>
|
添加 spring-cloud-starter-netflix-hystrix 是为了添加 hystrix-metrics-event-stream 启用监控。
然后修改配置文件:
1 2 3 4 5
| management: endpoints: web: exposure: include: hystrix.stream
|
将 相关信息 加入 actuator 的 endpoints 暴露出来。
最后在启动类增加注解 @EnableCircuitBreaker 。
1 2 3 4 5 6 7 8 9 10 11
| @EnableDiscoveryClient @EnableFeignClients @EnableCircuitBreaker @SpringBootApplication public class ConsumerApplication {
public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }
}
|
启动项目,访问任意接口后再访问 /actuator/hystrix.stream 查看。

- 通过 Dashboard 查看 Hystrix Stream
新建项目hystrix.dashboard,并添加依赖
依赖文件
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>lyyljs.cloud</groupId> <artifactId>hystrix.dashboard</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hystrix.dashboard</name>
<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
|
配置服务
1 2 3 4 5
| spring: application: name: hystrix-dashboard server: port: 8010
|
在启动类添加注解
1 2 3 4 5 6 7 8 9
| @SpringBootApplication @EnableHystrixDashboard public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }
}
|
然后启动,访问http://localhost:8010/hystrix。

填写要hystrix.stream的地址,设定delay,title,进入监控。

具体含义参考wiki:

Turbine
对op来说,我们更希望能监控整个系统的数据。Turbine 将 hystrix stream 聚合为 turbine stream 从而可以展示整个系统的状况,并且支持eureka自动发现。
修改项目hystrix.dashboard
添加依赖
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency>
|
修改配置文件
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
| spring: application: name: hystrix-dashboard server: port: 8010 register: port1: 8000 port2: 8001 port3: 8002 eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.register.port1}/eureka/
turbine: aggregator: clusterConfig: default # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问 appConfig: spring-cloud-consumer # 配置Eureka中的serviceId列表,表明监控哪些服务 clusterNameExpression: "'default'" # 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称 # 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default # 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC
|
1 2 3 4
| @SpringBootApplication @EnableHystrixDashboard @EnableTurbine public class Application {
|
启动,访问 http://localhost:8010/turbine.stream 查看聚合信息

将turbine.stream地址填写在hystrix dashboard中,则可在 hystrix dashboard 中查看可视化信息
