【发布时间】:2018-09-16 20:19:40
【问题描述】:
com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult- 我对 Hystrix-Javanica 给出的 @CacheResult 注释感到有些困惑。根据文档 - 它可以缓存 HystrixCommand 的结果。在下面的示例中,服务始终执行 @HystrixCommand 注释方法,不明白为什么 @CacheResult 没有发挥作用。
Map<String, String> map = new HashMap<>();
@CacheResult(cacheKeyMethod="getCacheKey")
@HystrixCommand(fallbackMethod = "callStudentServiceAndGetData_Fallback", commandProperties= {
@HystrixProperty(name="requestCache.enabled" , value="true")
} )
public String callStudentServiceAndGetData(@CacheKey String schoolname) {
System.out.println("Getting School details for " + schoolname);
String response = restTemplate.exchange("http://localhost:8098/getStudentDetailsForSchool/{schoolname}",HttpMethod.GET, null, new ParameterizedTypeReference<String>() {}, schoolname).getBody();
map.put(schoolname, response);
return response;
}
public String getCacheKey (String key) {
return map.get(key);
}
private String callStudentServiceAndGetData_Fallback(String schoolname) {
return "Service down. "+ new Date();
}
我没有找到任何有用的带有 Javanica 注释的 Hystrix 示例。你能看看我在这里缺少什么吗? TIA
【问题讨论】:
标签: spring-boot spring-cloud-netflix hystrix netflix