一. 哨兵模式配置
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(); // 獲取用戶信息