Spring Cloud实战系列(四) - 熔断器Hystrix
前言
在微服务框架 Spring Cloud
中,可以用 RestTemplate
配合 Ribbon
或 Feign
实现 服务与服务 之间的 相互调用。
为了保证服务的 高可用,单个服务 通常会采用 集群部署。由于 网络原因,服务并不能保证 100%
的 可用性,如果 单个服务 出现问题,调用这个服务就会出现 线程阻塞,此时若有 大量的请求 涌入,Servlet
容器的 线程资源 会被耗尽,导致 服务瘫痪。
服务与服务之间具有 依赖性,故障会传播,导致整个微服务系统发生 雪崩。
正文
Netflix
开源了 Hystrix
组件,实现了 熔断器模式, Spring Cloud
对这个组件进行了整合。在微服务架构中,一个请求需要调用 多个服务 是非常常见的,如下图所示:
下层 的服务如果出现故障,会导致 故障级联效应。当对特定的服务的调用的 失败次数 达到一个 阀值(Hystrix
是 5
秒 20
次),断路器 将会被打开。
断路器 打开后 底层服务 将会隔断,可用避免 故障级联 问题,上层服务 调用 fallback
方法直接返回一个 固定值。
1. 在Ribbon上使用熔断器
在 pom.xml
文件中引入 hystrix
的 起步依赖 spring-cloud-starter-hystrix
:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
在应用的启动类上使用 @EnableHystrix
开启 hystrix
的功能。
@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
使用注解 @HystrixCommand
标记调用失败时需要熔断的方法,fallbackMethod
属性指定 降级方法 的 方法名 为 fallback
。
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "fallback")
public String requestForHiService(String name){
return restTemplate.getForObject("http://SERVICE-HI/hi?name=" + name, String.class);
}
public String fallback(String name){
return "Fallback method invoked, name is " + name;
}
}
2. 在Feign上使用熔断器
Feign
是自带 断路器 的,不过需要在 配置文件 中开启 hystrix
的配置。
feign:
hystrix:
enabled: true
Hystrix
支持 降级回退 操作,当 发生熔断 或 出现错误 时,调用会执行 默认代码路径。
@FeignClient(value = "service-hi", fallback = HelloServiceFallback.class)
public interface HelloService {
@RequestMapping(value = "/hi", method = RequestMethod.GET)
String sayHi(@RequestParam(value = "name") String name);
}
通过设置 fallback
属性为实现 降级回退 的 类,来启用 @FeignClient
的 失败降级。
@Service
public class HelloServiceFallback implements HelloService {
@Override
public String sayHi(String name) {
return "Fallback method invoked, name is " + name;
}
}
如果需要获取导致 回退触发 的原因,可以指定 @FeignClient
注解内部的 fallbackFactory
属性,fallbackFactory
属性和 fallback
属性不能一起使用。
@FeignClient(value = "service-hi", fallbackFactory = HelloServiceFallbackFactory.class)
public interface HelloService {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHi(@RequestParam(value = "name") String name);
}
然后提供一个 FallbackFactory
的 实现类,实现类指定 泛型参数 为 HelloService
。
@Component
public class HelloServiceFallbackFactory implements FallbackFactory<HelloService> {
@Override
public HelloService create(Throwable throwable) {
return new HelloService() {
@Override
public String sayHi(String name) {
return "Fallback method invoked, name is "
+ name + ", message is " + throwable.getMessage();
}
};
}
}
3. Hystrix Dashboard监控熔断器的状态
Hystrix Dashboard
是一个 监控熔断器 状况的组件,提供了 数据监控 和 图形界面。
3.1. 在Ribbon中使用Hystrix Dashboard
在加入 spring-cloud-starter-hystrix
依赖的基础上,加入下面的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
在应用程序 启动类 已经加上 @EnableHystrix
的基础上,加入 @EnableHystrixDashboard
注解,代码如下:
@EnableHystrix
@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceRibbonApplication {
}
启动应用程序,访问 http://localhost:8765/hystrix.stream
,可以查看 熔断器 的 数据指标。访问 http://localhost:8765/hystrix
,查看 Hystrix Dashboard
的界面。在界面上填写 数据指标 的 URL
地址,点击 Monitor Stream
进入页面,如图所示:
3.2. 在Feign中使用Hystrix Dashboard
在加入 spring-cloud-starter-hystrix
依赖的基础上,加入下面的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
在启动程序上加上 @EnableHystrixDashboard
注解 开启 Hystrix Dashboard
,完整代码如下:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableHystrix
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
完成上面两步配置,即可开启 Feign
的 Hystrix Dashboard
功能。
参考
- 方志朋《深入理解Spring Cloud与微服务构建》
欢迎关注技术公众号: 零壹技术栈
本帐号将持续分享后端技术干货,包括虚拟机基础,多线程编程,高性能框架,异步、缓存和消息中间件,分布式和微服务,架构学习和进阶等学习资料和文章。