[关闭]
@coldxiangyu 2017-06-08T06:42:46.000000Z 字数 2444 阅读 1211

Spring Cloud(二、断路器Hystrix)

WEB框架


我们在上一篇文章介绍了Spring Cloud的服务注册、消费以及负载均衡。这篇讲一下Netfilx Hystrix,断路器。

HystrixNetflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。

在微服务架构中,我们将系统拆分为一个个不同的单元,比如我们的Eureka注册中心、服务提供方、Ribbon、Feign消费者,它们彼此通过服务注册订阅的方式进行相互依赖,且分别运行在不同的进程中,通过远程调用的方式执行。
如果遇到网络延迟或者高负载导致某个服务无法及时响应,导致请求积压阻塞,最终会拖垮与此相关的整个业务流程。
这也是每一个微服务架构面临的主要问题,Hystrix也就应运而生了。
有了Hystrix,它可以监控整个系统中的故障,并返回错误信息,避免请求积压,故障蔓延。

下面我们来看看如何使用Hystrix
重新启动我们的服务注册中心:http://localhost:1111
不进行服务注册,直接运行消费者ribbon之后,访问http://localhost:3333/add,可以看到以下错误页面。
image_1bi2ntecql5v161f8dchb113t9.png-23.6kB

我们在ribbon中加入Hystrix,在pom中增加hystrix依赖:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-hystrix</artifactId>
  4. </dependency>

eureka-ribbon的主类RibbonApplication中使用@EnableCircuitBreaker注解开启断路器功能:

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableCircuitBreaker
  4. public class RibbonApplication {
  5. @Bean
  6. @LoadBalanced
  7. RestTemplate restTemplate() {
  8. return new RestTemplate();
  9. }
  10. public static void main(String[] args) {
  11. SpringApplication.run(RibbonApplication.class, args);
  12. }
  13. }

改造原来的服务消费方式,新增ComputeService类,在使用ribbon消费服务的函数上增加@HystrixCommand注解来指定回调方法。

  1. @Service
  2. public class ComputeService {
  3. @Autowired
  4. RestTemplate restTemplate;
  5. @HystrixCommand(fallbackMethod = "addServiceFallback")
  6. public String addService() {
  7. return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
  8. }
  9. public String addServiceFallback() {
  10. return "error";
  11. }
  12. }

提供rest接口的Controller改为调用ComputeService的addService

  1. @RestController
  2. public class ConsumerController {
  3. @Autowired
  4. private ComputeService computeService;
  5. @RequestMapping(value = "/add", method = RequestMethod.GET)
  6. public String add() {
  7. return computeService.addService();
  8. }
  9. }

这样一来,在调用addService方法异常之后,直接调用回调函数进行返回。
我们启动ribbon消费者验证进行验证,访问http://localhost:3333/add
image_1bi363ie1l6bed711ol7qgucam.png-13.3kB

与Ribbon相比,Feign使用Hystrix就方便多了,因为Feign本身是依赖Hystrix的,我们无需修改pom。

我们需要对之前的eureka-feign进行改造,使用@FeignClient注解中的fallback属性指定回调类:

  1. @FeignClient(value = "compute-service", fallback = ComputeClientHystrix.class)
  2. public interface ComputeClient {
  3. @RequestMapping(method = RequestMethod.GET, value = "/add")
  4. Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
  5. }

创建回调类ComputeClientHystrix,实现@FeignClient的接口,此时实现的方法就是对应@FeignClient接口中映射的fallback函数。

  1. @Component
  2. public class ComputeClientHystrix implements ComputeClient {
  3. @Override
  4. public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
  5. return -9999;
  6. }
  7. }

启动Feign消费者,访问http://localhost:3333/add
image_1bi36jd2aso6out1f881iqmnb313.png-9.5kB
本文源码已上传github:
ribbon-hystrix
feign-hystrix

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注