详解Spring集成Redis的两种方式
目录
- 一、使用Jedis方式集成
- 1、增加依赖
- 2、配置项
- 3、配置连接池
- 4、测试
- 使用spring-data-redis
- 1、引入依赖
- 2、配置项
- 3、使用
- 4、可能会遇到的坑
- 哨兵和集群
- 总结:
一、使用Jedis方式集成
1、增加依赖
org.springframework.boot spring-boot-starter-weborg.projectlombok lomboktrue redis.clients jediscom.alibaba fastjson1.2.73
2、配置项
redis.host=localhostredis.maxTotal=5redis.maxIdle=5redis.testOnBorrow=true#以下方式也可以,SpringBoot同样能将其解析注入到JedisPoolConfig中#redis.max-total=3#redis.max-idle=3#redis.test-on-borrow=true
3、配置连接池
/** * @author 公-众-号:程序员阿牛 * 由于Jedis实例本身不非线程安全的,因此我们用JedisPool */@Configurationpublic class CommonConfig {@Bean@ConfigurationProperties("redis")public JedisPoolConfig jedisPoolConfig() {return new JedisPoolConfig(); }@Bean(destroyMethod = "close")public JedisPool jedisPool(@Value("${redis.host}") String host) {return new JedisPool(jedisPoolConfig(), host); }}
4、测试
/** * @author 公-众-号:程序员阿牛 */@RestControllerpublic class JedisController {@Autowiredprivate JedisPool jedisPool; @RequestMapping("getUser")public String getUserFromRedis(){UserInfo userInfo = new UserInfo(); userInfo.setUserId("A0001"); userInfo.setUserName("张三丰"); userInfo.setAddress("武当山"); jedisPool.getResource().set("userInfo", JSON.toJSONString(userInfo)); UserInfo userInfo1 = JSON.parseObject(jedisPool.getResource().get("userInfo"),UserInfo.class); return userInfo1.toString(); }}
运行结果如下:
文章图片
【详解Spring集成Redis的两种方式】我们可以自己包装一个RedisClient,来简化我们的操作
使用spring-data-redis
1、引入依赖
org.springframework.boot spring-boot-starter-data-redis
2、配置项
在application.properties中增加配置
spring.redis.host=localhostspring.redis.port=6379
3、使用
/** * @author 公-众-号:程序员阿牛 */@RestControllerpublic class RedisController {@Autowiredprivate RedisTemplate redisTemplate; @RequestMapping("getUser2")public String getUserFromRedis(){UserInfo userInfo = new UserInfo(); userInfo.setUserId("A0001"); userInfo.setUserName("张三丰"); userInfo.setAddress("武当山"); redisTemplate.opsForValue().set("userInfo", userInfo); UserInfo userInfo1 = (UserInfo) redisTemplate.opsForValue().get("userInfo"); return userInfo1.toString(); }}
是的,你只需要引入依赖、加入配置就可以使用Redis了,不要高兴的太早,这里面会有一些坑
4、可能会遇到的坑
文章图片
使用工具查看我们刚才set的内容,发现key前面多了一串字符,value也是不可见的
原因
使用springdataredis,默认情况下是使用org.springframework.data.redis.serializer.JdkSerializationRedisSerializer这个类来做序列化
具体我们看一下RedisTemplate 代码如何实现的
/***在初始化的时候,默认的序列化类是JdkSerializationRedisSerializer*/public void afterPropertiesSet() {super.afterPropertiesSet(); boolean defaultUsed = false; if (this.defaultSerializer == null) {this.defaultSerializer = new JdkSerializationRedisSerializer(this.classLoader != null ? this.classLoader : this.getClass().getClassLoader()); }...省略无关代码}
如何解决
很简单,自己定义RedisTemplate并指定序列化类即可
/** * @author 公-众-号:程序员阿牛 */@Configurationpublic class RedisConfig {@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); template.setValueSerializer(jackson2JsonRedisSerializer()); //使用StringRedisSerializer来序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jackson2JsonRedisSerializer()); template.afterPropertiesSet(); return template; }@Beanpublic RedisSerializer
查看运行结果:
文章图片
哨兵和集群 只需要改一下配置项即可
# 哨兵spring.redis.sentinel.master=mymasterspring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381#集群spring.redis.cluster.max-redirects=100spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
总结: 以上两种方式都可以,但是还是建议你使用Spring-data-redis,因为Spring经过多年的发展,尤其是Springboot的日渐成熟,已经为我们简化了很多操作。
到此这篇关于详解Spring集成Redis的两种方式的文章就介绍到这了,更多相关Spring集成Redis 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- Java|Java OpenCV图像处理之SIFT角点检测详解
- C语言浮点函数中的modf和fmod详解
- 解决SpringBoot引用别的模块无法注入的问题
- 私有化轻量级持续集成部署方案--03-部署web服务(下)
- 2018-07-09|2018-07-09 Spring 的DBCP,c3p0
- 虚拟DOM-Diff算法详解
- LSTM网络层详解及其应用实例
- spring|spring boot项目启动websocket