Skip to content

Commit 4157610

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

File tree

9 files changed

+111
-4
lines changed

9 files changed

+111
-4
lines changed

labx-23/labx-22-scn-hystrix-demo01/pom.xml renamed to labx-23/labx-23-scn-hystrix-demo01/pom.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

12-
<artifactId>labx-22-scn-hystrix-demo01</artifactId>
12+
<artifactId>labx-23-scn-hystrix-demo01</artifactId>
1313

1414
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
1517
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
1618
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
1719
</properties>
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,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+
}

labx-23/labx-22-user-service/pom.xml renamed to labx-23/labx-23-user-service/pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

12-
<artifactId>labx-22-user-service</artifactId>
12+
<artifactId>labx-23-user-service</artifactId>
1313

1414
<properties>
15+
<java.version>1.8</java.version>
1516
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
1617
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
1718
</properties>

labx-23/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<artifactId>labx-23</artifactId>
1313
<packaging>pom</packaging>
1414
<modules>
15-
<module>labx-22-scn-hystrix-demo01</module>
16-
<module>labx-22-user-service</module>
15+
<module>labx-23-scn-hystrix-demo01</module>
16+
<module>labx-23-user-service</module>
1717
</modules>
1818

1919

0 commit comments

Comments
 (0)