Lettuce 5.1.1接続プールはRedis 5.0クラスタ環境テスト

2852 ワード

package org.util;

import io.lettuce.core.ReadFrom;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands;
import io.lettuce.core.support.ConnectionPoolSupport;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * lettuce-core 5.1.1
 * commons-pool2  2.6.0 (2.6.0 , )
 */
public class RedisStreamDemo {

    public static void main(String[] args) {

        List list = new ArrayList<>();
        list.add(RedisURI.create("redis://192.168.2.4:7000"));
        list.add(RedisURI.create("redis://192.168.2.5:7000"));
        list.add(RedisURI.create("redis://192.168.2.6:7000"));
        list.add(RedisURI.create("redis://192.168.2.4:7001"));
        list.add(RedisURI.create("redis://192.168.2.5:7001"));
        list.add(RedisURI.create("redis://192.168.2.6:7001"));
        RedisClusterClient clusterClient = RedisClusterClient.create(list);
        // Redis
        RedisClusterClient client = RedisClusterClient.create(list);
        GenericObjectPool> pool;
        GenericObjectPoolConfig> poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMinIdle(8);
        poolConfig.setMaxIdle(8);
        poolConfig.setMaxTotal(16);
        poolConfig.setMinEvictableIdleTimeMillis(1000*30);
        poolConfig.setSoftMinEvictableIdleTimeMillis(1000*30);
        poolConfig.setMaxWaitMillis(0);
        pool = ConnectionPoolSupport.createGenericObjectPool(() -> {
           System.err.println("Requesting new StatefulRedisClusterConnection "+System.currentTimeMillis());
            return client.connect();
        }, poolConfig);

        StatefulRedisClusterConnection connection = null;
        try {
            connection = pool.borrowObject();
            connection.setReadFrom(ReadFrom.MASTER_PREFERRED);

            RedisAdvancedClusterAsyncCommands commands = connection.async();
            commands.set("id","taozhongyu");
            RedisFuture future = commands.get("id");
            String str = future.get();
            System.out.println(str);

        } catch (Exception e) {
            e.printStackTrace();
        }
        pool.close();
        clusterClient.shutdown();    

    }
}