发布于 2016-01-03 03:06:43 | 278 次阅读 | 评论: 0 | 来源: PHPERZ
Jedis Redis的Java客户端
Jedis 是 Redis 官方首选的 Java 客户端开发包。
Redis简单配置和使用
1. windows启动redis-server要配置一下maxheap 10240000,否则报错“To work around this you may either increase the size of the system paging file, or decrease the size of the Redis heap with the --maxheap flag”
2. 使用配置项requirepass xxxxxx为redis配置强密码,参考漏洞《Redis 未授权访问缺陷可轻易导致系统被黑》
3. redis主从配置也很简单,从redis使用配置项slaveof 192.168.0.118 6379配置主redis,如果主redis设置了密码,从redis需使用配置项masterauth xxxxxx配置访问主redis的密码,从redis和主redis都要设置密码。
4. windows redis客户端使用命令redis-cli -h 192.168.0.118 -p 6379 -a password启动
5. 进一步提高性能的措施:读写分离、redis集群
6. redis官网:http://redis.io/
Jedis的简单代码示例
Jedis官网:https://github.com/xetorthio/jedis
Jedis对象不是线程安全的
简单示例
public static void test(String[] args) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10);
config.setMaxIdle(5);
config.setTestOnBorrow(true);
JedisPool pool = new JedisPool(config, serverIp, serverPort, timeout, passwrod);
if (pool != null) {
Jedis jedis = pool.getResource();
try {
Set<String> keys = jedis.keys("*");
System.out.println(keys);
}
catch (JedisConnectionException e) {
if (jedis != null) {
pool.returnBrokenResource(jedis);
jedis = null;
}
}
finally {
if (jedis != null) {
pool.returnResource(jedis);
}
}
}
}
4. 《Jedis returnResource使用注意事项》提到,不能不管什么情况都一律使用returnResource,其中的示例调整为如下
public static void test(String[] args) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10);
config.setMaxIdle(5);
config.setTestOnBorrow(true);
JedisPool pool = new JedisPool(config, serverIp, serverPort, timeout, passwrod);
Jedis jedis = null;
boolean broken = false;
if (pool != null) {
try {
jedis = pool.getResource();
Set<String> keys = jedis.keys("*");
System.out.println(keys);
}
catch (JedisException e) {
broken = handleJedisException(e);
throw e;
}
finally {
closeResource(pool, jedis, broken);
}
pool.destroy();
}
}
private static boolean handleJedisException(JedisException jedisException) {
if (jedisException instanceof JedisConnectionException) {
logger.error("Redis connection lost.", jedisException);
}
else if (jedisException instanceof JedisDataException) {
if ((jedisException.getMessage() != null) && (jedisException
.getMessage().indexOf("READONLY") != -1)) {
logger.error("Redis connection are read-only slave.",
jedisException);
}
else {
// dataException, isBroken=false
return false;
}
}
else {
logger.error("Jedis exception happen.", jedisException);
}
return true;
}
/**
* Return jedis connection to the pool, call different return methods
* depends on the conectionBroken status.
*/
protected static void closeResource(JedisPool pool, Jedis jedis,
boolean conectionBroken) {
try {
if (conectionBroken) {
pool.returnBrokenResource(jedis);
}
else {
pool.returnResource(jedis);
}
}
catch (Exception e) {
logger.error("return back jedis failed, will fore close the jedis.",
e);
destroyJedis(jedis);
}
}
public static void destroyJedis(Jedis jedis) {
if ((jedis != null) && jedis.isConnected()) {
try {
try {
jedis.quit();
}
catch (Exception e) {
}
jedis.disconnect();
}
catch (Exception e) {
}
}
}
关于此代码,原帖下有人回复“是不是没有配置 testOnBorrow = true? 如果配置了这个参数,每次取jedis时,都会测试 jedis.isConnected和ping一下服务端的。”。