解决SpringBoot环境下Redis哨兵模式连接失败问题,“NOAUTH Authentication required”

文章描述了在项目中使用SpringBoot和RedisSentinel时遇到的连接问题,配置了sentinel密码但提示NOAUTHAuthenticationrequired。作者分享了解决方案,包括全局配置和处理密码问题。
摘要由CSDN通过智能技术生成

io.lettuce.core.RedisCommandException Exception:“NOAUTH Authentication required”

在某行工作,项目上线代码,uat环境无异常,上到pp环境有问题,报redis连接不上;

观察配置,发觉是apollo的配置是哨兵模式,有个哨兵密码。spring2.2.6RELEASE版本问题。于是写了全局配置,读取配置中的sentinel.password

package com.yupi.springbootinit.config;

import cn.hutool.core.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Configuration
public class RedisConfig {

    // 配置sentinel密码不生效,提示NOAUTH Authentication required。
//https://github.com/lettuce-io/lettuce-core/issues/1543
    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration(RedisProperties properties , @Value("${spring.redis.sentinel.password}") String sentinelPassword){
        RedisProperties.Sentinel sentinelProperties = properties.getSentinel();

        if (sentinelProperties != null) {
            RedisSentinelConfiguration config = new RedisSentinelConfiguration();
            config.master(sentinelProperties.getMaster());
            config.setSentinels(createSentinels(sentinelProperties));

            if (properties.getPassword() != null){
                config.setPassword(RedisPassword.of(properties.getPassword()));
            }
            if (sentinelPassword != null) {
            config.setSentinelPassword(RedisPassword.of(sentinelPassword));
            }
            config.setDatabase(properties.getDatabase());
            return config;
        }
        return null;
    }
        private List<RedisNode> createSentinels(RedisProperties.Sentinel sentinel) {
            List<RedisNode> nodes = new ArrayList<>();
            for (String node : sentinel.getNodes()) {
                String[] split = StringUtils.split(node, ":");
                Assert.notNull(split,"Must be defined as 'host:port'");
                Assert.state(split.length==2,"Must be defined as 'host:port'");
                nodes.add(new RedisNode(split[0],Integer.parseInt(split[1])));
            }
           return nodes;
        }

}

  • 11
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot可以集成Redis哨兵模式来提高Redis的高可用性和稳定性。引用中提到了搭建Redis主从环境和Nginx代理Redis哨兵模式的步骤。在环境介绍中,使用了Spring Boot 2.5.3、Spring Boot Data Redis 2.5.3以及Lettuce 6.1.4.RELEASE和commons-pool2 2.2.9.0等版本。在application.yml配置文件中,需要进行相关的配置,以实现Redis哨兵模式。在pom.xml文件中,需要引入一些基本的依赖,包括spring-boot-starter-web、spring-boot-starter-data-redis和fastjson等。这些依赖和配置将帮助我们在Spring Boot中使用Redis哨兵模式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [spring boot集成redis哨兵模式(一主二从)](https://blog.csdn.net/lhp3000/article/details/119699196)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot+Redis哨兵模式](https://blog.csdn.net/wl_Honest/article/details/124171062)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值