Skip to content

Commit e829f55

Browse files
author
YunaiV
committed
增加 Hystrix 入门示例
1 parent 4157610 commit e829f55

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo.controller;
2+
3+
import cn.iocoder.springcloud.labx23.hystrixdemo.service.CollapserDemoService;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import java.util.concurrent.ExecutionException;
12+
import java.util.concurrent.Future;
13+
14+
@RestController
15+
@RequestMapping("/collapser-demo")
16+
public class CollapserDemoController {
17+
18+
private Logger logger = LoggerFactory.getLogger(CollapserDemoController.class);
19+
20+
@Autowired
21+
private CollapserDemoService collapserDemoService;
22+
23+
@GetMapping("/test")
24+
public void test() throws ExecutionException, InterruptedException {
25+
logger.info("[test][准备获取用户信息]");
26+
Future<String> user01 = collapserDemoService.getUserFuture(1);
27+
Future<String> user02 = collapserDemoService.getUserFuture(2);
28+
logger.info("[test][提交获取用户信息]");
29+
30+
logger.info("[test][user({}) 的结果为({})]", 1, user01.get());
31+
logger.info("[test][user({}) 的结果为({})]", 2, user02.get());
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo.service;
2+
3+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
4+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
5+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
6+
import org.apache.commons.lang.StringUtils;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
import org.springframework.web.client.RestTemplate;
12+
13+
import java.util.Arrays;
14+
import java.util.Collections;
15+
import java.util.List;
16+
import java.util.concurrent.Future;
17+
18+
@Service
19+
public class CollapserDemoService {
20+
21+
private Logger logger = LoggerFactory.getLogger(CollapserDemoService.class);
22+
23+
@Autowired
24+
private RestTemplate restTemplate;
25+
26+
@HystrixCollapser(
27+
batchMethod = "getUsers",
28+
collapserProperties = {
29+
@HystrixProperty(name = "timerDelayInMilliseconds", value = "10000") // 演示,所以设置的时间较长
30+
}
31+
)
32+
public Future<String> getUserFuture(Integer id) {
33+
throw new RuntimeException("This method body should not be executed");
34+
}
35+
36+
@HystrixCommand
37+
public List<String> getUsers(List<Integer> ids) {
38+
logger.info("[getUsers][准备调用 user-service 获取多个用户({})详情]", ids);
39+
String[] users = restTemplate.getForEntity("http://127.0.0.1:18080/user/batch_get?ids=" + StringUtils.join(ids, ',')
40+
, String[].class).getBody();
41+
return users == null || users.length == 0 ? Collections.emptyList() : Arrays.asList(users);
42+
}
43+
44+
}

labx-23/labx-23-user-service/pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
<artifactId>labx-23-user-service</artifactId>
1313

1414
<properties>
15-
<java.version>1.8</java.version>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
1617
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
1718
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
1819
</properties>

labx-23/labx-23-user-service/src/main/java/cn/iocoder/springcloud/labx23/userservice/UserServiceApplication.java

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import org.springframework.web.bind.annotation.RequestParam;
88
import org.springframework.web.bind.annotation.RestController;
99

10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
1013
@SpringBootApplication
1114
public class UserServiceApplication {
1215

@@ -19,6 +22,11 @@ public String get(@RequestParam("id") Integer id) {
1922
return "User:" + id;
2023
}
2124

25+
@GetMapping("/batch_get")
26+
public List<String> batchGet(@RequestParam("ids") List<Integer> ids) {
27+
return ids.stream().map(id -> "User:" + id).collect(Collectors.toList());
28+
}
29+
2230
}
2331

2432
public static void main(String[] args) {

0 commit comments

Comments
 (0)