Hystrix断路器
微服务架构中,通常多个服务之间会相互调用,一些基础服务的故障可能会导致级联故障,进而导致整个系统不可用,这就是雪崩效应。服务的雪崩效应是一种因为服务提供者的不可用导致服务消费者的不可用,并将这种不可用逐渐放大的过程。
Hystrix是一个用于处理分布式系统的延迟和容错的开源库。分布式系统中,有许多依赖会因为超时、异常等原因导致调用失败。Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高系统稳定性(类似于电路中的保险丝)。
在Hystrix中有一个降级操作(Fallback),对于查询操作,我们可以实现一个fallback方法,当请求方异常时,可以使用fallback方法的返回值,而fallback方法的返回值一般设置的默认值或者来自于缓存。
项目构建,还是在之前的consumer基础上进行修改:
添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
主类添加使能注解**@EnableCircuitBreaker**
package com.zh.cloud;
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.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient // 开启EurekaClient功能
@EnableFeignClients
@EnableCircuitBreaker //开启hystrix
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
application.yml添加配置:
feign:
hystrix:
enabled: true #开启熔断策略
@FeignClient中添加fallbackFactory属性
package com.zh.cloud.api;
import com.zh.cloud.fallback.UserControllerFallback;
import com.zh.cloud.model.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@FeignClient(value = "provider", fallbackFactory = UserControllerFallback.class) //调用的服务名
public interface UserControllerInter {
@GetMapping("/v1/user/list") //此处的调用路径必须与服务提供者的路径相同
public List<User> queryUser();
}
编写UserControllerFallback类
package com.zh.cloud.fallback;
import com.zh.cloud.api.UserControllerInter;
import com.zh.cloud.model.User;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class UserControllerFallback implements FallbackFactory<UserControllerInter> {
@Override
public UserControllerInter create(Throwable throwable) {
return new UserControllerInter() {
@Override
public List<User> queryUser() {
List<User> list = new ArrayList<>();
User user = new User();
user.setId(999);
user.setName("降级名称");
user.setAge(88);
user.setAddress("降级地址");
list.add(user);
return list;
}
};
}
}
注:若接口有多个,则只需要对需要降级的controller中的方法进行重写,其余的不用动即可。
启动之后测试,看到可以正常访问,kill掉provider之后再次访问,就可以看到熔断的效果了。

完整代码地址:
https://gitee.com/superbutton/spring-cloud-study/tree/develop/springcloud-hystrix-application
Hystrix可视化数据监控Dashboard
Hystrix还提供了准实时的调用监控(Hystrix Dashboard),Hystrix会持续记录所有通过Hystrix发起的请求执行信息。并以统计图表和图形的形式展示给用户,包括每秒执行的请求数量,成功、失败数量等。
项目构建(在hystrix基础上搭建):
1、添加:Hystrix、Actuator、Hystrix-dashboard的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</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-dashboard</artifactId>
</dependency>
2、主类添加@EnableHystrixDashboard 开启Dashboard并
注册HystrixMetricsStreamServlet
package com.zh.cloud;
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.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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
@EnableDiscoveryClient // 开启EurekaClient功能
@EnableFeignClients
@EnableCircuitBreaker //开启hystrix
@EnableHystrixDashboard
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
public ServletRegistrationBean registrationBean() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
注:在2.x之前的版本中,会自动注入该Servlet的,但是在2.x之后的版本,没有自动注册该Servlet。
访问如下地址就可以查看服务的流数据,注:ip和端口是对应服务的ip+端口
http://ip:端口/hystrix.stream
如下图所示:
查看Hystrix Dashboard,注:ip和端口是对应服务的ip+端口
http://ip:端口/hystrix
如下图所示:
调用consumer的接口并不断刷新,模拟高并发的场景,观察Hystrix Dashboard界面的变化。
完整代码如下:
https://gitee.com/superbutton/spring-cloud-study/tree/develop/springcloud-hystrix-dashboard
相关解释如下: