详解spring cloud hystrix缓存功能的使用
hystrix缓存的作用是
-1.减少重复的请求数,降低依赖服务的返回数据始终保持一致。
-2.==在同一个用户请求的上下文中,相同依赖服务的返回数据始终保持一致==。
-3.请求缓存在run()和construct()执行之前生效,所以可以有效减少不必要的线程开销。
1通过HystrixCommand类实现
1.1开启缓存功能
继承HystrixCommand或HystrixObservableCommand,覆盖getCacheKey()方法,指定缓存的key,开启缓存配置。
importcom.netflix.hystrix.HystrixCommand; importcom.netflix.hystrix.HystrixCommandGroupKey; importcom.netflix.hystrix.HystrixCommandKey; importcom.netflix.hystrix.HystrixRequestCache; importcom.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault; importcom.szss.demo.orders.vo.UserVO; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.web.client.RestTemplate; publicclassUserCacheCommandextendsHystrixCommand{ privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(UserCacheCommand.class); privatestaticfinalHystrixCommandKeyGETTER_KEY=HystrixCommandKey.Factory.asKey("CommandKey"); privateRestTemplaterestTemplate; privateStringusername; publicUserCacheCommand(RestTemplaterestTemplate,Stringusername){ super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("userCacheCommand")).andCommandKey(GETTER_KEY)); this.restTemplate=restTemplate; this.username=username; } @Override protectedUserVOrun()throwsException{ LOGGER.info("thread:"+Thread.currentThread().getName()); returnrestTemplate.getForObject("http://users-service/user/name/{username}",UserVO.class,username); } @Override protectedUserVOgetFallback(){ UserVOuser=newUserVO(); user.setId(-1L); user.setUsername("调用失败"); returnuser; } @Override protectedStringgetCacheKey(){ returnusername; } publicstaticvoidflushCache(Stringusername){ HystrixRequestCache.getInstance(GETTER_KEY,HystrixConcurrencyStrategyDefault.getInstance()).clear(username); } }
1.2配置HystrixRequestContextServletFilter
通过servlet的Filter配置hystrix的上下文。
importcom.netflix.hystrix.strategy.concurrency.HystrixRequestContext; importjavax.servlet.*; importjavax.servlet.annotation.WebFilter; importjava.io.IOException; @WebFilter(filterName="hystrixRequestContextServletFilter",urlPatterns="/*",asyncSupported=true) publicclassHystrixRequestContextServletFilterimplementsFilter{ publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{ HystrixRequestContextcontext=HystrixRequestContext.initializeContext(); try{ chain.doFilter(request,response); }finally{ context.shutdown(); } } @Override publicvoidinit(FilterConfigfilterConfig)throwsServletException{ } @Override publicvoiddestroy(){ } }
在不同context中的缓存是不共享的,还有这个request内部一个ThreadLocal,所以request只能限于当前线程。
1.3清除失效缓存
继承HystrixCommand或HystrixObservableCommand,在更新接口调用完成后,清空缓存。
importcom.netflix.hystrix.HystrixCommand; importcom.netflix.hystrix.HystrixCommandGroupKey; importcom.netflix.hystrix.HystrixCommandKey; importcom.szss.demo.orders.vo.UserVO; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.http.HttpEntity; importorg.springframework.web.client.RestTemplate; publicclassUserUpdateCacheCommandextendsHystrixCommand{ privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(UserUpdateCacheCommand.class); privatestaticfinalHystrixCommandKeyGETTER_KEY=HystrixCommandKey.Factory.asKey("CommandKey"); privateRestTemplaterestTemplate; privateUserVOuser; publicUserUpdateCacheCommand(RestTemplaterestTemplate,UserVOuser){ super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("userUpdateCacheCommand"))); this.restTemplate=restTemplate; this.user=user; } @Override protectedUserVOrun()throwsException{ LOGGER.info("thread:"+Thread.currentThread().getName()); HttpEntity u=newHttpEntity (user); UserVOuserVO=restTemplate.postForObject("http://users-service/user",u,UserVO.class); UserCacheCommand.flushCache(user.getUsername()); returnuserVO; } //@Override //protectedUserVOgetFallback(){ //UserVOuser=newUserVO(); //user.setId(-1L); //user.setUsername("调用失败"); //returnuser; //} @Override protectedStringgetCacheKey(){ returnuser.getUsername(); } }
2使用@CacheResult、@CacheRemove和@CacheKey标注来实现缓存
2.1使用@CacheResult实现缓存功能
@CacheResult(cacheKeyMethod="getCacheKey") @HystrixCommand(commandKey="findUserById",groupKey="UserService",threadPoolKey="userServiceThreadPool") publicUserVOfindById(Longid){ ResponseEntityuser=restTemplate.getForEntity("http://users-service/user?id={id}",UserVO.class,id); returnuser.getBody(); } publicStringgetCacheKey(Longid){ returnString.valueOf(id); }
@CacheResult注解中的cacheKeyMethod用来标示缓存key(cacheKey)的生成函数。函数的名称可任意取名,入参和标注@CacheResult的方法是一致的,返回类型是String。
2.2使用@CacheResult和@CacheKey实现缓存功能
@CacheResult @HystrixCommand(commandKey="findUserById",groupKey="UserService",threadPoolKey="userServiceThreadPool") publicUserVOfindById2(@CacheKey("id")Longid){ ResponseEntityuser=restTemplate.getForEntity("http://users-service/user?id={id}",UserVO.class,id); returnuser.getBody(); }
标注@HystrixCommand注解的方法,使用@CacheKey标注需要指定的参数作为缓存key。
2.3使用@CacheRemove清空缓存
@CacheRemove(commandKey="findUserById") @HystrixCommand(commandKey="updateUser",groupKey="UserService",threadPoolKey="userServiceThreadPool") publicvoidupdateUser(@CacheKey("id")UserVOuser){ restTemplate.postForObject("http://users-service/user",user,UserVO.class); }
@CacheRemove必须指定commandKey,否则程序无法找到缓存位置。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。