private static JedisPool jedisPool;
static {
//加载配置文件
Properties properties = new Properties();
//获取配置路径
InputStream resourceAsStream = JedisUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
//写连接池配置
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));
jedisPoolConfig.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));
//创建连接池,把配置导入
jedisPool = new JedisPool(jedisPoolConfig,
properties.getProperty("host"), //ip地址
Integer.parseInt(properties.getProperty("port")));//端口号
}
public static Jedis getResource() {
Jedis resource = jedisPool.getResource();
return resource;
}
jedis.properties
host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10