Hystrix是一个延迟和容错库,旨在隔离对远程系统,服务和第三方库的访问点,停止级联故障,并在复杂的分布式系统中实现弹性,在这些系统中,故障是不可避免的。
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
SpringBootApplication入口中需要添加@EnableCircuitBreaker
注解,此时Spring工厂会启动AOP方式对所有的方法上有@HystrixCommand
的业务方法添加熔断策略。
@SpringBootApplication
@EnableCircuitBreaker
public class HystrixSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixSpringBootApplication.class,args);
}
}
在业务方法上添加@HystrixCommand
注解实现熔断。
@HystrixCommand
@Override
public User queryUserById(Integer id) {
System.out.println(Thread.currentThread().getId());
return new User("未熔断", new Date(), true, 11.11);
}
线程隔离
默认该方法的执行会启动新的线程执行和主程序不在一个线程中,因此如果上下文中存在ThreadLocal变量,在该方法中就失效了。因此一般可以通过设置commandProperties注解属性,设置线程就可以了。
- 默认情况Service和Controller线程ID
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")
})
@Override
public User queryUserById(Integer id) {
System.out.println("Service线程ID:" + Thread.currentThread().getId());
return new User("未熔断", new Date(), true, 11.11);
}
- 设置后Service和Controller线程ID
execution.isolation.strategy
该属性的可选值有两个THREAD
和SEMAPHORE
默认值是THREAD
。①一般如果一个实例一秒钟有100个并发,此时因为频繁启动线程的开销过大此时一般考虑使用SEMAPHORE,②非网络调用。
Fallback
过在@HystrixCommand中声明fallbackMethod
的名称可以实现优雅降级,如下所示:
@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById")
@Override
public User queryUserById(Integer id) {
System.out.println(Thread.currentThread().getId());
int i=10/0;
return new User("未熔断", new Date(), true, 11.11);
}
public User fallbackMethodQueryUserById(Integer id, Throwable e) {
System.out.println(e.getMessage());
return new User("熔断降级后", new Date(), true, 11.11);
}
调用结果:
注意要求fallbackMethod方法和目标方法必须在同一个类中,具有相同的参数(异常参数可选)
Error Propagation
根据此描述,@ HystrixCommand
能够指定应忽略的异常类型。如下所述ArithmeticException: / by zero
将不会触发fallbackMethod方法。
// @HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById")
// @HystrixCommand(commandProperties = {
// @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE")
// })
@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById",ignoreExceptions = {ArithmeticException.class})
@Override
public User getUser(Integer id) {
System.out.println("Service线程ID:" + Thread.currentThread().getId());
int i = 10 / 0;
return new User("未熔断", new Date(), true, 11.11);
}
public User fallbackMethodQueryUserById(Integer id, Throwable e) {
System.out.println(e.getMessage());
return new User("熔断降级后", new Date(), true, 11.11);
}
请求超时熔断
用户可以通过设置execution.isolation.thread.timeoutInMilliseconds
属性设置一个方法最大请求延迟,系统会抛出HystrixTimeoutException
@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById",commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="100")
})
@Override
public User getUser(Integer id) {
System.out.println("Service线程ID:" + Thread.currentThread().getId());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// int i = 10 / 0;
return new User("未熔断", new Date(), true, 11.11);
}
public User fallbackMethodQueryUserById(Integer id, Throwable e) {
System.out.println(e.getMessage());
return new User("超时熔断", new Date(), true, 11.11);
}
执行结果:
https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica
https://github.com/Netflix/Hystrix/wiki/Configuration
Hystrix Dashboard
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
SpringBoot入口类添加
@EnableHystrixDashboard
注解
访问页面http://localhost:端口/hystrix