在分布式架构中,一个应用依赖多个服务是很是常见的,若是其中一个依赖因为延迟太高发生阻塞,调用该依赖服务的线程就会阻塞,若是相关业务的QPS较高,就可能产生大量阻塞,从而致使该应用/服务因为服务器资源被耗尽而拖垮。java
另外,故障也会在应用之间传递,若是故障服务的上游依赖较多,可能会引发服务的雪崩效应。就跟数据瘫痪,会引发依赖该数据库的应用瘫痪是同样的道理。git
因此,断路器就是用来支持服务隔离、熔断等操做的工具。断路器会以隔离的方式来处理服务请求,当断路数量达到阈值,就会触发熔断(直接返回失败)。github
Hystrix是SOA/微服务架构中提供服务隔离、熔断、降级机制的工具/框架。经过以上手段来下降服务故障带来的关联影响,以提升系统的总体可用性。spring
Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable. -https://github.com/Netflix/Hystrix数据库
Hystrix就是断路器的典型表明。
Hystrix工做原理:https://ken.io/note/netflix-hystrix-intro-principlejson
框架 | 版本 |
---|---|
Spring Boot | 2.0.0.RELEASE |
Spring Cloud | Finchley.BUILD-SNAPSHOT |
JDK | 1.8.x |
参考上一篇 https://ken.io/note/spring-cloud-feign-quickstart
源码:https://github.com/ken-io/springcloud-course/tree/master/chapter-03/服务器
启动Eureka Server: http://localhost:8800
启动Test Service:http://localhost:8602架构
服务消费者Ribbon: https://ken.io/note/spring-cloud-ribbon-quickstart
服务消费者Feign:
https://ken.io/note/spring-cloud-feign-quickstartapp
本篇基于 https://ken.io/note/spring-cloud-ribbon-quickstart
中ribbonclient项目代码进行修改,引入Spring Cloud Netflix Hystrix框架
源码:https://github.com/ken-io/springcloud-course/tree/master/chapter-02/ribbonclient
修改pom.xml,引入Spring Cloud Netflix Hystrix
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
修改启动类App.java,增长@EnableHystrix注解开启Hystrix
@EnableHystrix @EnableDiscoveryClient @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
修改TestService.java,增长断路器功能
@HystrixCommand
并经过fallbackMethod参数指定断路后执行的方法@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\": \"服务断路\"}"; } }
启动 ribbonclient 项目,
访问:http://localhost:8604/ti ,将看到
{ "code": 0, "message": "hello", "content": null, "serviceName": "testservice", "host": "localhost:8602" }
关闭testservice,而后再访问 http://localhost:8604/ti ,将看到
{ "code": 999, "message": "服务断路" }
这就说明,当testservice不可用的时候,咱们访问增长了Hystrix容错措施的接口,会触发快速失败,当即执行fallback。而不是一直等待直到超时,很大程度上下降了形成阻塞可能。
本篇基于 https://ken.io/note/spring-cloud-feign-quickstart
中feignclient项目代码进行修改
源码:https://github.com/ken-io/springcloud-course/tree/master/chapter-03/feignclient
Feign已经引入了Hystrix,因此不须要再单独引入
修改application.yml,开启Hystrix
feign: hystrix: enabled: true
TestServiceHystrix会做为TestService的断路处理实现
package io.ken.springcloud.feignclient.service; import io.ken.springcloud.feignclient.model.Plus; import io.ken.springcloud.feignclient.model.Result; import org.springframework.stereotype.Component; @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); }
启动 feignclient 项目
访问:http://localhost:8605/ti ,将看到
{ "code": 0, "message": "hello", "content": null, "serviceName": "testservice", "host": "localhost:8602" }
关闭testservice,而后再访问 http://localhost:8605/ti ,将看到
{ "code": 999, "message": "服务断路" }
这就说明,当testservice不可用的时候,咱们访问增长了Hystrix容错措施的接口,会触发快速失败,当即执行fallback。而不是一直等待直到超时,很大程度上下降了形成阻塞可能。