You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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();
}
}