3Hystrix 信号量隔离 · SpringCloud微服务实战 · 看云
3.Hystrix 信号量
导航
Hystrix提供了两种隔离机制,信号量(SEMAPHORE)和线程(THREAD)隔离,我们先看信号量的隔离
当请求的并发数高于设定的阀值时,就不会再执行命令。相对于线程池机制,信号量的开销较小,但是信号量机制不支持超时和异步,除非对调用的服务有足够的信任,否则不建议使用信号量机制进行隔离。
注意:如果依赖关系被信号量隔离,然后变为潜在状态,则父线程将保持阻塞状态,直到基础网络调用超时为止。
一旦达到限制,信号灯拒绝将开始,但是填充信号量的线程无法得到释放
本节代码地址
接下来我们在代码中看看信号量机制怎么进行隔离的
3.1 新建FwHystrixCommondSemaphore类
需要在构造方法中指定SEMAPHORE机制,这里我们用默认的最大并发数(10)和回退并发数(10),开20线程进行测试。
@Slf4j
public class FwHystrixCommondSemaphore extends HystrixCommand<String> {
private final String name;
protected FwHystrixCommondSemaphore(String name) {
super(HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("myGroup"))
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter().withExecutionIsolationStrategy(
HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE
)
));
this.name = name;
}
@Override
protected String getFallback() {
log.info(this.name+":"+Thread.currentThread().getName()+"异常");
return this.name+":"+Thread.currentThread().getName();
}
@Override
protected String run() throws Exception {
log.info(this.name+":"+Thread.currentThread().getName()+"成功");
return this.name + ":" + Thread.currentThread().getName();
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
for (int i = 0; i <20 ; i++) {
final int index=i;
Thread t =new Thread() {
@Override
public void run() {
FwHystrixCommondSemaphore test = new FwHystrixCommondSemaphore("test" + index);
test.execute();
}
};
t.start();
}
Thread.sleep(5000);
}
}
3.2 运行main方法
可以看到console输出的日志
13:33:06.043 [Thread-15] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test15:Thread-15异常
13:33:06.043 [Thread-7] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test7:Thread-7异常
13:33:06.043 [Thread-11] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test11:Thread-11异常
13:33:06.043 [Thread-9] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test9:Thread-9异常
13:33:06.043 [Thread-18] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test18:Thread-18异常
13:33:06.043 [Thread-19] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test19:Thread-19异常
13:33:06.043 [Thread-12] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test12:Thread-12异常
13:33:06.043 [Thread-2] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test2:Thread-2异常
13:33:06.043 [Thread-17] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test17:Thread-17异常
13:33:06.043 [Thread-0] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test0:Thread-0异常
13:33:06.066 [Thread-6] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test6:Thread-6成功
13:33:06.066 [Thread-8] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test8:Thread-8成功
13:33:06.066 [Thread-16] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test16:Thread-16成功
13:33:06.066 [Thread-5] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test5:Thread-5成功
13:33:06.066 [Thread-3] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test3:Thread-3成功
13:33:06.066 [Thread-13] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test13:Thread-13成功
13:33:06.066 [Thread-10] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test10:Thread-10成功
13:33:06.066 [Thread-4] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test4:Thread-4成功
13:33:06.066 [Thread-14] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test14:Thread-14成功
13:33:06.066 [Thread-1] INFO com.yisu.hystrix.without.FwHystrixCommondSemaphore - test1:Thread-1成功