|
|
package UnitTest;
|
|
|
|
|
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
|
|
import redis.clients.jedis.HostAndPort;
|
|
|
import redis.clients.jedis.JedisCluster;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Set;
|
|
|
|
|
|
public class TestRedisCluster {
|
|
|
public static void main(String[] args) throws IOException { // 创建集群的节点集合
|
|
|
Set nodes = new HashSet<>();// 添加节点到集群中
|
|
|
nodes.add(new HostAndPort("10.10.14.61", 8890));
|
|
|
nodes.add(new HostAndPort("10.10.14.61", 8891));
|
|
|
nodes.add(new HostAndPort("10.10.14.61", 8892));
|
|
|
nodes.add(new HostAndPort("10.10.14.61", 8893));
|
|
|
nodes.add(new HostAndPort("10.10.14.61", 8894));
|
|
|
nodes.add(new HostAndPort("10.10.14.61", 8895));
|
|
|
int maxTotal = Integer.parseInt("100");
|
|
|
int maxIdle = Integer.parseInt("100");
|
|
|
int MinIdle = Integer.parseInt("100");// 设置Redis Pool相关参数
|
|
|
|
|
|
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
|
|
|
poolConfig.setMaxTotal(maxTotal);
|
|
|
poolConfig.setMaxIdle(maxIdle);
|
|
|
poolConfig.setMinIdle(MinIdle);// 利用上面的集群节点nodes和poolConfig,创建redis集群连接池,并获取一个redis连接
|
|
|
JedisCluster jedisCluster = new JedisCluster(nodes, poolConfig);// 利用获取的jedisCluster可以进行jedis的所有操作
|
|
|
System.out.println(jedisCluster.set("name", "beyond"));
|
|
|
System.out.println(jedisCluster.get("name"));// 归还连接
|
|
|
jedisCluster.close();
|
|
|
}
|
|
|
}
|