首页 > 数据库 >springboot 配置redis集群 JedisCluster 3主3从 哈希槽模式

springboot 配置redis集群 JedisCluster 3主3从 哈希槽模式

时间:2023-02-12 01:00:18浏览次数:47  
标签:springboot redis RedisClient jedis 哈希 new static null

package com.estate.util;

import redis.clients.jedis.*;

import java.util.HashSet;
import java.util.Set;

public class RedisClient {

    private static JedisCluster pool = null;

//    private static JedisCluster jedis = null;

    //可用连接实例的最大数目,默认为8;
    //如果赋值为-1,则表示不限制,如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
    private static Integer MAX_TOTAL = 1024;
    //控制一个pool最多有多少个状态为idle(空闲)的jedis实例,默认值是8
    private static Integer MAX_IDLE = 200;
    //等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。
    //如果超过等待时间,则直接抛出JedisConnectionException
    private static Integer MAX_WAIT_MILLIS = 10000;
    //在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作;
    //如果为true,则得到的jedis实例均是可用的
    private static Boolean TEST_ON_BORROW = true;
    //在空闲时检查有效性, 默认false
    private static Boolean TEST_WHILE_IDLE = true;
    //是否进行有效性检查
    private static Boolean TEST_ON_RETURN = true;

    //静态代码初始化池配置
    static {
        try {
            //创建jedis池配置实例
            JedisPoolConfig config = new JedisPoolConfig();
            //设置池配置项值
            config.setMaxTotal(MAX_TOTAL);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT_MILLIS);
            config.setTestOnBorrow(TEST_ON_BORROW);
            config.setTestWhileIdle(TEST_WHILE_IDLE);
            config.setTestOnReturn(TEST_ON_RETURN);
            //根据配置实例化jedis池


            Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
            //redis集群master
//            jedisClusterNode.add(new HostAndPort("127.0.0.1", 6381));
//            jedisClusterNode.add(new HostAndPort("127.0.0.1", 6382));
//            jedisClusterNode.add(new HostAndPort("127.0.0.1", 6383));
            //redis集群slave(只需要连接slave,连接master会报错)
            jedisClusterNode.add(new HostAndPort("127.0.0.1", 6384));
            jedisClusterNode.add(new HostAndPort("127.0.0.1", 6385));
            jedisClusterNode.add(new HostAndPort("127.0.0.1", 6386));


            pool = new JedisCluster(jedisClusterNode, 1000, 1000, 5, config);

//            pool = new JedisPool(config, "43.138.213.214", 6382);
//            pool = new JedisPool(config, "172.17.0.1", 6379);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得jedis对象
     */
    public static JedisCluster getJedis() {
        return pool;
    }

    /**
     * 归还jedis对象
     * @param jedis
     */
    private static void returnJedisOjbect(JedisCluster jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }


    public synchronized static Boolean SetString(String key, String value, Integer expireSecond) {
        JedisCluster jedis = null;
        try {
            jedis = RedisClient.getJedis();
            if (key != null && value != null && jedis != null) {
                String status = jedis.set(key, value);
                if (expireSecond != null) {
                    jedis.expire(key, expireSecond);
                }
                return status.equals("OK");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null){
                RedisClient.returnJedisOjbect(jedis);
            }
        }

        return false;
    }

    public synchronized static String GetString(String key) {
        JedisCluster jedis = null;
        try {
            jedis = RedisClient.getJedis();
            if (key != null && jedis != null) {
                return jedis.get(key);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisClient.returnJedisOjbect(jedis);
        }
        return null;
    }

    public synchronized static Boolean RemoveString(String key) {
        JedisCluster jedis = null;
        try {
            jedis = RedisClient.getJedis();
            if (key != null && jedis != null) {
                return jedis.del(key) > 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisClient.returnJedisOjbect(jedis);
        }
        return false;
    }

    public static void main(String[] args) {
//        for (int i = 0; i < 100; i++) {
//            System.err.println("第:" + i + "开始");
//            RedisClient.SetString(String.valueOf(i), "第:" + i, 20);
//            System.err.println("第:" + i + "结束");
//        }

//        RedisClient.getJedis().set("James", "Bond");

        boolean value = RedisClient.SetString("ran","shizhang",2000);
//        String value1 = RedisClient.GetString("k1");
//        RedisClient.getJedis().del("key0");

        System.out.println(value);
    }
}

 

可能出现的异常

1、DENIED Redis is running in protected mode because protected mode is enabled...

解决方法:redis.conf默认禁止外网访问,修改”protected-mode yes”为“protected-mode no”

2、No more cluster attempts left.

解决方法:redis设置集群时,服务器没有配置开启集群总线端口(redis端口+10000),如果redis-cli端口有7000-7005,则集群总线端口为17000-17005,服务器7000-70005、17000-17005端口都要打开

3、No reachable node in cluster

解决方法:查看redis.conf 的 "bind xxxxxxx" 是否限制了IP访问,注销bind则可以任意IP访问服务器Redis

--冉师长

标签:springboot,redis,RedisClient,jedis,哈希,new,static,null
From: https://www.cnblogs.com/ranshizhang/p/17113163.html

相关文章