springboot2.x版本整合redis(单机/集群)(使用lettuce)
2023-09-14 08:56:58 时间
在springboot1.x
系列中,其中使用的是jedis
,但是到了springboot2.x
其中使用的是Lettuce
。 此处springboot2.x
,所以使用的是Lettuce
。
关于jedis
跟lettuce
的区别:
- Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server。
- Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接
- Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
新建一个springboot工程,添加如下pom依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- redis依赖commons-pool 这个依赖一定要添加 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
然后在application.yml
配置一下redis
服务器的地址
server:
port: 1015
spring:
redis:
cache:
nodes: -192.168.159.129:7001
-192.168.159.129:7002
-192.168.159.129:7003
-192.168.159.129:7004
-192.168.159.129:7005
-192.168.159.129:7006
host: localhost:6379
password:
maxIdle:
minIdle:
maxTotal:
maxWaitMillis: 5000
其中nodes为集群redis的参数 host为单机redis的参数
redis配置类:
package webapp.conf; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisNode; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.HashSet; import java.util.Set; @Configuration public class RedisConfiguration { @Value("${spring.redis.cache.nodes:}") private String nodes; @Value("${spring.redis.cache.host:}") private String host; @Value("${spring.redis.cache.password:}") private String password; @Value("${spring.redis.cache.maxIdle:}") private Integer maxIdle; @Value("${spring.redis.cache.minIdle:}") private Integer minIdle; @Value("${spring.redis.cache.maxTotal:}") private Integer maxTotal; @Value("${spring.redis.cache.maxWaitMillis:}") private Long maxWaitMillis; @Bean LettuceConnectionFactory lettuceConnectionFactory() { // 连接池配置 GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxIdle(maxIdle == null ? 8 : maxIdle); poolConfig.setMinIdle(minIdle == null ? 1 : minIdle); poolConfig.setMaxTotal(maxTotal == null ? 8 : maxTotal); poolConfig.setMaxWaitMillis(maxWaitMillis == null ? 5000L : maxWaitMillis); LettucePoolingClientConfiguration lettucePoolingClientConfiguration = LettucePoolingClientConfiguration.builder() .poolConfig(poolConfig) .build(); // 单机redis RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(); redisConfig.setHostName(host==null||"".equals(host)?"localhost":host.split(":")[0]); redisConfig.setPort(Integer.valueOf(host==null||"".equals(host)?"6379":host.split(":")[1])); if (password != null && !"".equals(password)) { redisConfig.setPassword(password); } // 哨兵redis // RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration(); // 集群redis /*RedisClusterConfiguration redisConfig = new RedisClusterConfiguration(); Set<RedisNode> nodeses = new HashSet<>(); String[] hostses = nodes.split("-"); for (String h : hostses) { h = h.replaceAll("\\s", "").replaceAll("\n", ""); if (!"".equals(h)) { String host = h.split(":")[0]; int port = Integer.valueOf(h.split(":")[1]); nodeses.add(new RedisNode(host, port)); } } redisConfig.setClusterNodes(nodeses); // 跨集群执行命令时要遵循的最大重定向数量 redisConfig.setMaxRedirects(3); redisConfig.setPassword(password);*/ return new LettuceConnectionFactory(redisConfig, lettucePoolingClientConfiguration); } @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(lettuceConnectionFactory); //序列化类 MyRedisSerializer myRedisSerializer = new MyRedisSerializer(); //key序列化方式 template.setKeySerializer(myRedisSerializer); //value序列化 template.setValueSerializer(myRedisSerializer); //value hashmap序列化 template.setHashValueSerializer(myRedisSerializer); return template; } static class MyRedisSerializer implements RedisSerializer<Object> { @Override public byte[] serialize(Object o) throws SerializationException { return serializeObj(o); } @Override public Object deserialize(byte[] bytes) throws SerializationException { return deserializeObj(bytes); } /** * 序列化 * @param object * @return */ private static byte[] serializeObj(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { throw new RuntimeException("序列化失败!", e); } } /** * 反序列化 * @param bytes * @return */ private static Object deserializeObj(byte[] bytes) { if (bytes == null){ return null; } ByteArrayInputStream bais = null; try { bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { throw new RuntimeException("反序列化失败!", e); } } } }
以上已经完成整合教程,测试案例:
注入:
@Autowired private RedisTemplate<String, Object> redisTemplate;
添加:
redisTemplate.opsForValue().set(key, value);
添加,设置过期时间:
redisTemplate.opsForValue().set(key, obj, expireTime, TimeUnit.SECONDS);
获取:
Object o = redisTemplate.opsForValue().get(key);
删除:
redisTemplate.delete(key);
相关文章
- 探索Redis: 探寻数据的踪迹(查询redis中的数据)
- Redis集群实现安全同步(redis集群同步)
- 如何更改Redis服务器的密码(怎么更换redis的密码)
- 瞬间提升效率利用视频掌握Redis(视频redis)
- 获取Redis哨兵最新版一步搞定(获取redis哨兵版本)
- 肯德基系统宕机Redis未找到(肯德基redis未找到)
- 配置Redis,实现功能性强大(编写redis配置类)
- 榜单直播,Redis助力活动火爆程度(榜单直播redis)
- 不让Redis在开机立即启动(禁用redis开机启动)
- 某个值查看Redis中是否存在某个值(查询redis中是否含有)
- 探索Redis数据库容量大小(查看redis数据库容量)
- 探索Redis命令历史一次简单回顾(查看redis 命令历史)
- 点赞与评论的新助力 Redis给予的支持(点赞 评论 redis)
- 较优秀地使用深度解析Redis如何更好地使用它(深入理解redis 怎样)
- 浏览器轻松连接Redis服务(浏览器连接redis)
- 安全可靠使用有密码的Redis集群(有密码的redis集群)
- 如何有效保存数据到Redis中(数据保存到redis中)
- TP50与Redis的联手,为完善Web应用打造极致体验(tp5.0redis)
- 单机搭建Redis集群简单实现分布式环境(单机下redis集群)
- Redis中间层简单提升系统性能的方法(如何做redis中间层)
- 重新启动Redis闪退进行修复(启动redis闪退)
- Redis雪崩机器重启的抗拒者(redis 雪崩机器重启)
- 浅析Redis集群配置指南(redis集群配置详解)
- 提升效率Redis集群访问路径调整(redis集群访问地址)
- Redis集群优势无限(redis集群的有点)
- Redis集群更高效的投票机制(redis集群投票机制)
- 化Redis集群最优性能管理方案(redis集群性能最优)
- 快速实现Redis集群导入数据库数据(redis集群导入数据库数据)
- Redis集群高效存储的机制实现(redis集群存储原理)
- Redis集群优化更安全高效的数据存储(redis集群后数据存储)
- 使用Redis集群构建基于JWT的可扩展授权模型(redis集群jwt)
- 改进的Redis选举机制轻松把握集群控制权(redis选举方法)
- 由Redis寓意的英文名字再次回到起点(redis英文名字寓意)
- 重构系统运维以Redis为核心的新架构(redis 运维框架)
- Redis精准计算过期时间(redis 过期时间计算)