SpringCloud 组件之集成 Hystrix 实现断路器
一、Hystrix 是什么?
Hystrix 由 Netflix 开源,实现断路器。在微服务架构中,通常有多层服务调用。较低级别的服务的服务故障可能导致用户级联故障。当对特定服务的呼叫达到一定阈值时(Hystrix 中默认为5秒20次),断路器会被打开,向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。
Hystrix 的主要优点之一是它收集关于每个 HystrixCommand 的一套指标。Hystrix 提供仪表盘以有效的方式显示每个断路器的运行状况。
二、创建项目实现断路器
1.项目说明
Feign 自带断路器,因此我们基于 SpringCloud 组件之集成Feign实现负载均衡 中的工程进行改造。
2.改造接口类 HelloFeignClient
改造接口类 HelloFeignClient,在注解 @FeignClient 上指定 fallback 类 HelloFeignClientHystrix。
改造前:
@FeignClient(name = "service-provider")
public interface HelloFeignClient {
@RequestMapping(value = "/sayHi", method = RequestMethod.GET)
String sayHi();
}
改造后:
@FeignClient(name = "service-provider", fallback = HelloFeignClientHystrix.class)
public interface HelloFeignClient {
@RequestMapping(value = "/sayHi", method = RequestMethod.GET)
String sayHi();
}
3.新建 HelloFeignClientHystrix 类
package com.chaoyue.consumer.feignClient;
import org.springframework.stereotype.Component;
@Component
public class HelloFeignClientHystrix implements HelloFeignClient {
@Override
public String sayHi() {
return "调用失败";
}
}
application.yml 中添加配置:
feign:
hystrix:
enabled: true
4.启动后断路器不生效的处理方法
启动后断路器不生效,将 SpringBoot 和 SpringCloud 依赖的版本降低后问题解决。
SpringBoot 版本由 2.6.3 降为 2.2.4.RELEASE
SpringCloud 版本由 2021.0.0 降为 Hoxton.SR12
修改后的 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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chaoyue</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-openfeign</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>
5.测试
启动服务后,浏览器输入:http://localhost:8082/sayHi
启动 provider 时页面显示:
Hi,我来自端口:8081
关闭 provider 时页面显示:
调用失败
6.Hystrix 仪表盘
Hystrix 仪表盘以有效的方式显示每个断路器的运行状况。
(1).添加依赖
pom.xml 文件中加入依赖 spring-boot-starter-actuator 和 spring-cloud-starter-netflix-hystrix-dashboard,spring-boot-starter-actuator 为服务监控依赖:
<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-dashboard</artifactId>
</dependency>
(2).添加配置
application.yml 文件中添加配置用来暴露 endpoints,由于 endpoints 中会包含很多敏感信息,除了 health 和 info 两个支持直接访问外,其他的默认不能直接访问,所以进行指定:
management:
endpoints:
web:
exposure:
include: hystrix.stream
(3).启动类加入注解
在启动类中加入注解 @EnableHystrixDashboard,开启Hystrix Dashboard:
package com.chaoyue.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
(4).启动类注入 ServletRegistrationBean
在启动类注入 ServletRegistrationBean,该 Bean 的作用是在 Hystrix Dashboard 中打开 hystrix.stream:
package com.chaoyue.consumer;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
(5).启动服务
启动服务。浏览器输入:http://localhost:8082/hystrix
监控 Stream 输入:http://localhost:8082/hystrix.stream,点击【Monitor Stream】