Netflix Hystrix是SOA/微服务架构中提供服务隔离、熔断、降级机制的工具/框架。Netflix Hystrix是断路器的一种实现,用于高微服务架构的可用性,是防止服务出现雪崩的利器。java
在分布式架构中,一个应用依赖多个服务是很是常见的,若是其中一个依赖因为延迟太高发生阻塞,调用该依赖服务的线程就会阻塞,若是相关业务的QPS较高,就可能产生大量阻塞,从而致使该应用/服务因为服务器资源被耗尽而拖垮。web
另外,故障也会在应用之间传递,若是故障服务的上游依赖较多,可能会引发服务的雪崩效应。就跟数据瘫痪,会引发依赖该数据库的应用瘫痪是同样的道理。spring
当一个应用依赖多个外部服务,一切都正常的状况下,以下图:数据库
若是其中一个依赖发生延迟,当前请求就会被阻塞json
出现这种状况后,若是没有应对措施,后续的请求也会被持续阻塞缓存
每一个请求都占用了系统的CPU、内存、网络等资源,若是该应用的QPS较高,那么该应用因此的服务资源会被快速消耗完毕,直至应用死掉。若是这个出问题的依赖(Dependency I),不止这一个应用,亦或是受影响的应用上层也有更多的依赖,那就会带来咱们前面所提到的服务雪崩效应。 因此,为了应对以上问题,就须要有支持服务隔离、熔断等操做的工具。服务器
在经过网络依赖服务出现高延迟或者失败时,为系统提供保护和控制 能够进行快速失败,缩短延迟等待时间和快速恢复:当异常的依赖回复正常后,失败的请求所占用的线程会被快速清理,不须要额外等待 提供失败回退(Fallback)和相对优雅的服务降级机制 提供有效的服务容错监控、报警和运维控制手段网络
Hystrix将请求的逻辑进行封装,相关逻辑会在独立的线程中执行架构
Hystrix有自动超时策略,若是外部请求超过阈值,Hystrix会以超时来处理并发
Hystrix会为每一个依赖维护一个线程池,当线程满载,不会进行线程排队,会直接终止操做
Hystrix有熔断机制: 在依赖服务失效比例超过阈值时,手动或者自动地切断服务一段时间 因此,当引入了Hystrix以后,当出现某个依赖高延迟的时候
可能会有人有疑问,为何不依赖于HTTP Client去作容错保护(快速失败、熔断等),而是在访问依赖以外经过线程&线程池隔离的方式作这个断路器(Hystrix)`
主要是如下几个方面:
修改pom.xml,引入Spring Cloud Netflix Hystrix
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
修改启动类Application.java,增长@EnableHystrix注解开启Hystrix
@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
修改TestService.java,增长断路器功能
@Service
public class TestService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "indexError")
public Object index() {
return restTemplate.getForObject("http://testservice", String.class);
}
public Object plus(int numA, int numB) {
String url = String.format("http://testservice/plus?numA=%s&numB=%s", numA, numB);
return restTemplate.getForObject(url, String.class);
}
public Object indexError() {
return "{\"code\": 999,\"message\": \"服务断路\"}";
}
}
启动 Application 项目, 访问:http://localhost:8604/ti ,将看到
{
"code": 0,
"message": "hello",
"content": null,
"serviceName": "testservice",
"host": "localhost:8602"
}
关闭testservice,而后再访问 http://localhost:8604/ti ,将看到
{
"code": 999,
"message": "服务断路"
}
至此完成Ribbon+Hystrix的熔断。
feign:
hystrix:
enabled: true
新建 TestServiceHystrix.java做为TestService的断路处理实现
@Component
public class TestServiceHystrix implements TestService {
@Override
public String indexService() {
return "{\"code\": 999,\"message\": \"服务断路\"}";
}
@Override
public Result plusService(int numA, int numB) {
Result result = new Result();
result.setCode(999);
result.setMessage("服务断路");
return new Result();
}
@Override
public Result plusabService(Plus plus) {
Result result = new Result();
result.setCode(999);
result.setMessage("服务断路");
return new Result();
}
@Override
public Result plus2Service(Plus plus) {
Result result = new Result();
result.setCode(999);
result.setMessage("服务断路");
return new Result();
}
}
@FeignClient(value = "testservice", fallback = TestServiceHystrix.class)
public interface TestService {
@RequestMapping(value = "/", method = RequestMethod.GET)
String indexService();
@RequestMapping(value = "/plus", method = RequestMethod.GET)
Result plusService(@RequestParam(name = "numA") int numA, @RequestParam(name = "numB") int numB);
@RequestMapping(value = "/plus", method = RequestMethod.POST, consumes = "application/json")
Result plusabService(Plus plus);
@RequestMapping(value = "/plus2", method = RequestMethod.POST)
Result plus2Service(@RequestBody Plus plus);
}
{
"code": 0,
"message": "hello",
"content": null,
"serviceName": "testservice",
"host": "localhost:8602"
}
{
"code": 999,
"message": "服务断路"
}
至此完成Feign+Hystrix的熔断。
欢迎关注公众号! 公众号回复:
入群
,扫码加入咱们交流群!