redis 哨兵连接池 JedisSentinelPool 实现对redis集群操作(一般是 一主二从三哨兵)

redis 哨兵连接池 JedisSentinelPool 实现对redis集群操作(一般是 一主二从三哨兵)

redis 集群主从复制+哨兵,当主服务器宕机时,可以自动将从服务器切换为主服务器。提高系统的稳定性。

1.配置基本参数

需要的依赖:

org.springframework.boot

spring-boot-starter-data-redis

redis.clients

jedis

2.9.0

在 yml 文件中配置基本参数

myjedis:

redis:

config:

#哨兵集群节点

nodes: 172.21.1.111:26379,172.21.2.111:26379

masterName: mymaster

password: 123456

database: 8

#总数

maxTotal: 100

#最大空闲数

maxIdle: 50

#最小空闲数

minIdle: 10

timeout: 60000

maxWaitMillis: 5000

创建配置类,使用 @ConfigurationProperties(prefix = "myjedis.redis.config") 注解读取yml 中配置,参数名称和路径要与yml文件中配置的对应。

@ConfigurationProperties(prefix = "myjedis.redis.config")

@Component

@Data

public class RedisConfigProperties {

private String nodes;

private String masterName;

private String password;

private int database;

private String maxTotal;

private String maxIdle;

private String minIdle;

private String timeout;

private String maxWaitMillis;

//以下单机redis配置信息

private String mode;

private String host;

private String port;

private String pass;

}

2.配置redis哨兵模式

@Configuration

public class RedisPoolConfig {

/** 配置类信息 */

@Autowired

RedisConfigProperties redisConfigProperties;

/** 初始化 jedis 连接池的配置 */

@Bean

public JedisPoolConfig getJedisPoolConfig(){

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

jedisPoolConfig.setMaxTotal(Integer.parseInt(redisConfigProperties.getMaxTotal()));

jedisPoolConfig.setMaxIdle(Integer.parseInt(redisConfigProperties.getMaxIdle()));

jedisPoolConfig.setMinIdle(Integer.parseInt(redisConfigProperties.getMinIdle()));

jedisPoolConfig.setMaxWaitMillis(Integer.parseInt(redisConfigProperties.getMaxWaitMillis()));

jedisPoolConfig.setTestOnReturn(true);

jedisPoolConfig.setTestOnBorrow(true);

return jedisPoolConfig;

}

/** 根据 jedis 连接池配置创建连接池 */

@Bean

public Pool getJedisPool(JedisPoolConfig jedisPoolConfig){

//集群部署

Set nodeSet = new HashSet<>();

String[] nodeStr = redisConfigProperties.getNodes().split(",");

for(String node:nodeStr){

nodeSet.add(node);

}

return new JedisSentinelPool(redisConfigProperties.getMasterName(), nodeSet, jedisPoolConfig, 2000,

2000, redisConfigProperties.getPassword(), redisConfigProperties.getDatabase());

}

/** 通过连接池获取 jedis 实例 */

@Bean

public Jedis getJedis(Pool jedisPool){

return jedisPool.getResource();

}

}

3.配置 ApplicationContextAware 实现类,获取spring容器中的实例 Bean。

@Component

public class ApplicationContextProvider implements ApplicationContextAware {

/**

* 上下文对象实例

*/

public static ApplicationContext applicationContext;

@Override

public void setApplicationContext(ApplicationContext context) throws BeansException {

ApplicationContextProvider.applicationContext = context;

}

/**

* 获取applicationContext

* @return

*/

public static ApplicationContext getApplicationContext() {

return applicationContext;

}

/**

* 通过class获取Bean.

* @param clazz

* @param

* @return

*/

public static T getBean(Class clazz){

return getApplicationContext().getBean(clazz);

}

/**

* 通过name获取 Bean.

* @param name

* @return

*/

public static Object getBean(String name){

return getApplicationContext().getBean(name);

}

}

4.创建 JedisUtil 工具类,使用哨兵模式连接池。

public class JedisUtil {

private static Pool jedisSentinelPool = ApplicationContextProvider.getBean(JedisSentinelPool.class);

public static Pool getJedisSentinelPool() {

if (jedisSentinelPool == null) {

jedisSentinelPool = ApplicationContextProvider.getBean(JedisSentinelPool.class);

}

return jedisSentinelPool;

}

public static String get(String key) {

String value = null;

Jedis jedis = getJedisSentinelPool().getResource();

if (jedis.exists(key)) {

value = jedis.get(key);

value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;

}

jedis.close();

return value;

}

public static long del(String key) {

long result = 0;

Jedis jedis = getJedisSentinelPool().getResource();

if (jedis.exists(key)){

result = jedis.del(key);

log.debug("del {}", key);

}else{

log.debug("del {} not exists", key);

}

jedis.close();

return result;

}

/**

* 设置缓存

* @param key 键

* @param value 值

* @return

*/

public static String set(String key, String value) {

return setex(key, value, 0);

}

/**

* 设置缓存

* @param key 键

* @param value 值

* @param cacheSeconds 超时时间,0为不超时

* @return

*/

public static String setex(String key, String value, int cacheSeconds) {

Jedis jedis = getJedisSentinelPool().getResource();

String result = null;

if (cacheSeconds != 0) {

jedis.setex(key, cacheSeconds, value);

}else{

result = jedis.set(key, value);

}

log.debug("set {} = {}", key, value);

jedis.close();

return result;

}

/**

* 以秒为单位返回 key 的剩余过期时间

* @param key 键

* @return

*/

public static long ttl(String key) {

Jedis jedis = getJedisSentinelPool().getResource();

long time = jedis.ttl(key);

jedis.close();

return time/1000;

}

}

相关推荐

DNF宠物大盘点 那些绝版宠物你包里是否有呢?
365足球平台入口

DNF宠物大盘点 那些绝版宠物你包里是否有呢?

📅 10-08 👁️ 2214
1μA等于多少安培
365体育投注3

1μA等于多少安培

📅 08-20 👁️ 2661
Microsoft Applocale无法安装怎么办?Microsoft Applocale无法安装的解决教程
c1驾照升b2需要多少钱
谁知道365bet网址

c1驾照升b2需要多少钱

📅 08-31 👁️ 1863
网络并非法外之地,言论自由也需谨言慎行
365体育投注3

网络并非法外之地,言论自由也需谨言慎行

📅 07-06 👁️ 2162
《英雄联盟lol》皇族战队详细资料玩家心得
谁知道365bet网址

《英雄联盟lol》皇族战队详细资料玩家心得

📅 10-26 👁️ 396
香港改編日本流行曲的黃金時代!13首改編自日語歌曲的廣東歌 - 喜愛日本 LikeJapan
《黑神话悟空》升满有额外效果装备一览
365足球平台入口

《黑神话悟空》升满有额外效果装备一览

📅 07-11 👁️ 2142
2025年CF游戏里怎么实现飞天?有哪些方法可以尝试?