断路器 Hystrix
也就是当500的时候,报错的时候,会显示固定的信息,所以就理解成断路,断路以后会有这个断路器来显示相关的信息,用于告知用户或前端。

断路器入门

导包

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  4. </dependency>

修改Feign注解

@FeignClient(value = "PRODUCT-DATA-SERVICE",fallback = FaultClient.class)

错误类

要实现接口目标接口,和改注解的那个类要呼应

  1. package com.msims.ribbon.feignClient;
  2. import common.CommonResult;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class FaultClient implements StudentClientFeign{
  6. public CommonResult fault(){
  7. return new CommonResult(0,"接口不可用",null);
  8. }
  9. }

配置文件

  1. feign.hystrix.enabled: true

image.png

断路器监控

断路器监控需要单开一个项目来适配

主启动

  1. import org.springframework.boot.autoconfigure.SpringBootApplication;
  2. import org.springframework.boot.builder.SpringApplicationBuilder;
  3. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  4. import cn.hutool.core.util.NetUtil;
  5. @SpringBootApplication
  6. @EnableHystrixDashboard
  7. public class ProductServiceHystrixDashboardApplication {
  8. public static void main(String[] args) {
  9. int port = 8020;
  10. if(!NetUtil.isUsableLocalPort(port)) {
  11. System.err.printf("端口%d被占用了,无法启动%n", port );
  12. System.exit(1);
  13. }
  14. new SpringApplicationBuilder(ProductServiceHystrixDashboardApplication.class).properties("server.port=" + port).run(args);
  15. }
  16. }

导包

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-actuator</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  21. </dependency>
  22. </dependencies>

配置文件

  1. spring:
  2. application:
  3. name: hystrix-dashboard

给其它feign配置

@EnableCircuitBreaker

访问地址

打开监控地址
http://localhost:8020/hystrix
输入框输入
这个输入的是需要监控的feign地址,格式:http://服务地址:端口号/actuator/hystrix.stream

  1. http://localhost:8012/actuator/hystrix.stream