Lettuce4.2 连接池 操作Redis3.2.6
4.2连接池貌似只能连接单机Redis??????!( RedisClusterClient没有pool()方法! 如果有知道集群连接池怎么使用的请告诉我,谢谢 )顾当连接redis集群时可能出现MOVE...异常
此连接池在lettuce4.3中已经弃用了.
import java.util.concurrent.TimeUnit;
import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisConnectionPool;
import com.lambdaworks.redis.RedisFuture;
import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.api.async.RedisAsyncCommands;
/**
* lettuce4.2连接池, 4.3已经废弃了 仅能连接单台redis?! 如果是Redis集群,可能因为哈希槽报错:MOVE.....
*/
public class Lettuce_4_2_Pool {
private static final RedisConnectionPool<RedisAsyncCommands<String, String>> pool;
private static final String REDIS_HOST = "192.168.37.128";
private static final int REDIS_PORT = 7000;
private static final int REDIS_EXPIRE = 1;
private static final int REDIS_MAX_IDLE = 10;
private static final int REDIS_MAX_ACTIVE = 10;
static {
RedisURI redisURI = new RedisURI();
redisURI.setHost(REDIS_HOST);
redisURI.setPort(REDIS_PORT);
// redisURI.setPassword("");
redisURI.setTimeout(REDIS_EXPIRE);
redisURI.setUnit(TimeUnit.SECONDS);
// 也可直接将url的字符串传入 RedisClient.create()方法中
// eg:redis://[password@]host[:port][/databaseNumber]
RedisClient client = RedisClient.create(redisURI); //
// 从redis客户端中获取一个异步的redis缓冲池
pool = client.asyncPool(REDIS_MAX_IDLE, REDIS_MAX_ACTIVE);
// 参数说明:REDIS_MAX_IDLE 为本缓冲池中最大闲置连接数量 // REDIS_MAX_ACTIVE为本缓冲池中最大活动连接数量
}
// 从缓冲池中获取一个连接
public static RedisAsyncCommands<String, String> getRedisConnection() {
return pool.allocateConnection();
}
// 关闭服务器时 关闭缓冲池
public static void shutDown() {
pool.close();
}
public static void main(String[] args) {
try (RedisAsyncCommands<String, String> commads = pool.allocateConnection()) {
RedisFuture<String> future = commads.get("test2");
String str = future.get();
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}