vlambda博客
学习文章列表

hystrix dashboard 断路器仪表盘

hystrix 对请求的降级和熔断,可以产生监控信息,hystrix dashboard可以实时的进行监控

sp07-hystrix 项目添加 actuator,并暴露 hystrix 监控端点

actuator 是 spring boot 提供的服务监控工具,提供了各种监控信息的监控端点

management.endpoints.web.exposure.include 配置选项,
可以指定端点名,来暴露监控端点

如果要暴露所有端点,可以用 “*”

hystrix dashboard 断路器仪表盘

pom.xml 添加 actuator 依赖

右键点击项目或pom.xml, 编辑起步依赖, 添加 actuator 依赖
hystrix dashboard 断路器仪表盘

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>

调整 application.yml 配置,并暴露 hystrix.stream 监控端点

spring: application: name: hystrix server: port: 3001 eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka ribbon: MaxAutoRetriesNextServer: 1 MaxAutoRetries: 1 OkToRetryOnAllOperations: true hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 500
management: endpoints: web: exposure: include: hystrix.stream

访问 actuator 路径,查看监控端点

  • http://localhost:3001/actuator

hystrix dashboard 断路器仪表盘

Hystrix dashboard 仪表盘

hystrix dashboard 断路器仪表盘

新建 sp08-hystrix-dashboard 项目
hystrix dashboard 断路器仪表盘

hystrix dashboard 断路器仪表盘

pom.xml

<?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 https://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.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.tedu</groupId> <artifactId>sp08-hystrix-dashboard</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sp08-hystrix-dashboard</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version> </properties>
<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>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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>

application.yml

spring: application: name: hystrix-dashboard server: port: 4001
eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

主程序添加 @EnableHystrixDashboard 和 @EnableDiscoveryClient

package cn.tedu.sp08;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableDiscoveryClient@EnableHystrixDashboard@SpringBootApplicationpublic class Sp08HystrixDashboardApplication {
public static void main(String[] args) { SpringApplication.run(Sp08HystrixDashboardApplication.class, args); }
}

启动,并访问测试

hystrix dashboard 断路器仪表盘

访问 hystrix dashboard

  • http://localhost:4001/hystrix

hystrix dashboard 断路器仪表盘

填入 hystrix 的监控端点,开启监控

  • http://localhost:3001/actuator/hystrix.stream

hystrix dashboard 断路器仪表盘

  • 通过 hystrix 访问服务多次,观察监控信息

http://localhost:3001/item-service/35

http://localhost:3001/user-service/7
http://localhost:3001/user-service/7/score?score=100

http://localhost:3001/order-service/123abc
http://localhost:3001/order-service/

hystrix dashboard 断路器仪表盘

hystrix dashboard 断路器仪表盘

hystrix 熔断

整个链路达到一定的阈值,默认情况下,10秒内产生超过20次请求,则符合第一个条件。
满足第一个条件的情况下,如果请求的错误百分比大于阈值,则会打开断路器,默认为50%
Hystrix的逻辑,先判断是否满足第一个条件,再判断第二个条件,如果两个条件都满足,则会开启断路器

断路器打开 5 秒后,会处于半开状态,会尝试转发请求,如果仍然失败,保持打开状态,如果成功,则关闭断路器

使用 apache 的并发访问测试工具 ab

http://httpd.apache.org/docs/current/platform/windows.html#down

  • 用 ab 工具,以并发50次,来发送20000个请求
ab -n 20000 -c 50 http://localhost:3001/item-service/35
  • 断路器状态为 Open,所有请求会被短路,直接降级执行 fallback 方法

hystrix 配置

https://github.com/Netflix/Hystrix/wiki/Configuration

  • hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
    请求超时时间,超时后触发失败降级

  • hystrix.command.default.circuitBreaker.requestVolumeThreshold
    10秒内请求数量,默认20,如果没有达到该数量,即使请求全部失败,也不会触发断路器打开

  • hystrix.command.default.circuitBreaker.errorThresholdPercentage
    失败请求百分比,达到该比例则触发断路器打开

  • hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds
    断路器打开多长时间后,再次允许尝试访问(半开),仍失败则继续保持打开状态,如成功访问则关闭断路器,默认 5000