增加 Hystrix 入门示例

This commit is contained in:
YunaiV
2020-05-12 08:16:02 +08:00
parent caea8cd7bd
commit 4157610638
9 changed files with 111 additions and 4 deletions

View File

@@ -9,9 +9,11 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>labx-22-scn-hystrix-demo01</artifactId>
<artifactId>labx-23-scn-hystrix-demo01</artifactId>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
</properties>

View File

@@ -0,0 +1,33 @@
package cn.iocoder.springcloud.labx23.hystrixdemo.controller;
import cn.iocoder.springcloud.labx23.hystrixdemo.service.CacheDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/cache-demo")
public class CacheDemoController {
@Autowired
private CacheDemoService cacheDemoService;
@GetMapping("/get_user")
public String getUser(@RequestParam("id") Integer id) {
String userA = cacheDemoService.getUser(id);
String userB = cacheDemoService.getUser(id);
String userC = cacheDemoService.getUser(id);
return userC;
}
@GetMapping("/update_user")
public String updateUser(@RequestParam("id") Integer id) {
String userA = cacheDemoService.getUser(id);
cacheDemoService.updateUser(id);
String userC = cacheDemoService.getUser(id);
return userC;
}
}

View File

@@ -0,0 +1,28 @@
package cn.iocoder.springcloud.labx23.hystrixdemo.filter;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@Component
@WebFilter(urlPatterns = "/")
public class HystrixRequestContextFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// 初始化 HystrixRequestContext
HystrixRequestContext context = HystrixRequestContext.initializeContext();
// 继续过滤器
try {
chain.doFilter(request, response);
} finally {
// 销毁 HystrixRequestContext
context.close();
}
}
}

View File

@@ -0,0 +1,43 @@
package cn.iocoder.springcloud.labx23.hystrixdemo.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class CacheDemoService {
private Logger logger = LoggerFactory.getLogger(CacheDemoService.class);
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "getUserFallback")
@CacheResult(cacheKeyMethod = "genGetUserCacheKey")
public String getUser(Integer id) {
logger.info("[getUser][准备调用 user-service 获取用户({})详情]", id);
return restTemplate.getForEntity("http://127.0.0.1:18080/user/get?id=" + id, String.class).getBody();
}
@HystrixCommand
@CacheRemove(commandKey = "getUser", cacheKeyMethod = "genGetUserCacheKey")
public void updateUser(Integer id) {
logger.info("[updateUser][更新用户({})详情]", id);
}
public String getUserFallback(Integer id, Throwable throwable) {
logger.info("[getUserFallback][id({}) exception({})]", id, ExceptionUtils.getRootCauseMessage(throwable));
return "mock:User:" + id;
}
public String genGetUserCacheKey(Integer id) {
return "USER_" + id;
}
}

View File

@@ -9,9 +9,10 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>labx-22-user-service</artifactId>
<artifactId>labx-23-user-service</artifactId>
<properties>
<java.version>1.8</java.version>
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
</properties>

View File

@@ -12,8 +12,8 @@
<artifactId>labx-23</artifactId>
<packaging>pom</packaging>
<modules>
<module>labx-22-scn-hystrix-demo01</module>
<module>labx-22-user-service</module>
<module>labx-23-scn-hystrix-demo01</module>
<module>labx-23-user-service</module>
</modules>