1.Hystrix介绍
断路器:Hystrix客户端
Netflix的创造了一个调用的库Hystrix实现了断路器图案。在微服务架构中,通常有多层服务调用。
图1.微服务图
较低级别的服务中的服务故障可能导致用户级联故障。当对特定服务的呼叫达到一定阈值时(Hystrix中的默认值为5秒内的20次故障),电路打开,不进行通话。在错误和开路的情况下,开发人员可以提供后备。
图2. Hystrix回退防止级联故障
开放式电路会停止级联故障,并允许不必要的或失败的服务时间来愈合。回退可以是另一个Hystrix保护的调用,静态数据或一个正常的空值。回退可能被链接,所以第一个回退使得一些其他业务电话又回到静态数据。
2.项目中的使用Hystrix
要在项目中包含Hystrix,请使用组org.springframework.cloud
和artifact id spring-cloud-starter-hystrix
的启动器。有关使用当前的Spring Cloud发布列表设置构建系统的详细信息,请参阅Spring Cloud项目页面。
示例启动应用程序:
pom.xml中加
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.alen</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>hystrix-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--spring-cloud-starter-eureka已经引了,所以可引可不引-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- 断路器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
使用EnableCircuitBreaker
或者 EnableHystrix
表明Spring boot
工程启用hystrix,两个注解是等价的.
@SpringBootApplication
//通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
@EnableEurekaClient
//注解开启Hystrix
@EnableHystrix
public class HystrixApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class, args);
}
/**
* 向程序的ioc注入一个bean: restTemplate;
* 并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@Value("${eurekaclientURL}")
private String eurekaclientURL;
//该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法
//有个坑 这个方法fallbackMethod = "failError"的方法参数还得和getAge原方法的一样才行
@HystrixCommand(fallbackMethod = "failError")
public String getAge(Integer age) {
return restTemplate.getForObject(eurekaclientURL + age, String.class);
}
public String failError(Integer age) {
return "hi,"+age+",sorry,error!";
}
}
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
/**
* 服务调用 消费者
* @param age
* @return
*/
@RequestMapping("/getage")
public String getConsumer(@RequestParam Integer age) {
return helloService.getAge(age);
}
}
失败时结果:
hi,10,sorry,error!
3.Feign中使用断路器
Feign是自带断路器的
基于service-feign工程进行改造,只需要在FeignClient的HelloService接口的注解中加上fallback的指定类就行了:
@FeignClient(value ="eureka-client",fallback = HelloServiceImpl.class)
public interface HelloService {
@RequestMapping("/hello")
//必须显示的指定age,不显示还不行
String hello(@RequestParam("age") Integer age);
}
HelloServiceImpl 需要实现HelloService 接口,并注入到Ioc容器中,代码如下:
public class HelloServiceImpl implements HelloService {
@Override
public String hello(Integer age) {
return age+"失败";
}
}
调用失败时:10失败
这证明断路器起到作用了。
4.Hystrix Dashboard (断路器:Hystrix 仪表盘)
Hystrix仪表板
Hystrix的主要优点之一是它收集关于每个HystrixCommand的一套指标。Hystrix仪表板以有效的方式显示每个断路器的运行状况。
基于hystrix-service 改造,Feign的改造和这一样。
首选在pom.xml引入spring-cloud-starter-hystrix-dashboard的起步依赖:
<!--Hystrix仪表板
Hystrix的主要优点之一是它收集关于每个HystrixCommand的一套指标。
Hystrix仪表板以有效的方式显示每个断路器的运行状况-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
在主程序启动类中加入@EnableHystrixDashboard注解,开启hystrixDashboard:
@SpringBootApplication
//通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
@EnableEurekaClient
//开启Feign的功能
@EnableFeignClients
public class FeignServiceApp {
public static void main(String[] args) {
SpringApplication.run(FeignServiceApp.class, args);
}
}
打开浏览器:访问http://localhost:8085/hystrix,界面如下:
健康指标
连接断路器的状态也暴露在呼叫应用程序的/health
端点中。
{
"hystrix": {
"openCircuitBreakers": [
"StoreIntegration::getStoresByLocationLink"
],
"status": "CIRCUIT_OPEN"
},
"status": "UP"
}
Hystrix指标流
要使Hystrix指标流包含对spring-boot-starter-actuator
的依赖。这将使/hystrix.stream
作为管理端点。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
5.Hystrix超时和Ribbon客户
当使用包含Ribbon客户端的Hystrix命令时,您需要确保您的Hystrix超时配置为长于配置的Ribbon超时,包括可能进行的任何潜在的重试。例如,如果您的Ribbon连接超时为一秒钟,并且Ribbon客户端可能会重试该请求三次,那么您的Hystrix超时应该略超过三秒钟。
参考:
http://blog.csdn.net/forezp/article/details/69934399
https://springcloud.cc/spring-cloud-dalston.html#_circuit_breaker_hystrix_clients