基本概念
- Hystrix是Netflix针对微服务分布式系统采用的熔断保护中间件。
- Hystrix是通过HystrixCommand对调用进行隔离。
1 Hystrix的简单使用
2 回退支持
重新HystrixCommand的getFallback方法。
3 信号量策略配置
public class MyHystrixCommand extends HystrixCommand<String> {
private final String name;
public MyHystrixCommand(String name){
super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("MyGroup"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(
HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)));
this.name = name;
}
protected String run() throws Exception {
return this.name+":"+Thread.currentThread().getName();
}
}
4 线程隔离策略配置
- 通过使用andThreadPoolPropertiesDefaults配置线程池的一些参数
- 代码
public class MySecondHystrixCommand extends HystrixCommand<String> { private final String name; public MySecondHystrixCommand(String name){ super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("MyGroup")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionIsolationStrategy( HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withCoreSize(10) .withMaxQueueSize(100) .withMaximumSize(100))); this.name = name; } protected String run() throws Exception { return this.name+":"+Thread.currentThread().getName(); } }
6 结果缓存
- Hystrix提供了方法级别的缓存。通过重新getCacheKey来判断是否返回缓存的数据,getCacheKey返回的是缓冲的key,而值就是run方法对应的返回的值。
- 代码
public class MyCacheHystrixCommand extends HystrixCommand<String> { private final String name; public MyCacheHystrixCommand(String name){ super(HystrixCommandGroupKey.Factory.asKey("CacheGroup")); this.name = name; } @Override protected String getCacheKey() { return name; } protected String run() throws Exception { System.err.println("get data"); return this.name+":"+Thread.currentThread().getName(); } }
- 输出,只有一次调用run的方法,输出get data
6 缓存清除
- 通过Hystrix的方法根据name进行清除
- 代码
public class MyCacheHystrixCommand extends HystrixCommand<String> { private final String name; private static final HystrixCommandKey GETTER_KEY = HystrixCommandKey.Factory.asKey("MyKey"); public MyCacheHystrixCommand(String name){ super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("cache")) .andCommandKey(GETTER_KEY)); this.name = name; } @Override protected String getCacheKey() { return name; } protected String run() throws Exception { System.err.println("get data"); return this.name+":"+Thread.currentThread().getName(); } public static void flushCache(String name){ HystrixRequestCache.getInstance(GETTER_KEY, HystrixConcurrencyStrategyDefault.getInstance()).clear(name); } }
- 结果
- 结果
7 合并请求
- Hystrix支持将多个请求自动合并为一个请求,可以节省网络开销。
Feign整合Hystrix
- 配置Feign对Hystrix的支持
- feign.hystrix.enable = true
1. Fallback方法
- 通过在@FeignClient客户端类上指定 fallback进行回退。
@FeignClient(value="eureka-client-user-service",fallback=UserClientFallback.class) public interface UserClient{ @GetMapping("/user/hello") String hello(); } //回退类 @Component public class UserClientFallback implements UserClient{ return "fail"; }
2 FallbackFactory方式
- 通过fallback可以实现服务不可用时回退的功能,如果想知道触发回退的原因,使用FallbackFactory实现。