package com.pcitc.dmb.common;
import com.pcitc.dmb.common.util.AppPropertiesUtils;
import com.pcitc.dmb.common.util.StringConstantsUtil;
import com.pcitc.dmb.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.*;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @Description: redis-Cluster工具类$
* @Date: 2023-08-11$
* @Param: $
* @return: $
*/
public class RedisClusterUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisClusterUtil.class);
private static JedisCluster jedisCluster;
private static JedisPool jedisPool;
// 连接实例的最大连接数
private static int MAX_ACTIVE = 10;
// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE =10;
private static int MIN_IDLE = 2;
private static int MAX_WAIT_MILLS = 30*1000;
private static int TIME_BETWEEN_EVICTION_RUNS_MILLSIS = 10*1000;
private static int MIN_EVICTABLE_IDLE_TIME_MILLS = 30*1000;
/**
* @Description: 初始化Redis连接池
* @Date: 2023/8/28
* @return:
* @param:
**/
static {
try{
AppPropertiesUtils.initAppProperties("application.properties");
//Redis集群节点信息
Set<HostAndPort> nodes = new HashSet<>();
/**
* @Description: 此处适配一个服务的六个端口的redis集群,本次在85服务器上部署了六个节点
* @Date: 2023/8/11
* @return:
* @param:
**/
String node = AppPropertiesUtils.getProperty("redis.node1");
if (StringUtils.isNotBlank(node)){
nodes.add(new HostAndPort(node,Integer.parseInt(AppPropertiesUtils.getProperty("redis.port1"))));
nodes.add(new HostAndPort(node,Integer.parseInt(AppPropertiesUtils.getProperty("redis.port2"))));
nodes.add(new HostAndPort(node,Integer.parseInt(AppPropertiesUtils.getProperty("redis.port3"))));
nodes.add(new HostAndPort(node,Integer.parseInt(AppPropertiesUtils.getProperty("redis.port4"))));
nodes.add(new HostAndPort(node,Integer.parseInt(AppPropertiesUtils.getProperty("redis.port5"))));
nodes.add(new HostAndPort(node,Integer.parseInt(AppPropertiesUtils.getProperty("redis.port6"))));
}
//Redis连接池配置
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(MAX_ACTIVE);//最大连接数 设置最大实例总数
poolConfig.setMaxIdle(MAX_IDLE);//最大空闲连接数
poolConfig.setMinIdle(MIN_IDLE);//最小空闲连接数
//表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
poolConfig.setMaxWaitMillis(MAX_WAIT_MILLS);
//向资源池借用连接时是否做连接有效性检测(ping)。检测到的无效连接将会被移除。
poolConfig.setTestOnBorrow(true);
// 向资源池归还连接时是否做有效性监测(ping)。监测到无效会被移除
poolConfig.setTestOnReturn(true);
//是否开启空闲资源监测,这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;
poolConfig.setTestWhileIdle(true);
//资源池中资源最小空闲时间,达到此值后空闲资源将会被移除,这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;
poolConfig.setMinEvictableIdleTimeMillis(MIN_EVICTABLE_IDLE_TIME_MILLS);
//表示idle(空闲) object evitor两次扫描之间要sleep的毫秒数
//空闲资源的监测周期
poolConfig.setTimeBetweenEvictionRunsMillis(TIME_BETWEEN_EVICTION_RUNS_MILLSIS);
//当资源池用尽后,调用者是否要等待,只有当值为true时,maxWaitMillis才会生效。
poolConfig.setBlockWhenExhausted(true);
//是否开启JMX监控
poolConfig.setJmxEnabled(true);
//创建JedisCluster
jedisCluster = new JedisCluster(nodes,poolConfig);
} catch (Exception e){
jedisCluster = null;
LOGGER.error("redis集群连接异常,异常信息:{}", e.getMessage());
}
}
/**
* @Description: 获取JedisCluster实例
* @Date: 2023/8/11
* @return: redis.clients.jedis.JedisCluster
* @param: []
**/
public synchronized static JedisCluster getJedisCluster() {
try{
if (jedisCluster != null){
return jedisCluster;
}else {
return null;
}
}catch (Exception e) {
LOGGER.error("redis集群连接异常,异常信息:{}", e.getMessage());
return null;
}
}
public static void main(String[] args) throws BusinessException, IOException {
JedisCluster jedisCluster = RedisClusterUtil.getJedisCluster();
if (jedisCluster == null){
throw new BusinessException("","","redis集群连接异常");
}
// 使用JedisCluster对象进行Redis操作
List<String> pidAssociation = jedisCluster.lrange(StringConstantsUtil.REDIS_PID_ASSOCIATION, 0, -1);
LOGGER.info(String.valueOf(pidAssociation));
}
}
RedisClusterUtil ツールクラス
Guess you like
Origin blog.csdn.net/Isonion/article/details/132586890
Recommended
Ranking