Hystrix-06-信号量隔离

信号量隔离:SEMAPHORE

隔离本地代码或可快速返回远程调用(如memcached,redis)可以直接使用信号量隔离,降低线程隔离开销.

使用demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.fuyi.hystrix.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;

public class SemaphoreCommand extends HystrixCommand<String> {

private final String name;

public SemaphoreCommand(String name) {
super(Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("userServiceGroup"))
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(
HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE
)
)
);
this.name = name;
}

@Override
protected String run() throws Exception {
System.out.println("thread : " + Thread.currentThread().getName());
return "hello";
}

public static void main(String[] args) {
SemaphoreCommand c = new SemaphoreCommand("hh");
String result = c.execute();
System.out.println(result);
System.out.println("thread : " + Thread.currentThread().getName());
}
}

执行结果

1
2
3
thread : main
hello
thread : main