Spring Cloud (四):断路器(Hystrix)
在微服务架构中, 根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,程资源会被消耗Servlet容器的线完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
为了解决这个问题,业界提出了断路器模型。
一、断路器简介
Netflix 开源了Hystrix组件,实现了断路器模式,Spring Cloud 对这一组件进行了整合。在微服务中,一个请求调用多个服务是很常见的事情。如下图:
较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。
断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。
二、 准备工作
接着上篇文章的工程,启动端口号为8761 的 eureka-server 工程,启动 端口号为8762 的 eureka-client 工程。
三、在ribbon 使用断路器
改造 eureka-ribbon 工程的代码,首先在pom文件里 加入依赖 spring-cloud-starter-netflix-hystrix
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
在启动类上 加入注解 @EnableHystrix 开启 Hystrix:
@EnableHystrix // 引入断路器模型
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
System.out.println("================eureka-ribbon 启动成功=================");
}
@Bean
@LoadBalanced // 开启负载均衡 功能
RestTemplate restTemplate(){
return new RestTemplate();
}
}
改造 HelloService 类,在hiService方法上加上 @HystrixCommand 注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回一个字符串,字符串为 "hi, "+ name + “, sorry error!”;
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name){
return restTemplate.getForObject("http://EUREKA-CLIENT/hi?name="+name, String.class);
}
public String hiError(String name){
return "hi, "+ name + ", sorry error!";
}
}
启动 eureka-ribbon 工程,访问 http://localhost:8764/hi?name=lbh,浏览器 显示: hi lbh ,i am from port:8762
当关闭eureka-client 工程时,再次访问,浏览器 显示: hi, lbh, sorry error!
此时,断路器起作用了。当eureka-client 工程不可用时,eureka-ribbon 调用 eureka-client 的API接口时,会执行快速失败,返回一组字符串,而不是等待响应超时,这很好控制了 容器的线程阻塞。
四、Feign 中使用断路器
feign 是自带断路器功能的,在 D版本的spring cloud 之后,它没有默认开启,需要在配置文件中开启:
feign:
hystrix:
enabled: true # 开启 feign 断路器模式
基于 eureka-feign 工程进行改造,只需要在 FeignClient 的 SchedualServiceHi接口的注解中加上fallback的指定类就行了:
/**
*
* 注: 启用断路器 只需要在 @FeignClient()注解中加上 fallback 属性就可以了,SchedualServiceHiHystric 实现改接口。
* Created by Advancer on 2019/1/17 10:46.
* auth: lbh
*/
@Component
@FeignClient(value = "eureka-client", fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
@RequestMapping(value = "/hi", method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
SchedualServiceHiHystric需要实现SchedualServiceHi 接口,并注入到Ioc容器中,代码如下:
@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
@Override
public String sayHiFromClientOne(String name) {
return "sorry "+ name;
}
}
启动 eureka-feign 工程,浏览器 打开 http://localhost:8765/hi?name=lbh ,浏览器 显示: hi lbh ,i am from port:8762
没有启动的情况下 显示: sorry lbh
此时,断路器起到作用了。
源码下载地址:
https://github.com/JavaAdvancer/eurekademo
参考文章:
https://www.fangzhipeng.com/springcloud/2017/07/12/sc04-hystrix/