ctgjedispool连接池配置
时间: 2023-09-01 18:11:01 浏览: 69
ctgjedispool是一个基于Jedis实现的连接池库,可以用来连接Redis数据库。
以下是ctgjedispool连接池的配置步骤:
1. 添加ctgjedispool依赖
在项目的pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>com.ctgjedispool</groupId>
<artifactId>ctgjedispool</artifactId>
<version>1.0.0</version>
</dependency>
```
2. 配置连接池参数
在项目的配置文件中,添加以下连接池参数:
```
#Redis服务器地址
redis.host=127.0.0.1
#Redis服务器端口
redis.port=6379
#Redis服务器密码
redis.password=123456
#连接池最大连接数
redis.maxTotal=500
#连接池最大空闲连接数
redis.maxIdle=10
#连接池最小空闲连接数
redis.minIdle=2
#获取连接时的最大等待毫秒数
redis.maxWaitMillis=10000
#在获取连接的时候检查有效性,默认为false
redis.testOnBorrow=false
#在归还连接的时候检查有效性,默认为false
redis.testOnReturn=false
```
3. 创建连接池
在Java代码中,使用以下代码创建连接池:
```
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(env.getProperty("redis.maxTotal")));
config.setMaxIdle(Integer.parseInt(env.getProperty("redis.maxIdle")));
config.setMinIdle(Integer.parseInt(env.getProperty("redis.minIdle")));
config.setMaxWaitMillis(Integer.parseInt(env.getProperty("redis.maxWaitMillis")));
config.setTestOnBorrow(Boolean.parseBoolean(env.getProperty("redis.testOnBorrow")));
config.setTestOnReturn(Boolean.parseBoolean(env.getProperty("redis.testOnReturn")));
JedisPool jedisPool = new JedisPool(config, env.getProperty("redis.host"), Integer.parseInt(env.getProperty("redis.port")), 10000, env.getProperty("redis.password"));
```
4. 使用连接池
使用连接池从Redis数据库中获取连接,进行相关操作:
```
try (Jedis jedis = jedisPool.getResource()) {
// 使用jedis进行相关操作
}
```