JedisSentinelPool 连接Redis 主节点工具类

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<String> 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 即可解决。

发布了41 篇原创文章 · 获赞 25 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/Andy86869/article/details/89047746