Spring Boot高级教程之使用Redis实现session共享
Redis是一个缓存消息中间件及具有丰富特性的键值存储系统。SpringBoot为Jedis客户端库和由SpringDataRedis提供的基于Jedis客户端的抽象提供自动配置。spring-boot-starter-redis'StarterPOM'为收集依赖提供一种便利的方式。
引入spring-boot-starter-redis,在pom.xml配置文件中增加配置如下(基于之前章节“SpringBoot构建框架”中的pom.xml文件):
org.springframework.boot spring-boot-starter-redis
可以注入一个自动配置的RedisConnectionFactory,StringRedisTemplate或普通的跟其他SpringBean相同的RedisTemplate实例。默认情况下,这个实例将尝试使用localhost:6379连接Redis服务器。
@Component publicclassMyBean{ privateStringRedisTemplatetemplate; @Autowired publicMyBean(StringRedisTemplatetemplate){ this.template=template; } //... }
如果添加一个自己的任何自动配置类型的@Bean,它将替换默认的(除了RedisTemplate的情况,它是根据bean的名称'redisTemplate'而不是它的类型进行排除的)。如果在classpath路径下存在commons-pool2,默认会获得一个连接池工厂。
应用使用Redis案例
添加配置文件,配置内容如下:
#REDIS(RedisProperties) #Redis服务器地址 spring.redis.host=192.168.0.58 #Redis服务器连接端口 spring.redis.port=6379 #连接超时时间(毫秒) spring.redis.timeout=0
redis配置类,具体代码如下:
importorg.springframework.boot.context.properties.ConfigurationProperties; importorg.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix="spring.redis") publicclassRedisConn{ privateStringhost; privateintport; privateinttimeout; publicStringgetHost(){ returnhost; } publicvoidsetHost(Stringhost){ this.host=host; } publicintgetPort(){ returnport; } publicvoidsetPort(intport){ this.port=port; } publicintgetTimeout(){ returntimeout; } publicvoidsetTimeout(inttimeout){ this.timeout=timeout; } @Override publicStringtoString(){ return"Redis[localhost="+host+",port="+port+",timeout="+timeout+"]"; } }
注意:在RedisConn类中注解@ConfigurationProperties(prefix="spring.Redis")的作用是读取springboot的默认配置文件信息中以spring.redis开头的信息。
配置cache类
importjava.lang.reflect.Method; importjava.util.HashMap; importjava.util.Map; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.cache.CacheManager; importorg.springframework.cache.annotation.CachingConfigurerSupport; importorg.springframework.cache.annotation.EnableCaching; importorg.springframework.cache.interceptor.KeyGenerator; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.ComponentScan; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.PropertySource; importorg.springframework.data.redis.cache.RedisCacheManager; importorg.springframework.data.redis.connection.RedisConnectionFactory; importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory; importorg.springframework.data.redis.core.RedisTemplate; importorg.springframework.data.redis.core.StringRedisTemplate; importorg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; importorg.springframework.stereotype.Component; importcom.cachemodle.RedisConn; importcom.fasterxml.jackson.annotation.JsonAutoDetect; importcom.fasterxml.jackson.annotation.PropertyAccessor; importcom.fasterxml.jackson.databind.ObjectMapper; /** * *@authorsandsarediscacheservice * */ @Configuration @EnableCaching publicclassRedisConfigextendsCachingConfigurerSupport{ @Autowired privateRedisConnredisConn; /** *生产key的策略 * *@return */ @Bean @Override publicKeyGeneratorkeyGenerator(){ returnnewKeyGenerator(){ @Override publicObjectgenerate(Objecttarget,Methodmethod,Object...params){ StringBuildersb=newStringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for(Objectobj:params){ sb.append(obj.toString()); } returnsb.toString(); } }; } /** *管理缓存 * *@paramredisTemplate *@return */ @SuppressWarnings("rawtypes") @Bean publicCacheManagerCacheManager(RedisTemplateredisTemplate){ RedisCacheManagerrcm=newRedisCacheManager(redisTemplate); //设置cache过期时间,时间单位是秒 rcm.setDefaultExpiration(60); Mapmap=newHashMap (); map.put("test",60L); rcm.setExpires(map); returnrcm; } /** *redis数据库连接池 *@return */ @Bean publicJedisConnectionFactoryredisConnectionFactory(){ JedisConnectionFactoryfactory=newJedisConnectionFactory(); factory.setHostName(redisConn.getHost()); factory.setPort(redisConn.getPort()); factory.setTimeout(redisConn.getTimeout());//设置连接超时时间 returnfactory; } /** *redisTemplate配置 * *@paramfactory *@return */ @SuppressWarnings({"rawtypes","unchecked"}) @Bean publicRedisTemplate redisTemplate(RedisConnectionFactoryfactory){ StringRedisTemplatetemplate=newStringRedisTemplate(factory); Jackson2JsonRedisSerializerjackson2JsonRedisSerializer=newJackson2JsonRedisSerializer(Object.class); ObjectMapperom=newObjectMapper(); om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); returntemplate; } }
分析:缓存类继承的是CachingConfigurerSupport,它把读取的配置文件信息的RedisConn类对象注入到这个类中。在这个类中keyGenerator()方法是key的生成策略,CacheManager()方法是缓存管理策略,redisConnectionFactory()是redis连接,redisTemplate()方法是redisTemplate配置信息,配置后使redis中能存储Java对象。
测试配置是否成功,实例:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(Application.class) publicclassTestRedis{ @Autowired privateStringRedisTemplatestringRedisTemplate;//处理字符串 @Autowired privateRedisTemplateredisTemplate;//处理对象 @Test publicvoidtest()throwsException{ stringRedisTemplate.opsForValue().set("yoodb","123"); Assert.assertEquals("123",stringRedisTemplate.opsForValue().get("yoodb")); } }
简单封装的Redis工具类,代码如下:
importjava.io.Serializable; importjava.util.Set; importjava.util.concurrent.TimeUnit; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.data.redis.core.RedisTemplate; importorg.springframework.data.redis.core.ValueOperations; importorg.springframework.stereotype.Component; @Component publicclassRedisUtils{ @SuppressWarnings("rawtypes") @Autowired privateRedisTemplateredisTemplate; /** *批量删除对应的value * *@paramkeys */ publicvoidremove(finalString...keys){ for(Stringkey:keys){ remove(key); } } /** *批量删除key * *@parampattern */ @SuppressWarnings("unchecked") publicvoidremovePattern(finalStringpattern){ Setkeys=redisTemplate.keys(pattern); if(keys.size()>0) redisTemplate.delete(keys); } /** *删除对应的value * *@paramkey */ @SuppressWarnings("unchecked") publicvoidremove(finalStringkey){ if(exists(key)){ redisTemplate.delete(key); } } /** *判断缓存中是否有对应的value * *@paramkey *@return */ @SuppressWarnings("unchecked") publicbooleanexists(finalStringkey){ returnredisTemplate.hasKey(key); } /** *读取缓存 * *@paramkey *@return */ @SuppressWarnings("unchecked") publicObjectget(finalStringkey){ Objectresult=null; ValueOperations operations=redisTemplate.opsForValue(); result=operations.get(key); returnresult; } /** *写入缓存 * *@paramkey *@paramvalue *@return */ @SuppressWarnings("unchecked") publicbooleanset(finalStringkey,Objectvalue){ booleanresult=false; try{ ValueOperations operations=redisTemplate.opsForValue(); operations.set(key,value); result=true; }catch(Exceptione){ e.printStackTrace(); } returnresult; } /** *写入缓存 * *@paramkey *@paramvalue *@return */ @SuppressWarnings("unchecked") publicbooleanset(finalStringkey,Objectvalue,LongexpireTime){ booleanresult=false; try{ ValueOperations operations=redisTemplate.opsForValue(); operations.set(key,value); redisTemplate.expire(key,expireTime,TimeUnit.SECONDS); result=true; }catch(Exceptione){ e.printStackTrace(); } returnresult; } }
查询数据库时自动使用缓存,根据方法生成缓存,参考代码如下:
@Service publicclassUserService{ @Cacheable(value="redis-key") publicUserInfogetUserInfo(Longid,Stringsex,intage,Stringname){ System.out.println("无缓存时调用----数据库查询"); returnnewUserInfo(id,sex,age,name); } }
注意:value的值就是缓存到redis中的key,此key是需要自己在进行增加缓存信息时定义的key,用于标识唯一性的。
Session共享
分布式系统中session共享有很多不错的解决方案,其中托管到缓存中是比较常见的方案之一,下面利用Session-spring-session-data-redis实现session共享。
引入依赖,在pom.xml配置文件中增加如下内容:
org.springframework.session spring-session-data-redis
Session配置,具体代码如下:
@Configuration @EnableRedisHttpSession(maxInactiveIntervalInSeconds=86400*30) publicclassSessionConfig{ }
maxInactiveIntervalInSeconds:设置Session失效时间,使用RedisSession之后,原SpringBoot的server.session.timeout属性不再生效。
测试实例,具体代码如下:
@RequestMapping("uid") Stringuid(HttpSessionsession){ UUIDuid=(UUID)session.getAttribute("uid"); if(uid==null){ uid=UUID.randomUUID(); } session.setAttribute("uid",uid); returnsession.getId(); }
登录redis服务端,输入命令keys'session*',查看缓存是否成功。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。