Spring Cloud Hystrix熔断器
在微服务中,一个请求进来可能需要调用多个服务,如果其中一个服务出现故障,比如超时堵塞。导致其他请求进来也堵塞从而导致线程耗尽等情况。一个服务不可能用导致整个服务不可用,Hystrix为解决这个问题。 断路器(Cricuit Breaker):
- 服务不可用自动断开(快速失败):如果对某服务调用错误次数达到一个值,后续服务都会快速失败,有点像保险丝。
- 服务可用时自动恢复:熔断并没有完全断,还会允许少量请求,以判断服务状态是否允许恢复。
- 资源隔离:对依赖服务调用会在单独的线程池中进行。
- 监控:Dashboard 提供成功率、平时响应时间、调用频次、断路器状态等监控。
一、快速起步
maven pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
spring boot 启动函数
@SpringBootApplication
@EnableCircuitBreaker
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
@Component
public class StoreIntegration {
@HystrixCommand(fallbackMethod = "defaultStores")
public Object getStores(Map<String, Object> parameters) {
//do stuff that might fail
}
public Object defaultStores(Map<String, Object> parameters) {
return /* something useful */;
}
}
可以使用commandProperties参数和@HystrixProperty注解的集合来配置@HystrixCommand和线程池。
@HystrixCommand(
commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")},
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "30"),
@HystrixProperty(name = "maxQueueSize", value = "101"),
@HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"),
@HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
})
public String getUserById(String id) {
return null;
}
二、配置
# 超时设置
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 30000
三、仪表盘
Hystrix的主要作用是会采集每一个HystrixCommand的信息指标,把每一个断路器的信息指标显示的Hystrix仪表盘上。但是仪表盘只能一次查看一个服务的hystrix.stream,如果服务多节点多,监控起来很麻烦。我们可以引入turbine,让turbine去注册中心获取所有hystrix.stream,然后汇总。
代码
maven
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<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>
</dependencies>
spring boot main
@SpringBootApplication
@EnableTurbine
@EnableDiscoveryClient
@EnableHystrixDashboard
public class TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineApplication.class, args);
}
}
配置
spring.application.name=turbine-dashbord
server.port=9010
turbine.appConfig= client-server
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
eureka.client.serviceUrl.defaultZone=http://discovery:9001/eureka/
面板: ![](/images/Hystrix Dashboard.png) [图1:HystrixDashboard ] ![](/images/Hystrix Stream turbine.png) [图2:HystrixDashboard 汇总 turbine ]