初识SpringCloud|第四篇|Hystrix

​ 在微服务架构中,业务被分为一个个微小的服务,服务和服务之间可以相互调用(RPC),为保证高可用性,单个服务通常会集群部署,由于网络或者其他众多原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会陷入线程阻塞的状态,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。为了解决这个问题,业界提出了”断路器”模型。

断路器简介

1
2
Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.
----摘自官网

​ Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的,如下图:

img

​ 较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。

img

准备工作

​ 这篇文章基于上一篇文章的工程,首先启动上一篇文章的工程,启动eureka-server 工程;启动eureka-client工程,它的端口为8763。

在Ribbon使用熔断器

​ 改进eureka-ribbon工程,在pom文件中加入Hystrix依赖。

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

​ 工程启动类Application上添加注解@EnableHystrix注解开启Hystrix。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@EnableEurekaClient
@SpringBootApplication
@EnableHystrix
public class EurekaRibbonApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
}

@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}

​ 改造HelloService类,在hiService方法上加上@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回了一个字符串,字符串为”hi”+name+”error”;,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Service
public class HelloService {

@Autowired
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+" error";
}
}

​ 启动 eureka-ribbon 工程,当我们访问http://localhost:8764/hi?name=kn,浏览器显示:

1
hi kn,i am from port:8763

​ 此时关闭 eureka-client工程,当我们再访问http://localhost:8764/hi?name=forezp,浏览器会显示:

1
hi kn error

​ 这就说明当 eureka-client 工程不可用的时候,eureka-ribbon调用 eureka-client的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。

Feign中使用断路器

​ Feign是自带断路器的,在D版本的Spring Cloud之后,它没有默认打开。需要在配置文件中配置打开它,在配置文件加以下代码:

1
2
3
feign:
hystrix:
enabled: true

​ 这里我输入hystrix,自动的输出了:

1
2
3
hystrix:
metrics:
enabled: true

​ 这导致了我的第一次运行失败。hystrix.metrics.enabled是属于SpringCloud-Hystrix的属性而我这里使用的是Feign客户端,所以应该启用feign.hystrix.enabled的属性。

​ 对eureka-feign进行改造,只需要在FeignClient的FeignServiceClient接口的注解中加上fallback的指定类就行了:

1
2
3
4
5
@FeignClient(value = "eureka-client",fallback = FeignServerHystrix.class)
public interface FeignServiceClient {
@GetMapping( "/hi")
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

​ FeignServerHystrix需要实现FeignServiceClient接口并加入容器中:

1
2
3
4
5
6
7
@Component
public class FeignServerHystrix implements FeignServiceClient {
@Override
public String sayHiFromClientOne(String name) {
return "sorry,error,"+name;
}
}

​ 启动eureka-sevice,启动eureka-client,启动eureka-feign,在浏览器输入http://localhost:8765/hi?name=kn,会显示:

1
hi kn,i am from port:8763

​ 停掉eureka-client服务,再次访问,浏览器会出现:

1
sorry,error,kn

​ 这说明断路器起到了作用。

结尾

​ feign开启熔断之后,FeignServerHystrix会要重写接口的所有方法,而Ribbon似乎更加灵活,只需要在注解上增加fallbackmethod指定方法即可,现在看来两者各有好处。一步步的解决Bug运行成功时令人开心的,还式要继续努力啊。

​ 最后附上大牛原文链接:http://blog.csdn.net/forezp/article/details/81040990

0%