1.介绍
熔断器 Hystrix 是容错管理工具,作用是通过隔离、控制服务从而对延迟和故障提供更强大的容错能力,避免整个系统被拖垮。
复杂分布式架构通常都具有很多依赖,当一个引用高度耦合其他服务时非常危险且容易导致失败,这种伤害很容易伤害服务调用者,最后导致一个接一个的连续错误,应用本
身就处在被拖垮的风险中,最后失去控制,就像在一个高流量的网站中,某个单一的后端一旦发生延迟,将会在很短时间内导致所有应用资源被耗尽。如何处理这些问题是有关
系统性能和效率的关键性问题。
当在系统高峰时期,大量对微服务的调用可能会堵塞远程服务器的线程池,如果这个线程池没有和主服务器的线程池隔离,就可能会拖垮整个服务器。Hystrix 使用自己的
线程池,这样和主应用服务器线程池隔离,如果调用会花费很长时间,会终止调用,不同的命令和命令组能够被使用它们各自的线程池,隔离不同服务。
2.如何使用
通过 Netflix Hystrix 实现断路器容错的功能,使用Fallback方法为熔断或异常提供备用方案进行处理。
涉及到的工程:eureka-sever、product-service、order-service-hystrix。(前两个工程前面章节已讲解,最后一个自行创建)
验证使用 order-service-hystrix 调用 product-service,正常情况下返回产品服务的结果,当product-service服务无法访问时,立刻调用回滚方法返回结果。
order-service-hystrix目录结构如图:
(1)pom.xml增加 Hystrix依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
(2)启动类增加注解 @EnableCircuitBreaker,开启断路器功能。
package com.hole;
import com.hole.web.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;
@EnableDiscoveryClient
@SpringBootApplication
@RestController
@EnableCircuitBreaker
public class OrderServiceHystrixApplication {
@Autowired
private ProductService productService;
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrderServiceHystrixApplication.class, args);
}
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public ResponseEntity<String> hello(){
return new ResponseEntity<String>("hello order service hystrix", HttpStatus.OK);
}
@RequestMapping(value = "/product/hello",method = RequestMethod.GET)
public String helloProductService(){
return productService.helloService();
}
}
(3)增加 ProductService 类,通过它来调用 product-service服务,当调用不通过立即调用回滚方法。
package com.hole.web;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* Created by helei on 2017/7/16.
*/
@Service
public class ProductService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "productServiceFallback")
public String helloService(){
return restTemplate.getForEntity("http://PRODUCT-SERVICE/hello",String.class).getBody();
}
public String productServiceFallback(){
return "product服务不见了,稍后再试";
}
}
(4)启动。
三个工程启动后,查看监控界面。
(5)验证。
访问order-service-hystrix,这时通过order-service-hystrix可以调用 product-service,并且返回正确结果。如图:
然后停掉 product-service 服务,再次访问。这次访问会得到回滚方法的结果。