SpringCloud微服务系列(5): 服务容错断路器Hystrix

SpringCloud微服务系列(5):  服务容错断路器Hystrix
作者:家辉,日期:2017-08-08 CSDN博客: http://blog.csdn.net/gobitan
摘要:在本系列的前四篇创建了一个高可用Eureka微服务注册中心,一个hello服务,一个服务消费者ribbon-consumer。本文将介绍服务容错断路器Hystrix。

概述
用户的一个请求可能在后端微服务中被拆分成多个服务,如果其中一个服务出现故障,如何进行隔离,尽可能地降低因为单个服务故障对整个系统的影响。这就是本文中的断路器Hystrix。

第一步:在 ribbon-consumer工程上增加对Hystrix的依赖
在pom.xml中添加对Hystrix的依赖,如下:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
第二步:在主类中开启断路器功能
在主类中添加注解@EnableCircuitBreaker开启断路器功能。
也可用@SpringCloudApplication注解来替换如下三个注解:
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker

第三步:改造服务消费方式
[1] 新增HelloService类
package cn.dennishucd.ribbonconsumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

public class HelloService {

    @Autowired
    RestTemplate restTemplate;
    
    @HystrixCommand(fallbackMethod = "helloFallback")
    public String helloService() {
        return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
    }
    
    public String helloFallback() {
        return "error";
    }
}
通过为服务方法helloService增加@HystrixCommand注解,并定义服务降级回调方法fallbackMethod,当服务失败时就调用该方法。

[2] 修改ConsumerController类
package cn.dennishucd.ribbonconsumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {
    @Autowired
    HelloService helloService;

    @RequestMapping(value = "ribbon-consumer", method = RequestMethod.GET)
    public String helloConsumer() {
        return helloService.helloService();
    }
}
修改后的类,通过调用前面的服务实现服务消费,而不是在Controller中直接实现。

第四步:修改ribbon-consumer的版本
为了与未加入断路器的之前的ribbon-consumer区别,将它的版本由原来默认的0.0.1改为0.0.2。具体修改位置位于pom.xml中如下:
<groupId>cn.dennishucd</groupId>
<artifactId>ribbonconsumer</artifactId>
<version>0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>
第五步:调测和验证断路器
准备工作:
[1] 启动两个eureka注册中心:eureka1和eureka2; 启动方法参考系列3.
[2] 启动两个hello-service服务注册到注册中心; 启动方法参考系列4.

这时,分别做如下测试步骤:
[1] 启动老版本的ribbon-consumer,即java -jar ribbonconsumer-0.0.1-SNAPSHOT.jar。连续两次请求 http://localhost:9000/ribbon-consumer服务,可以看到分别请求了启动在8081和8082的hello-service服务,每次都返回”Hello World!”; 然后将8081端口的hello-service停掉,然后再连续两次请求ribbon-consumer服务,可以看到一次返回500错误请求失败,一次返回”Hello World!”; 
[2] 停止老版本,启动新版本的ribbon-consumer,即java -jar ribbonconsumer-0.0.2-SNAPSHOT.jar。连续两次请求 http://localhost:9000/ribbon-consumer服务,可以看到分别请求了启动在8081和8082的hello-service服务,每次都返回”Hello World!”; 然后将8081端口的hello-service停掉,然后再连续两次请求ribbon-consumer服务,可以看到一次返回error,一次返回”Hello World!”; 这说明断路器发挥了作用,当服务失败时,自动返回自定义的回调返回error。
[3] 经过测试,当8081的服务发生故障,过一会儿后(具体时间待确定)之后,ribbon每次请求都会成功。ribbon不会再将请求发往8081。因为,当一个服务发生故障后,注册器周期性地会把发生故障的服务清理出去。此时,后台的服务就只剩下8082了。

Hystrix还可以实现请求缓存,后续再做研究。
疑问:服务故障后,到注册中心主动清除的默认超时时间是多长?




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gobitan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值