JedisPoolUtils

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 JedisPoolUtils {
private static JedisPool pool = null;

//静态代码块,最先加载
static {
    //加载配置文件
    InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
    Properties pro = new Properties();
    try {
        pro.load(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //获得池子对象
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxIdle(Integer.parseInt((String) pro.get("redis.maxIdle"))); //最大闲置个数
    poolConfig.setMinIdle(Integer.parseInt((String) pro.get("redis.minIdle"))); //最小闲置个数
    poolConfig.setMaxTotal(Integer.parseInt((String) pro.get("redis.maxTotal"))); //最大连接数
    pool = new JedisPool(poolConfig, pro.getProperty("redis.url"), Integer.parseInt((String) pro.get("redis.port")));

}

//获得jedis资源方法
public static Jedis getJedis() {
    return pool.getResource();
}

public static void main(String[] args) {
    Jedis jedis = getJedis();
    System.out.println(jedis.get("xxx"));
}

}

/**

  • 创建 redis.properties 文件
    *redis.maxIdle=30
    *redis.minIdle=10
    *redis.maxTotal=100
    *redis.url=192.168.232.128
    *redis.port=6379
    */

你可能感兴趣的:(JedisPoolUtils)