六、Hystrix请求缓存(request cache)

Hystrix支持将一个请求结果缓存起来,在同一个请求上下文中,具有相同key的请求将直接从缓存中取出结果,很适合查询类的接口,可以使用缓存进行优化,减少请求开销,从而跳过真实服务的访问请求。

Hystrix请求结果缓存的作用:
1、在同一个请求上下文中,可以减少使用相同参数请求原始服务的开销。
3、请求缓存在 run() 和 construct() 执行之前生效,所以可以有效减少不必要的线程开销。

要使用Hystrix cache功能:
1、需要构建 RequestContext ,可以在拦截器中使用 HystrixRequestContext.initializeContext() 和 HystrixRequestContext.shutdown() 来初始化 RequestContext 和 关闭RequestContext资源。
2、需要重写 HystrixCommand 或 HystrixObservableCommand 中的 getCacheKey() 方法,指定缓存的 key,开启缓存配置。


一、配置HystrixRequestContextServletFilter
通过Servlet的Filter配置hystrix的上下文。
@WebFilter(filterName = "hystrixRequestContextServletFilter",urlPatterns = "/*",asyncSupported = true)
public class HystrixRequestContextServletFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
chain.doFilter(request, response);
} finally {
context.shutdown();
}
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void destroy() {

}
}

二、开启缓存功能
继承HystrixCommand或HystrixObservableCommand,覆盖getCacheKey()方法,指定缓存的key,开启缓存配置。
public class GetProductInfoCommand extends HystrixCommand<ProductInfo>{

private static final HystrixCommandKey COMMAND_KEY= HystrixCommandKey.Factory.asKey("GetProductInfoCommand");

private Long productId;

public GetProductInfoCommand(Long productId) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ProductInfoService"))
.andCommandKey(COMMAND_KEY)
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("GetProductInfoPool"))
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withCoreSize(15)
.withQueueSizeRejectionThreshold(10))
);
this.productId=productId;
}

@Override
protected ProductInfo run() throws Exception {
String url = "http://127.0.0.1:8082/getProductInfo?productId="+productId;
String response = HttpClientUtils.sendGetRequest(url);
return JSONObject.parseObject(response,ProductInfo.class);
}

@Override
protected String getCacheKey() {
return "product_info_"+productId;
}

public static void flushCache(Long productId){
HystrixRequestCache.getInstance(COMMAND_KEY, HystrixConcurrencyStrategyDefault.getInstance()).clear("product_info_"+productId);
}
}

三、清除失效缓存
继承HystrixCommand或HystrixObservableCommand,在更新接口调用完成后,清空缓存。
public class UpdateProductInfoCommand extends HystrixCommand<Boolean>{

private Long productId;
public UpdateProductInfoCommand(Long productId) {
super(HystrixCommandGroupKey.Factory.asKey("UpdateProductInfoGroup"));
this.productId=productId;
}

@Override
protected Boolean run() throws Exception {
//执行更新操作
GetProductInfoCommand.flushCache(productId);
return true;
}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值