1、pom引入依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency>
2、property配置文件,配置集群信息
REDIS_CLUSTER_CFG=127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004
3、封装实例
public class RedisClusterUtils{ public static JedisCluster getInstence() { String reClusCfg = System.getProperty("REDIS_CLUSTER_CFG"); // 节点 ipport配置 String[] ipPorts = reClusCfg.split(","); Set<HostAndPort> nodes = new HashSet<>(); for (String ipPort : ipPorts){ String[] ipPortPair = ipPort.split(":"); nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim()))); } JedisCluster jedisCluster = new JedisCluster(nodes, 1000, 1000); return jedisCluster; } }
4、进行操作
RedisClusterUtils.getInstence().zadd("order_table", System.currentTimeMillis() - 5*3600*1000, "5564634636356");
System.out.println( RedisClusterUtils.getInstence().zrange("order_table", 0, -1).toString() );