Jedis操作sentinel、接続プール方式を採用
5584 ワード
問題のまとめ
1. server.conf sentinel.con , redis , ,
protected-model no
2. 127.0.0.1 not getSource from the pool
sentinel master ip
package com;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
public class SentinelTest {
private Logger logger=LoggerFactory.getLogger(SentinelTest.class);
private JedisSentinelPool jSentinelPool;
@Before
public void setUp(){
String masterName="mymaster";
//sentinel
Setset=new HashSet();
set.add("192.168.138.128:26380");
set.add("192.168.138.128:26381");
set.add("192.168.138.128:26382");
GenericObjectPoolConfig gPoolConfig=new GenericObjectPoolConfig();
gPoolConfig.setMaxIdle(10);
gPoolConfig.setMaxTotal(10);
gPoolConfig.setMaxWaitMillis(10);
gPoolConfig.setJmxEnabled(true);
jSentinelPool=new JedisSentinelPool(masterName,set,gPoolConfig);
}
@Test
public void testWriet(){
Jedis jedis=null;
for(int i=0;i<10;i++){
try {
jedis=jSentinelPool.getResource();
System.out.println(i);
String userKey="user"+i;
jedis.set(userKey, String.valueOf(i));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
logger.error(e.getMessage(),e);
}
finally{
if (jedis!=null) {
jedis.close();
}
}
}
}
}