JedisSentinelPool接続Redisマスターノードツールクラス
6004 ワード
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
import java.util.HashSet;
import java.util.Set;
/**
* @Author mcg
* @Date 2019/4/5 20:22
**/
public class SentinelPool {
private static JedisSentinelPool pool;
private static Set sentinel = new HashSet<>();
static {
sentinel.add("47.**.***.77:26379");
sentinel.add("47.**.***.77:26380");
sentinel.add("47.**.***.77:26381");
initPool();
}
private static void initPool() {
pool = new JedisSentinelPool("mymaster",
sentinel,
new GenericObjectPoolConfig(),
3000);
}
public static Jedis getJedis() {
return pool.getResource();
}
public static void returnBrokenResource(Jedis jedis) {
pool.returnBrokenResource(jedis);
}
public static void returnResource(Jedis jedis) {
pool.returnResource(jedis);
}
}
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
/**
* @Author mcg
* @Date 2019/4/5 20:31
**/
@Slf4j
public class JedisSentinelUtil {
/**
* key
* @param key
* @param value
* @return
*/
public static String set(String key, String value) {
Jedis jedis = null;
String result = null;
try {
jedis = SentinelPool.getJedis();
result= jedis.set(key, value);
} catch (Exception e) {
log.error("set key:{}, error",key,e);
SentinelPool.returnBrokenResource(jedis);
return result;
}
SentinelPool.returnResource(jedis);
return result;
}
/**
* kv
* @param key
* @param value
* @param exTime
* @return
*/
public static String setEx(String key, String value,int exTime) {
Jedis jedis = null;
String result = null;
try {
jedis = SentinelPool.getJedis();
result = jedis.setex(key, exTime, value);
} catch (Exception e) {
log.error("setex key:{}, error",key,e);
SentinelPool.returnBrokenResource(jedis);
return result;
}
SentinelPool.returnResource(jedis);
return result;
}
/**
* key
* @param key
* @param value
* @return
*/
public static Long setNx(String key, String value) {
Jedis jedis = null;
Long result = null;
try {
jedis = SentinelPool.getJedis();
result= jedis.setnx(key, value);
} catch (Exception e) {
log.error("setnx key:{}, error",key,e);
SentinelPool.returnBrokenResource(jedis);
return result;
}
SentinelPool.returnResource(jedis);
return result;
}
/**
* key
* @param key
* @return
*/
public static Long del(String key) {
Jedis jedis = null;
Long result = null;
try {
jedis = SentinelPool.getJedis();
result = jedis.del(key);
} catch (Exception e) {
log.error("del key {}, error",key,e);
SentinelPool.returnBrokenResource(jedis);
return result;
}
SentinelPool.returnResource(jedis);
return result;
}
/**
* Key
* @param key
* @return
*/
public static String get(String key) {
Jedis jedis = null;
String result = null;
try {
jedis = SentinelPool.getJedis();
result = jedis.get(key);
} catch (Exception e) {
log.error("get key {}, error",key,e);
SentinelPool.returnBrokenResource(jedis);
return result;
}
SentinelPool.returnResource(jedis);
return result;
}
/**
* ,
* @param key
* @param value
* @return
*/
public static String getSet(String key,String value) {
Jedis jedis = null;
String result = null;
try {
jedis = SentinelPool.getJedis();
result = jedis.getSet(key, value);
} catch (Exception e) {
log.error("getSet key {}, error",key,e);
SentinelPool.returnBrokenResource(jedis);
return result;
}
SentinelPool.returnResource(jedis);
return result;
}
/**
*
* @param key
* @param exTime
* @return
*/
public static Long expire(String key, int exTime) {
Jedis jedis = null;
Long result = null;
try {
jedis = SentinelPool.getJedis();
result= jedis.expire(key, exTime);
} catch (Exception e) {
log.error("expire key:{}, error",key,e);
SentinelPool.returnBrokenResource(jedis);
return result;
}
SentinelPool.returnResource(jedis);
return result;
}
/**
* dbsize
* @return
*/
public static Long getDbSize() {
Jedis jedis = null;
Long result = null;
try {
jedis = SentinelPool.getJedis();
result = jedis.dbSize();
} catch (Exception e) {
log.error("get dbSize, error",e);
SentinelPool.returnBrokenResource(jedis);
return result;
}
SentinelPool.returnResource(jedis);
return result;
}
}
クライアントコードとredisが同じ物理マシンにある場合は、次の構成に問題はありません.sentinel monitor mymaster 127.0.0.1 6379 2しかし、クライアントコードとRedisが同じマシンにいない場合は、このように設定しないでください.そうしないと、redisに接続できません.get-master-addr-by-name
で入手したプライマリノードは127.0.0.1 6379
ですが、コードを実行しているマシンにはRedisはありません.接続を拒否するエラーが表示されます.実際のローカルエリアネットワークIPまたは外部ネットワークIPに設定する必要があります.