断路器 Hystrix
也就是当500的时候,报错的时候,会显示固定的信息,所以就理解成断路,断路以后会有这个断路器来显示相关的信息,用于告知用户或前端。
断路器入门
导包
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>
修改Feign注解
@FeignClient(value = "PRODUCT-DATA-SERVICE",fallback = FaultClient.class)
错误类
要实现接口目标接口,和改注解的那个类要呼应
package com.msims.ribbon.feignClient;import common.CommonResult;import org.springframework.stereotype.Component;@Componentpublic class FaultClient implements StudentClientFeign{public CommonResult fault(){return new CommonResult(0,"接口不可用",null);}}
配置文件
feign.hystrix.enabled: true
断路器监控
主启动
import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import cn.hutool.core.util.NetUtil;@SpringBootApplication@EnableHystrixDashboardpublic class ProductServiceHystrixDashboardApplication {public static void main(String[] args) {int port = 8020;if(!NetUtil.isUsableLocalPort(port)) {System.err.printf("端口%d被占用了,无法启动%n", port );System.exit(1);}new SpringApplicationBuilder(ProductServiceHystrixDashboardApplication.class).properties("server.port=" + port).run(args);}}
导包
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><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><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency></dependencies>
配置文件
spring:application:name: hystrix-dashboard
给其它feign配置
@EnableCircuitBreaker
访问地址
打开监控地址
http://localhost:8020/hystrix
输入框输入
这个输入的是需要监控的feign地址,格式:http://服务地址:端口号/actuator/hystrix.stream
http://localhost:8012/actuator/hystrix.stream
