redission和spring整合配置
一. 哨兵模式配置
spring:
redis:
sentinel:
master: lsnrrdscmdbp01 # 这个由redis团队提供, 主节点名称
nodes:
- ip:26379
- ip:26379
- ip:26379
password: 密码
二. 集群版(非哨兵模式)
spring:
redis:
cluster:
nodes:
- IP1:Port
- IP2:Port
- IP3:Port
connectionTimeout: 6000
soTimeout: 6000
maxAttempts: 5
password: 密码
三. 单机版配置
spring:
redis:
database: 0
host: ip
port: 6379
password: 密码
timeout: 1000
四. pom.xml文件
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
<exclusion>
<artifactId>netty-common</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>netty-codec</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>netty-handler</artifactId>
<groupId>io.netty</groupId>
</exclusion>
</exclusions>
<version>3.15.3</version>
</dependency>
五. 常用示例
@Autowired
private RedissonClient redissonClient;
1. map方式存储, 例如: 用户名作为key, token作为值
RMap<Object, Object> map = redissonClient.getMap("logged_in_user_account_map");
map.put(userInfo.getUserAccount(), token);
2. bucket方式存储用户信息
RBucket<Object> bucket = redissonClient.getBucket("login_user_entity" + token); // 获取一个bucket桶(如果存在则直接获取, 不存在则会新建一个)
bucket.set(userInfo, 30, TimeUnit.MINUTES); // 存储过期30分种钟
bucket.set(userInfo); // 存储
bucket.getAndDelete(); // 删除
bucket.expire(30, TimeUnit.MINUTES); // 用户登陆状态,进行续期
UserLoginInfo userInfo = (UserLoginInfo) bucket.get(); // 获取用户信息
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix