我是靠谱客的博主 陶醉煎蛋,这篇文章主要介绍@CacheResult、@CacheRemove、@HystrixCollapser理解和应用,现在分享给大家,希望可以做个参考。

@CacheResult
该注解用来标记请求命令返回的结果应该被缓存,它必须与@HystrixCommand注解结合使用,eg:

@Override
@CacheResult
@HystrixCommand(commandKey=“getUserByNameCommandKey”)
@HystrixCollapser(batchMethod=“getUsersByNames”,collapserProperties= {
@HystrixProperty(name=“timerDelayInMilliseconds”,value=“1000”)
})
public String getUserByName(String name) {
System.out.println(“call EmallServiceImpl.getUserByName()====”);
String json = sysFeignClient.getUserByName(name);
return json;
}

@CacheRemove
该注解用来让请求命令的缓存失效,失效的缓存根据定义Key决定,eg:

@Override
@CacheRemove(commandKey=“getUserByNameCommandKey”)
@HystrixCommand
public void removeUserByNameCache(String name) {
System.out.println(“call EmallServiceImpl.removeUserByNameCache()====”);
}

通过HystrixCollapser合并请求提高应用吞吐量

我们知道elasticsearch可以通过指定index和doc id来获取某个doc的,也支持mget的方式,发送一次请求,将多个doc id发送过去查询出相应的docs。

这样做可以有效的减少发往ES的请求数,降低ES的负载。

在web应用层通过HystrixCollapser合并单个get请求为mget请求的处理方式,就能大大提升系统的TPS,eg:

@Override
@CacheResult
@HystrixCommand(commandKey=“getUserByIdCommandKey”)
@HystrixCollapser(batchMethod=“getUsersByIds”,collapserProperties= {
@HystrixProperty(name=“timerDelayInMilliseconds”,value=“1000”)
})
public String getUserById(String id) {
System.out.println(“call EmallServiceImpl.getUserById()====”);
String json = sysFeignClient.getUserById(id);
return json;
}

最后

以上就是陶醉煎蛋最近收集整理的关于@CacheResult、@CacheRemove、@HystrixCollapser理解和应用的全部内容,更多相关@CacheResult、@CacheRemove、@HystrixCollapser理解和应用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部