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

来源:互联网 发布:印象笔记破解版 mac 编辑:程序博客网 时间:2024/06/05 19:52
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;@RestControllerpublic 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还可以实现请求缓存,后续再做研究。
疑问:服务故障后,到注册中心主动清除的默认超时时间是多长?

[1] http://start.spring.io/
[2] http://projects.spring.io/spring-cloud/



阅读全文
0 0
原创粉丝点击