`

Netty-Redis-Lettuce 关于闲置连接和活动连接的一点心得

阅读更多

写netty项目的时候,学习了一下相关的Redis API.(Lettuce框架的)

了解了一下redis缓冲池中的连接分配机制:

拿来与大家分享,简要说明一下,觉得不好的不要喷....

这也是做笔记的一种方式:

(如果有不对的希望大家帮忙指出来 感谢感谢!!)

 

//redis pool
private static final RedisConnectionPool<RedisAsyncCommands<String, String>>  pool;

//init pool
static {
    //创建一个RedisURI对象 并为其设置必要的参数
    RedisURI redisURI = new RedisURI();
    redisURI.setHost( ConstUtil.REDIS_HOST );
    redisURI.setPort( ConstUtil.REDIS_PORT );
    redisURI.setPassword( ConstUtil.REDIS_PASSWORD );
    redisURI.setTimeout( ConstUtil.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( ConstUtil.REDIS_MAX_IDLE, ConstUtil.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 ) {
    //RedisAsyncCommands<String, String> commands = getRedisConnection();
    //RedisAsyncCommands<String, String> commands1 = getRedisConnection();
    RedisURI redisURI = new RedisURI();
    redisURI.setHost( ConstUtil.REDIS_HOST );
    redisURI.setPort( ConstUtil.REDIS_PORT );
    redisURI.setPassword( ConstUtil.REDIS_PASSWORD );
    redisURI.setTimeout( ConstUtil.REDIS_EXPIRE );
    redisURI.setUnit( TimeUnit.SECONDS );
    RedisClient client = RedisClient.create( redisURI );
    //此处将redis缓冲池中 最大闲置连接数量控制为1
    RedisConnectionPool<RedisAsyncCommands<String, String>> pool = client.asyncPool( 1, ConstUtil.REDIS_MAX_ACTIVE );
    //打印当前缓冲池中的 连接情况 活动连接为0 闲置连接也为0
    System.out.println("Before NumActive is: " + pool.getNumActive() + " NumIdel is: " +pool.getNumIdle());

    //此处使用try(){}语句块获得一个新连接
    try(RedisAsyncCommands<String, String> connection = pool.allocateConnection()){
        connection.get( "key" );//该连接当前状态为ALLOCATED
        //此处加断点可观察到该连接为 1e8b7643
        System.out.println("call try-catch");
    }//由于RedisAsyncCommands继承了closeable接口 所以此处语句块执行完毕后connection将被自动关闭 缓冲池中连接为1e8b7643 当前状态为IDLE
    //打印当前缓冲池中的 连接情况 活动连接为0 闲置连接也为1 
    System.out.println( "After call{ 01 } NumActive is: " + pool.getNumActive() + " NumIdel is: " + pool.getNumIdle() );
    //此处使用try(){}语句块获得一个连接
    try(RedisAsyncCommands<String, String> connection = pool.allocateConnection()){//此处返回连接为 1e8b7643 为刚才的连接  当前状态为ALLOCATED
        connection.get( "key" ); 
        System.out.println( "call try-catch" );
        try(RedisAsyncCommands<String, String> connection2 = pool.allocateConnection()){//此处返回一个新的连接为 3f57bcad
            connection2.get( "key" );
            System.out.println("call try-catch");
        }//此处释放connection2 
        System.out.println("isAuto-close?”);//此处添加断点 缓存池中连接为 3f57bcad 状态为 IDLE
    }
     //打印当前缓冲池中的 连接情况 活动连接为0 闲置连接也为1 
    System.out.println( "After call{ 02 } NumActive is: " + pool.getNumActive() + " NumIdel is: " + pool.getNumIdle() );
    //此处使用try(){}语句块获得一个连接 该链接为3f57bcad 由此可见先被放回缓冲池的连接 状态由ALLOCATED 置为 IDLE
 后被释放的连接如果缓存池中的闲置连接数量达到了设置的最大闲置数量 则不放入缓冲池作为IDLE连接
    try(RedisAsyncCommands<String, String> connection = pool.allocateConnection()){
        connection.get( "key" );
        System.out.println("call try-catch");
    }
    //pool.close();
    //打印当前缓冲池中的 连接情况 活动连接为0 闲置连接也为1     
    System.out.println( "After call{ 03 } NumActive is: " + pool.getNumActive() + " NumIdel is: " + pool.getNumIdle() );

    RedisConnectionPool<RedisAsyncCommands<String, String>> pool2 = client.asyncPool( 1, ConstUtil.REDIS_MAX_ACTIVE );
    RedisAsyncCommands<String, String> connection = pool2.allocateConnection();
    connection.get( "key" );
    System.out.println( "unused TRY Before NumActive is: " + pool2.getNumActive() + " NumIdel is: " + pool2.getNumIdle() );
    pool2.freeConnection( connection );
    System.out.println( "unused TRY After call{ 03 } NumActive is: " + pool2.getNumActive() + " NumIdel is: " + pool2.getNumIdle() );

}

 

 

 

2
3
分享到:
评论
2 楼 HarborChung 2016-07-30  
islandforus 写道
按照你的这样写法,连接池获取不到怎么回事呢?

public class RedisOption {

    //redis pool
    private static final RedisConnectionPool<RedisAsyncCommands<String, String>> pool;

    //init pool
    static {
        RedisURI redisURI = new RedisURI();//init uri to connect
        redisURI.setHost(ConstUtil.REDIS_HOST);//host
        redisURI.setPort(ConstUtil.REDIS_PORT);//port
        redisURI.setPassword(ConstUtil.REDIS_PASSWORD);  //pwd
        redisURI.setUnit(TimeUnit.SECONDS);
        RedisClient client = RedisClient.create(redisURI);//create client
        pool = client.asyncPool(ConstUtil.REDIS_MAX_IDLE, ConstUtil.REDIS_MAX_ACTIVE);  //5  20
    }

    //从缓冲池中获取一个连接
    public static RedisAsyncCommands<String, String> getRedisConnection() {
        return pool.allocateConnection();
    }

    //关闭服务器时 关闭缓冲池
    public static void shutDown() {
        pool.close();
    }
}
这样写你试试 后来改成这种方式获取了 可以直接用 上面那个只是个demo 共同进步昂 这几个月一直在加班 没咋看博客
1 楼 islandforus 2016-06-12  
按照你的这样写法,连接池获取不到怎么回事呢?

相关推荐

Global site tag (gtag.js) - Google Analytics