Jedis支持哨兵模式下认证

摘要:
Redis高可用性哨兵模式还将添加authentication#,以在Redis哨兵模式下检查主节点命令$SENTINELget-master-addr by namemymaster。因此,当我们在Java中连接Redis时,需要配置两个密码;1.连接哨兵的身份验证密码:sentinelPassword 2。哨兵返回的主节点密码:redisPassword对于以上问题,我们选择Jed作为连接Redis的数据库

Redis高可用哨兵模式,为了安全,也会添加认证

# 检查Redis哨兵模式下master节点命令
$ SENTINEL get-master-addr-by-name mymaster

所以,我们在Java中连接Redis时,就需要配置两种密码;

1. 连接哨兵的认证密码: sentinelPassword

2. 哨兵返回的master节点密码:redisPassword

针对以上问题,我们选择连接Redis的库为Jedis: redis/jedis: A blazingly small and sane redis java client (github.com)

代码如下:

    private static void createJedisPool() {
        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);
        String masterName = "mymaster";
        Set<String> sentinels = new HashSet<String>();
        sentinels.add(new HostAndPort("10.0.0.13", 26379).toString());
        sentinels.add(new HostAndPort("10.0.0.14", 26379).toString());
        sentinels.add(new HostAndPort("10.0.0.15", 26379).toString());
        String password = "redisPassword"; // 连接redis时master节点认证密码
        String sentinelPassword = "sentinelPassword";  // 连接sentinel时的认证密码

        // sentinel no auth
        // pool = new JedisSentinelPool(masterName, sentinels, config, TIMEOUT, password, 0);

// sentinel need auth pool = new JedisSentinelPool(masterName, sentinels, password, sentinelPassword); }

但是怎么调试都不行,看了代码实现

    public JedisSentinelPool(String masterName, Set<String> sentinels, GenericObjectPoolConfig<Jedis> poolConfig, int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, int database, String clientName, int sentinelConnectionTimeout, int sentinelSoTimeout, String sentinelUser, String sentinelPassword, String sentinelClientName) {
        this(masterName, sentinels, poolConfig, new JedisFactory(connectionTimeout, soTimeout, infiniteSoTimeout, user, password, database, clientName));
        this.connectionTimeout = connectionTimeout;
        this.soTimeout = soTimeout;
        this.infiniteSoTimeout = infiniteSoTimeout;
        this.user = user;
        this.password = password;
        this.database = database;
        this.clientName = clientName;
        this.sentinelConnectionTimeout = sentinelConnectionTimeout;
        this.sentinelSoTimeout = sentinelSoTimeout;
        this.sentinelUser = sentinelUser;
        this.sentinelPassword = sentinelPassword;
        this.sentinelClientName = sentinelClientName;
    }

    public JedisSentinelPool(String masterName, Set<String> sentinels, GenericObjectPoolConfig<Jedis> poolConfig, JedisFactory factory) {
        this(masterName, parseHostAndPorts(sentinels), poolConfig, (JedisFactory)factory, DefaultJedisClientConfig.builder().build());
    }

    public JedisSentinelPool(String masterName, Set<HostAndPort> sentinels, GenericObjectPoolConfig<Jedis> poolConfig, JedisClientConfig masteClientConfig, JedisClientConfig sentinelClientConfig) {
        this(masterName, sentinels, poolConfig, new JedisFactory(masteClientConfig), sentinelClientConfig);
    }

    public JedisSentinelPool(String masterName, Set<HostAndPort> sentinels, GenericObjectPoolConfig<Jedis> poolConfig, JedisFactory factory, JedisClientConfig sentinelClientConfig) {
        super(poolConfig, factory);
        this.masterListeners = new HashSet();
        this.initPoolLock = new Object();
        this.poolConfig = poolConfig;
        this.factory = factory;
        this.sentinelClientConfig = sentinelClientConfig;
        HostAndPort master = this.initSentinels(sentinels, masterName);
        this.initMaster(master);
    }

  发现在 this.initSentinels(sentinels, masterName); 时,确实没有将哨兵的密码传入;所以我们就到官方仓库去查看;发现官方的里面是有传入哨兵密码信息的;

所以,我们就更新了jedis的依赖版本;【jedis 3.6.0 是没有解决的,只有3.6.*是否解决没有测试,3.7.0是解决了】

        <!-- jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.7.0</version>
        </dependency>

    解决之后的代码如下:

    public JedisSentinelPool(String masterName, Set<String> sentinels, GenericObjectPoolConfig<Jedis> poolConfig, int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, int database, String clientName, int sentinelConnectionTimeout, int sentinelSoTimeout, String sentinelUser, String sentinelPassword, String sentinelClientName) {
        this(masterName, parseHostAndPorts(sentinels), poolConfig, (JedisClientConfig)DefaultJedisClientConfig.builder().connectionTimeoutMillis(connectionTimeout).socketTimeoutMillis(soTimeout).blockingSocketTimeoutMillis(infiniteSoTimeout).user(user).password(password).database(database).clientName(clientName).build(), DefaultJedisClientConfig.builder().connectionTimeoutMillis(sentinelConnectionTimeout).socketTimeoutMillis(sentinelSoTimeout).user(sentinelUser).password(sentinelPassword).clientName(sentinelClientName).build());
        this.connectionTimeout = connectionTimeout;
        this.soTimeout = soTimeout;
        this.infiniteSoTimeout = infiniteSoTimeout;
        this.user = user;
        this.password = password;
        this.database = database;
        this.clientName = clientName;
        this.sentinelConnectionTimeout = sentinelConnectionTimeout;
        this.sentinelSoTimeout = sentinelSoTimeout;
        this.sentinelUser = sentinelUser;
        this.sentinelPassword = sentinelPassword;
        this.sentinelClientName = sentinelClientName;
    }

    public JedisSentinelPool(String masterName, Set<String> sentinels, GenericObjectPoolConfig<Jedis> poolConfig, JedisFactory factory) {
        this(masterName, parseHostAndPorts(sentinels), poolConfig, (JedisFactory)factory, DefaultJedisClientConfig.builder().build());
    }

    public JedisSentinelPool(String masterName, Set<HostAndPort> sentinels, JedisClientConfig masteClientConfig, JedisClientConfig sentinelClientConfig) {
        this(masterName, sentinels, new GenericObjectPoolConfig(), masteClientConfig, sentinelClientConfig);
    }

    public JedisSentinelPool(String masterName, Set<HostAndPort> sentinels, GenericObjectPoolConfig<Jedis> poolConfig, JedisClientConfig masteClientConfig, JedisClientConfig sentinelClientConfig) {
        this(masterName, sentinels, poolConfig, new JedisFactory(masteClientConfig), sentinelClientConfig);
    }

    public JedisSentinelPool(String masterName, Set<HostAndPort> sentinels, GenericObjectPoolConfig<Jedis> poolConfig, JedisFactory factory, JedisClientConfig sentinelClientConfig) {
        super(poolConfig, factory);
        this.masterListeners = new HashSet();
        this.initPoolLock = new Object();
        this.poolConfig = poolConfig;
        this.factory = factory;
        this.sentinelClientConfig = sentinelClientConfig;
        HostAndPort master = this.initSentinels(sentinels, masterName);
        this.initMaster(master);
    }

发现其实只要更新依赖包到最新就可以解决,囧

免责声明:文章转载自《Jedis支持哨兵模式下认证》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇利用js获取图片尺寸与图片大小(高度与宽度)ICMP报文类型下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

uniapp 中出现 wx.config is not a function

最近使用uniapp做了一个微信公众号网页 调用微信jssdk 报错为wx.config is not a function 我也引用了http://res2.wx.qq.com/open/js/jweixin-1.6.0.js 原因是uniapp里面内置了一个名为wx的全局变量,结果是肯定的,wx变量都被重写了,wx.config肯定也是undefi...

Yapi部署说明

1.环境搭建 确保 node 版本=> 7.6,请运行 node -v 查看版本号 确保 mongodb 版本 => 2.6,请运行 mongo --version 查看版本号 确保安装了 npm, 运行 npm -v 查看版本号(高点版本node一般自带) 确保安装了 git,运行 git --version 查看版本号(确保git版本最新,...

Spring Boot 2.4 配置文件将加载机制大变化

Spring Boot 2.4.0.M2 刚刚发布,它对 application.properties 和 application.yml 文件的加载方式进行重构。如果应用程序仅使用单个 application.properties 或 application.yml 作为配置文件,那么可能感受不到任何区别。但是如果您的应用程序使用更复杂的配置(例如,Sp...

关于ASP.NET中独立页面设置身份认证等问题

大家都知道通过以下的方法实现对所有页面的身份认证要求:<authentication mode="Forms"><forms name=".OnLineWork" loginUrl="logoin.aspx" protection="All" timeout="60" /></authentication><aut...

HTML中data自定义属性的使用和插件应用

大家可能会经常看到一些HTML里都带有data属性,这些都是HTML5的自定义属性,可以做很多事情,直接调用JS十分方便,虽然是HTML5的属性,但好在jQuery通用的,所以基本在所有浏览器里都是可以正常使用的,包括低版本的IE。下面为大家简单介绍一下使用方法: 1、简单使用 <div id="widget"data-text="123456"&...

git clone error:RPC failed; curl 18 transfer closed with outstanding read data remaining

git clone时报RPC failed; curl 18 transfer closed with outstanding read data remaining 错误 error: RPC failed; curl 18 transfer closed withoutstanding read data remaining fatal: The re...