如何使用spring boot 2.x为redis集群设置密码

Posted

技术标签:

【中文标题】如何使用spring boot 2.x为redis集群设置密码【英文标题】:How to set password for redis cluster with spring boot 2.x 【发布时间】:2019-01-01 00:08:51 【问题描述】:

“对不起,我的英语不太好”。

这是我的 redis 配置类:

@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
public class RedisClusterProperties 
    List<String> nodes;


@Configuration
public class RedisConfig 

@Autowired
RedisClusterProperties redisClusterProperties;

@Bean
public  RedisConnectionFactory connectionFactory()
    return  new JedisConnectionFactory(
            new RedisClusterConfiguration(redisClusterProperties.getNodes()));
    

接下来是我的 application.properties

spring.redis.cluster.nodes[0] = *.*.*.*:7001
spring.redis.cluster.nodes[1] = *.*.*.*:7002
spring.redis.cluster.nodes[2] = *.*.*.*:7003
spring.redis.cluster.nodes[3] = *.*.*.*:7004
spring.redis.cluster.nodes[4] = *.*.*.*:7005
spring.redis.cluster.nodes[5] = *.*.*.*:7006

接下来是我的测试课

@Component
@Slf4j
public class TestRedis 
    @Autowired
    RedisConnectionFactory connectionFactory;

    @Scheduled(cron = "0 26 18 ? * *")
    public void scheduler()
        RedisClusterConnection connection = 
connectionFactory.getClusterConnection();
    
connection.set("java_test".getBytes(),"java_test_value".getBytes());
        final byte[] bytes = connection.get("java_test".getBytes());
        System.out.println("print >> " + new String(bytes));
    

然后我从application.class开始我的项目。我可以从我的redis集群中获取/设置数据是正常的。

现在我为我的 redis 集群设置密码,如下所示: 我在 linux 中更新了我的每一个 redis.conf。

update redis.conf for add password

然后我将 ↓↓↓↓ 添加到我的 java 项目中的 application.properties 中。

spring.redis.password=*****

我验证redis可以正常使用。 redis normal picture

然后我启动我的项目,发现项目出错了。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testRedis': Unsatisfied dependency expressed through field 'connectionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [com/hizhu/crawler/brand/config/RedisConfig.class]: Invocation of init method failed; nested exception is redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1350) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:580) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at com.hizhu.crawler.brand.CrawlerBrandApplication.main(CrawlerBrandApplication.java:18) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [com/hizhu/crawler/brand/config/RedisConfig.class]: Invocation of init method failed; nested exception is redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1708) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:581) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 18 common frames omitted
Caused by: redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.
    at redis.clients.jedis.Protocol.processError(Protocol.java:127) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.Protocol.process(Protocol.java:161) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.Protocol.read(Protocol.java:215) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.Connection.getRawObjectMultiBulkReply(Connection.java:285) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:291) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.Jedis.clusterSlots(Jedis.java:3376) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.JedisClusterInfoCache.discoverClusterNodesAndSlots(JedisClusterInfoCache.java:54) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.JedisClusterConnectionHandler.initializeSlotsCache(JedisClusterConnectionHandler.java:39) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.JedisClusterConnectionHandler.<init>(JedisClusterConnectionHandler.java:17) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.JedisSlotBasedConnectionHandler.<init>(JedisSlotBasedConnectionHandler.java:20) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.BinaryJedisCluster.<init>(BinaryJedisCluster.java:48) ~[jedis-2.9.0.jar:na]
    at redis.clients.jedis.JedisCluster.<init>(JedisCluster.java:88) ~[jedis-2.9.0.jar:na]
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.createCluster(JedisConnectionFactory.java:423) ~[spring-data-redis-2.0.8.RELEASE.jar:2.0.8.RELEASE]
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.createCluster(JedisConnectionFactory.java:393) ~[spring-data-redis-2.0.8.RELEASE.jar:2.0.8.RELEASE]
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.afterPropertiesSet(JedisConnectionFactory.java:350) ~[spring-data-redis-2.0.8.RELEASE.jar:2.0.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1767) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 28 common frames omitted

我尝试从here 中找到如何部署的方法。但这是错误的。 我尝试参考弹簧数据here。但我找不到方法。

请问谁遇到过这个问题。 谢谢。

【问题讨论】:

【参考方案1】:

尝试在 RedisConfig 配置类中的 RedisClusterConfiguration 中设置密码:

@Bean
public  RedisConnectionFactory connectionFactory()

    RedisClusterConfiguration clusterConf = 
        new RedisClusterConfiguration(
            redisClusterProperties.getNodes());

    clusterConf.setPassword(RedisPassword.of("yourPassword"));

    return new JedisConnectionFactory(clusterConf);

【讨论】:

以上是关于如何使用spring boot 2.x为redis集群设置密码的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot 2.x 中使用redis

Spring Boot 2.x 集成 Redis 缓存

Spring Boot 2.x基础教程:使用Redis的发布订阅功能

Spring Boot 2.x基础教程:使用Redis的发布订阅功能

Spring Boot 2.x基础教程:使用集中式缓存Redis

Spring Boot 2.x(十四):整合Redis,看这一篇就够了