vlambda博客
学习文章列表

feign + hystrix 监控和熔断测试

修改sp09-feign项目
pom.xml 添加 hystrix 起步依赖

  • feign 没有包含完整的 hystrix 依赖


    右键点击项目,编辑起步依赖,添加hystrix依赖
    feign + hystrix 监控和熔断测试


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

主程序添加 @EnableCircuitBreaker

package cn.tedu.sp09;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableCircuitBreaker@EnableFeignClients@EnableDiscoveryClient@SpringBootApplicationpublic class Sp09FeignApplication {
public static void main(String[] args) { SpringApplication.run(Sp09FeignApplication.class, args); }
}

sp09-feign 配置 actuator,暴露 hystrix.stream 监控端点

actuator 依赖

查看pom.xml, 确认已经添加了 actuator 依赖

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

application.yml 暴露 hystrix.stream 端点

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

启动服务,查看监控端点

http://localhost:3001/actuator
feign + hystrix 监控和熔断测试

hystrix dashboard

启动 hystrix dashboard 服务,填入 feign 监控路径,开启监控
访问 http://localhost:4001/hystrix

  • 填入 feign 监控路径:
    http://localhost:3001/actuator/hystrix.stream

  • 访问微服务,以产生监控数据

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/

熔断测试

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