Overview
- For codis and jodis.
Codis
- TBD...
Jodis
- Java client for codis.
- Jodis is a java client for codis based on Jedis and Curator.
Features
- Use a round robin policy to balance load to multiple codis proxies.
- Detect proxy online and offline automatically.
Usage
- pom.xml
<!-- jodis -->
<dependency>
<groupId>io.codis.jodis</groupId>
<artifactId>jodis</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>3.1.0</version>
</dependency> - java
jedisPool = RoundRobinJedisPool.create()
.curatorClient(CODIS_ZK_QUORUM, 30000).zkProxyDir(CODIS_ZK_PROXY_DIR).build();
Jedis jedis = jedisPool.getResource();
// then do something with jedis
Jedis
- 使用池可以减少系统在使用所需对象时创建对象的开销,从而提高系统性能和效率。
Usage
- conf
#*****************jedis连接参数设置*********************# #redis服务器ip # redis.ip= #redis服务器端口号# redis.port=
#************************jedis池参数设置*******************# #jedis的最大分配对象# jedis.pool.maxActive=3000 #jedis最大保存idel状态对象数 # jedis.pool.maxIdle=1000 #jedis池没有对象返回时,最大等待时间 # jedis.pool.maxWait=1500 #jedis调用borrowObject方法时,是否进行有效检查# jedis.pool.testOnBorrow=true #jedis调用returnObject方法时,是否进行有效检查 # jedis.pool.testOnReturn=true - java
private static JedisPool pool;
// static code to init pool conf
static {
try {
Properties props = new Properties();
props.load(MyJedisPool.class.getClassLoader().getResourceAsStream("redis.properties"));
// create jedis pool conf instance
JedisPoolConfig config = new JedisPoolConfig(); // set confs for pool
config.setMaxActive(Integer.valueOf(props.getProperty("jedis.pool.maxActive"))); config.setMaxIdle(Integer.valueOf(props.getProperty("jedis.pool.maxIdle"))); config.setMaxWait(Long.valueOf(props.getProperty("jedis.pool.maxWait"))); config.setTestOnBorrow(Boolean.valueOf(props.getProperty("jedis.pool.testOnBorrow"))); config.setTestOnReturn(Boolean.valueOf(props.getProperty("jedis.pool.testOnReturn"))); // create jedis pool according to confs
pool = new JedisPool(config, props.getProperty("redis.ip"), Integer.valueOf(props.getProperty("redis.port")));
} catch (IOException e) {
e.printStackTrace();
}
} // get jedis object
public static Jedis getJedisObject() {
return pool.getResource(); } // return jedis object
public static void recyleJedisObject(Jedis jedis) {
pool.returnResource(jedis);
} // test jedis pool
public static void main(String[] args) {
Jedis jedis = getJedisObject();
// oper on the jedis object
recyleJedisObject(jedis);
}