一、maven依赖
<!-- redisson -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.19.3</version>
</dependency>
二、ClusterRedissonConfig.java配置redis哨兵模式集群
package com.sms.gateway.config;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;
/**
-
@author
-
@description 集群Redis配置
*/
@Configuration
public class ClusterRedissonConfig {@Value("${spring.redis.sentinel.nodes}")
private Listnodes; @Value("${spring.redis.sentinel.master}")
private String master;@Value("${spring.redis.password}")
private String password;@Bean
public RedissonClient getRedisson() {
Config config = new Config();
config.useSentinelServers()
.setMasterName(master)
//可以用"rediss://"来启用SSL连接
.addSentinelAddress(nodes.stream().map(node -> "redis://" + node).toArray(String[]::new))
.setPassword(password);
return Redisson.create(config);
}
}
三、使用redisson
注入redissonClient对象
使用redisson获取分布式锁