Skip to content

Commit 2b84f09

Browse files
committed
✨ Introducing new features. Hystrix
1 parent 7d3cad9 commit 2b84f09

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@
9595
<version>1.1.40</version>
9696
</dependency>
9797

98+
<dependency>
99+
<groupId>com.netflix.hystrix</groupId>
100+
<artifactId>hystrix-core</artifactId>
101+
<version>1.5.2</version>
102+
</dependency>
103+
98104
</dependencies>
99105

100106
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.crossoverjie.hystrix;
2+
3+
import com.netflix.hystrix.*;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* Function:订单服务
11+
*
12+
* @author crossoverJie
13+
* Date: 2018/7/28 16:43
14+
* @since JDK 1.8
15+
*/
16+
public class CommandOrder extends HystrixCommand<String> {
17+
18+
private final static Logger LOGGER = LoggerFactory.getLogger(CommandOrder.class);
19+
20+
private String orderName;
21+
22+
public CommandOrder(String orderName) {
23+
24+
25+
super(Setter.withGroupKey(
26+
//服务分组
27+
HystrixCommandGroupKey.Factory.asKey("OrderGroup"))
28+
//线程分组
29+
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("OrderPool"))
30+
31+
//线程池配置
32+
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
33+
.withCoreSize(10)
34+
.withKeepAliveTimeMinutes(5)
35+
.withMaxQueueSize(10)
36+
.withQueueSizeRejectionThreshold(10000))
37+
38+
.andCommandPropertiesDefaults(
39+
HystrixCommandProperties.Setter()
40+
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
41+
)
42+
;
43+
this.orderName = orderName;
44+
}
45+
46+
47+
@Override
48+
public String run() throws Exception {
49+
50+
LOGGER.info("orderName=[{}]", orderName);
51+
52+
TimeUnit.MILLISECONDS.sleep(100);
53+
return "OrderName=" + orderName;
54+
}
55+
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.crossoverjie.hystrix;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.concurrent.Future;
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* Function: 线程隔离测试
11+
*
12+
* @author crossoverJie
13+
* Date: 2018/7/28 16:58
14+
* @since JDK 1.8
15+
*/
16+
public class CommandTest {
17+
18+
private final static Logger LOGGER = LoggerFactory.getLogger(CommandTest.class);
19+
20+
21+
public static void main(String[] args) throws Exception {
22+
CommandOrder commandPhone = new CommandOrder("手机");
23+
CommandOrder command = new CommandOrder("电视");
24+
25+
26+
//阻塞方式执行
27+
String execute = commandPhone.execute();
28+
LOGGER.info("execute=[{}]", execute);
29+
30+
//异步非阻塞方式
31+
Future<String> queue = command.queue();
32+
String value = queue.get(200, TimeUnit.MILLISECONDS);
33+
LOGGER.info("value=[{}]", value);
34+
35+
36+
CommandUser commandUser = new CommandUser("张三");
37+
String name = commandUser.execute();
38+
LOGGER.info("name=[{}]", name);
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.crossoverjie.hystrix;
2+
3+
import com.netflix.hystrix.*;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* Function:用户服务
11+
*
12+
* @author crossoverJie
13+
* Date: 2018/7/28 16:43
14+
* @since JDK 1.8
15+
*/
16+
public class CommandUser extends HystrixCommand<String> {
17+
18+
private final static Logger LOGGER = LoggerFactory.getLogger(CommandUser.class);
19+
20+
private String userName;
21+
22+
public CommandUser(String userName) {
23+
24+
25+
super(Setter.withGroupKey(
26+
//服务分组
27+
HystrixCommandGroupKey.Factory.asKey("UserGroup"))
28+
//线程分组
29+
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("UserPool"))
30+
31+
//线程池配置
32+
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
33+
.withCoreSize(10)
34+
.withKeepAliveTimeMinutes(5)
35+
.withMaxQueueSize(10)
36+
.withQueueSizeRejectionThreshold(10000))
37+
38+
//线程池隔离
39+
.andCommandPropertiesDefaults(
40+
HystrixCommandProperties.Setter()
41+
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
42+
)
43+
;
44+
this.userName = userName;
45+
}
46+
47+
48+
@Override
49+
public String run() throws Exception {
50+
51+
LOGGER.info("userName=[{}]", userName);
52+
53+
TimeUnit.MILLISECONDS.sleep(100);
54+
return "userName=" + userName;
55+
}
56+
57+
58+
}

0 commit comments

Comments
 (0)