redis集群连接池实例

之前写了一个单机版的redis连接池实例,这里我在本机搭建了三个redis服务作为测试。
连接池示例:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

public class JedisPool {
    //JedisPool连一台Redis,ShardedJedisPool连Redis集群,通过一致性哈希算法决定把数据存到哪台上,算是一种客户端负载均衡,
    private static ShardedJedisPool pool;
    private static int MAXTOTAL=300;
    private static int MAXIDLE=200;
    private static int MINIDEL=10;
    private static int MAXWAIRMILLIS=1000;
    private static Boolean TESTONBORROW=true;
    private static Boolean TESTONRETURN=false;
    private static Boolean TESTWHILEIDLE=false;
    //静态代码初始化连接池配置
    static {
        try {
            //初始化连接池参数配置
            JedisPoolConfig config=initConfig();
            List shards=new ArrayList();
            String host="localhost:6379:testredis,localhost:6380:testredis,localhost:6381:testredis";//服务器地址,密码
            Set hosts=init(host);
            for (String hs:hosts){
                String[] values=hs.split(":");
                JedisShardInfo shard=new JedisShardInfo(values[0],Integer.parseInt(values[1]));
                if(values.length>2){
                    shard.setPassword(values[2]);
                }
                shards.add(shard);
            }
            pool=new ShardedJedisPool(config,shards);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //初始化连接池参数
    private static JedisPoolConfig initConfig(){
        JedisPoolConfig config=new JedisPoolConfig();
        config.setMaxTotal(MAXTOTAL);
        config.setMaxIdle(MAXIDLE);
        config.setMinIdle(MINIDEL);
        config.setMaxWaitMillis(MAXWAIRMILLIS);
        config.setTestOnBorrow(TESTONBORROW);
        config.setTestOnReturn(TESTONRETURN);
        config.setTestWhileIdle(TESTWHILEIDLE);
        return config;
    }

    private static Set init(String values){
        if(StringUtils.isBlank(values)){
            throw new NullPointerException("redis host not found");
        }
        Set paramter=new HashSet();
        String[] sentinelArray=values.split(",");
        for(String str:sentinelArray){
            paramter.add(str);
        }
        return paramter;
    }

    public static ShardedJedisPool getShardedJedisPool(){
        return pool;
    }
}

调用连接池示例

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisDataSource  {

    private static ThreadLocal jedisLocal = new ThreadLocal();
    private static ShardedJedisPool pool;
    static {
        pool = JedisPool.getShardedJedisPool();
    }

    public  ShardedJedis getClient() {
        ShardedJedis jedis = jedisLocal.get();
        if (jedis == null) {
            jedis = pool.getResource();
            jedisLocal.set(jedis);
        }
        return jedis;
    }

    //关闭连接
    public void returnResource() {
        ShardedJedis jedis = jedisLocal.get();
        if (jedis != null) {
            pool.destroy();
            jedisLocal.set(null);
        }
    }
}

由于我在公司中使用的是这种分片式的机制,对于redis主从也是类似的,不过是调用的api不同而已,这里给出实例仅供参考。

你可能感兴趣的:(redis集群连接池实例)