Jedis3.6.3 中JedisCluster 已经没有getResource方法了,那如何从JedisCluster 连接池中获取一个Jedis连接对象,请给出一个完整的示例
时间: 2024-02-05 21:12:29 浏览: 24
在Jedis3.6.3中,可以通过JedisCluster对象的`getConnection()`方法获取一个Jedis连接对象。以下是一个完整的示例:
```java
// 创建JedisCluster连接池
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("127.0.0.1", 6379));
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(100);
config.setMaxIdle(20);
config.setMinIdle(5);
config.setMaxWaitMillis(10000);
JedisCluster jedisCluster = new JedisCluster(nodes, config);
// 从连接池中获取Jedis连接对象
Jedis jedis = null;
try {
jedis = jedisCluster.getConnection();
// 使用Jedis连接对象操作Redis
jedis.set("key", "value");
String result = jedis.get("key");
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close(); // 释放连接对象到连接池中
}
}
```
注意:使用完Jedis连接对象后,需要调用`close()`方法将连接对象释放回连接池中。