Redis链接方式

来源:互联网 发布:高速公路客流量数据 编辑:程序博客网 时间:2024/05/20 17:38
package com.otsuser.usualpassenger.codisConnect;


import io.codis.jodis.JedisResourcePool;
import io.codis.jodis.RoundRobinJedisPool;


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


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import redis.clients.jedis.JedisPoolConfig;


/**
 * xiaoxu
 */
public class PoolFactory {


private Logger log = LoggerFactory.getLogger(PoolFactory.class);


// 创建当前的连接池类
JedisResourcePool jedisResourcePool;


/**
* 获取链接池

* @return
*/
public void initJedisPool() {
Properties prop = new Properties();
InputStream is = PoolFactory.class
.getResourceAsStream("/codis-connect.properties");
try {
// 读取配置文件
prop.load(is);
String zkAddr = prop.getProperty("zkAddr");
String zkProxyDir = prop.getProperty("zkProxyDir");
int maxTotal = Integer.parseInt(prop.getProperty("maxTotal"));
int maxIdle = Integer.parseInt(prop.getProperty("maxIdle"));
int minIdle = Integer.parseInt(prop.getProperty("minIdle"));
long maxWaitMillis = Integer.parseInt(prop
.getProperty("maxWaitMillis"));
int soTimeoutMs = Integer.parseInt(prop.getProperty("soTimeoutMs"));
int timeoutMs = Integer.parseInt(prop.getProperty("timeoutMs"));
int zkSessionTimeoutMs = Integer.parseInt(prop
.getProperty("zkSessionTimeoutMs"));


JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxTotal(maxTotal);
poolConfig.setMaxWaitMillis(maxWaitMillis);
// 获取连接池
jedisResourcePool = RoundRobinJedisPool.create()
.soTimeoutMs(soTimeoutMs).timeoutMs(timeoutMs)
.curatorClient(zkAddr, zkSessionTimeoutMs)
.zkProxyDir(zkProxyDir).poolConfig(poolConfig).build();
} catch (IOException e) {
log.error("initJedisPool error:", e.getStackTrace().toString());
}
}


/**
* 返回当前的实例

* @return
*/
public JedisResourcePool getjedisResourcePool() {
return jedisResourcePool;
}


}





codis-connect.properties

# codis connect
zkAddr=10.2.202.26:2181,10.2.202.27:2181,10.2.202.28:2181,10.2.202.29:2181,10.2.202.30:2181
zkProxyDir=/jodis/usualpassenger
maxTotal=10000
maxIdle=1000
minIdle=100
maxWaitMillis=100
soTimeoutMs=10000
timeoutMs=10000
zkSessionTimeoutMs=50000




原创粉丝点击