Skip to content

Commit 9af1942

Browse files
author
YunaiV
committed
增加 Hystrix 入门示例(Hystrix Dashboard)
1 parent 55b737d commit 9af1942

File tree

13 files changed

+409
-1
lines changed

13 files changed

+409
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labx-23</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-23-scn-hystrix-actuator</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
19+
</properties>
20+
21+
<!--
22+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
23+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
24+
-->
25+
<dependencyManagement>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-parent</artifactId>
30+
<version>${spring.boot.version}</version>
31+
<type>pom</type>
32+
<scope>import</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.cloud</groupId>
36+
<artifactId>spring-cloud-dependencies</artifactId>
37+
<version>${spring.cloud.version}</version>
38+
<type>pom</type>
39+
<scope>import</scope>
40+
</dependency>
41+
</dependencies>
42+
</dependencyManagement>
43+
44+
<dependencies>
45+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-web</artifactId>
49+
</dependency>
50+
51+
<!-- 引入 Spring Cloud Netflix Hystrix 相关依赖,将 Hystrix 作为服务保障组件,并实现对其的自动配置 -->
52+
<dependency>
53+
<groupId>org.springframework.cloud</groupId>
54+
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
55+
</dependency>
56+
57+
<!-- 实现对 Actuator 的自动化配置 -->
58+
<dependency>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-starter-actuator</artifactId>
61+
</dependency>
62+
</dependencies>
63+
64+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.web.client.RestTemplate;
8+
9+
@SpringBootApplication
10+
@EnableCircuitBreaker // 声明开启断路器
11+
public class DemoApplication {
12+
13+
@Bean
14+
public RestTemplate restTemplate() {
15+
return new RestTemplate();
16+
}
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(DemoApplication.class, args);
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo.controller;
2+
3+
import cn.iocoder.springcloud.labx23.hystrixdemo.service.CacheDemoService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("/cache-demo")
12+
public class CacheDemoController {
13+
14+
@Autowired
15+
private CacheDemoService cacheDemoService;
16+
17+
@GetMapping("/get_user")
18+
public String getUser(@RequestParam("id") Integer id) {
19+
String userA = cacheDemoService.getUser(id);
20+
String userB = cacheDemoService.getUser(id);
21+
String userC = cacheDemoService.getUser(id);
22+
return userC;
23+
}
24+
25+
@GetMapping("/update_user")
26+
public String updateUser(@RequestParam("id") Integer id) {
27+
String userA = cacheDemoService.getUser(id);
28+
cacheDemoService.updateUser(id);
29+
String userC = cacheDemoService.getUser(id);
30+
return userC;
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
35+
@GetMapping("/test_02")
36+
public void test02() throws ExecutionException, InterruptedException {
37+
logger.info("[test][准备获取用户信息]");
38+
Future<String> user01 = collapserDemoService.getUserFuture(2);
39+
Future<String> user02 = collapserDemoService.getUserFuture(1);
40+
logger.info("[test][提交获取用户信息]");
41+
42+
logger.info("[test][user({}) 的结果为({})]", 1, user01.get());
43+
logger.info("[test][user({}) 的结果为({})]", 2, user02.get());
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo.controller;
2+
3+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
4+
import org.apache.commons.lang.exception.ExceptionUtils;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
@RestController
15+
@RequestMapping("/demo")
16+
public class DemoController {
17+
18+
private Logger logger = LoggerFactory.getLogger(getClass());
19+
20+
@Autowired
21+
private RestTemplate restTemplate;
22+
23+
@GetMapping("/get_user")
24+
@HystrixCommand(fallbackMethod = "getUserFallback")
25+
public String getUser(@RequestParam("id") Integer id) {
26+
logger.info("[getUser][准备调用 user-service 获取用户({})详情]", id);
27+
return restTemplate.getForEntity("http://127.0.0.1:18080/user/get?id=" + id, String.class).getBody();
28+
}
29+
30+
public String getUserFallback(Integer id, Throwable throwable) {
31+
logger.info("[getUserFallback][id({}) exception({})]", id, ExceptionUtils.getRootCauseMessage(throwable));
32+
return "mock:User:" + id;
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo.filter;
2+
3+
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
4+
import org.springframework.stereotype.Component;
5+
6+
import javax.servlet.*;
7+
import javax.servlet.annotation.WebFilter;
8+
import java.io.IOException;
9+
10+
@Component
11+
@WebFilter(urlPatterns = "/")
12+
public class HystrixRequestContextFilter implements Filter {
13+
14+
@Override
15+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
16+
throws IOException, ServletException {
17+
// 初始化 HystrixRequestContext
18+
HystrixRequestContext context = HystrixRequestContext.initializeContext();
19+
// 继续过滤器
20+
try {
21+
chain.doFilter(request, response);
22+
} finally {
23+
// 销毁 HystrixRequestContext
24+
context.close();
25+
}
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo.service;
2+
3+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
4+
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
5+
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
6+
import org.apache.commons.lang.exception.ExceptionUtils;
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+
@Service
14+
public class CacheDemoService {
15+
16+
private Logger logger = LoggerFactory.getLogger(CacheDemoService.class);
17+
18+
@Autowired
19+
private RestTemplate restTemplate;
20+
21+
@HystrixCommand(fallbackMethod = "getUserFallback")
22+
@CacheResult(cacheKeyMethod = "genGetUserCacheKey")
23+
public String getUser(Integer id) {
24+
logger.info("[getUser][准备调用 user-service 获取用户({})详情]", id);
25+
return restTemplate.getForEntity("http://127.0.0.1:18080/user/get?id=" + id, String.class).getBody();
26+
}
27+
28+
@HystrixCommand
29+
@CacheRemove(commandKey = "getUser", cacheKeyMethod = "genGetUserCacheKey")
30+
public void updateUser(Integer id) {
31+
logger.info("[updateUser][更新用户({})详情]", id);
32+
}
33+
34+
public String getUserFallback(Integer id, Throwable throwable) {
35+
logger.info("[getUserFallback][id({}) exception({})]", id, ExceptionUtils.getRootCauseMessage(throwable));
36+
return "mock:User:" + id;
37+
}
38+
39+
public String genGetUserCacheKey(Integer id) {
40+
return "USER_" + id;
41+
}
42+
43+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
management:
2+
endpoints:
3+
web:
4+
exposure:
5+
include: 'hystrix.stream' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labx-23</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-23-scn-hystrix-dashboard</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
19+
</properties>
20+
21+
<!--
22+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
23+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
24+
-->
25+
<dependencyManagement>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-parent</artifactId>
30+
<version>${spring.boot.version}</version>
31+
<type>pom</type>
32+
<scope>import</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.cloud</groupId>
36+
<artifactId>spring-cloud-dependencies</artifactId>
37+
<version>${spring.cloud.version}</version>
38+
<type>pom</type>
39+
<scope>import</scope>
40+
</dependency>
41+
</dependencies>
42+
</dependencyManagement>
43+
44+
<dependencies>
45+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-web</artifactId>
49+
</dependency>
50+
51+
<!-- 引入 Spring Cloud Netflix Hystrix 相关依赖,将 Hystrix 作为服务保障组件,并实现对其的自动配置 -->
52+
<dependency>
53+
<groupId>org.springframework.cloud</groupId>
54+
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
55+
</dependency>
56+
57+
<!-- 实现对 Actuator 的自动化配置 -->
58+
<dependency>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-starter-actuator</artifactId>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.springframework.cloud</groupId>
65+
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
66+
</dependency>
67+
</dependencies>
68+
69+
</project>

0 commit comments

Comments
 (0)