jedis连接池配置

配置文件jedis.properties

maxTotal=50
maxIdle=10
host=127.0.0.7
port=6379

类JedisUtils.java

package JedisUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisUtils {
    //创建一个连接池
    private static JedisPool jedisPool;
    //
    static{
        //读取配置文件
        InputStream is = JedisUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        Properties pro=new Properties();
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //连接池配置
        JedisPoolConfig config=new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
        config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));

        //初始化连接池,把对应参数传递进去
        jedisPool=new JedisPool(config,pro.getProperty("host"), Integer.parseInt(pro.getProperty("port")));

    }

    //设置好后返回,返回一个Jedis对象
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}

 

测试类test.java

@Test
    public void test3(){
        //获得连接池对象
        Jedis jedis = JedisUtils.getJedis();
        //操作
        jedis.set("name","zhangsan");
        String name = jedis.get("name");
        System.out.println(name);
        //关闭连接
        jedis.close();

    }

 

posted @ 2019-06-08 18:43  findlisa  阅读(3915)  评论(0编辑  收藏  举报