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

sha*_*lea 7 java configuration spring-boot redis-cluster

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

这是我的 redis 配置类:

@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
public class RedisClusterProperties {
    List<String> nodes;
}
Run Code Online (Sandbox Code Playgroud)
@Configuration
public class RedisConfig {

@Autowired
RedisClusterProperties redisClusterProperties;

@Bean
public  RedisConnectionFactory connectionFactory(){
    return  new JedisConnectionFactory(
            new RedisClusterConfiguration(redisClusterProperties.getNodes()));
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来是我的 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
Run Code Online (Sandbox Code Playgroud)

接下来是我的测试班

@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));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

更新 redis.conf 以添加密码

那我加????到我的 java 项目中的 application.properties。

spring.redis.password=*****
Run Code Online (Sandbox Code Playgroud)

我验证redis它可以正常使用。 redis 正常图片

然后我开始我的项目,发现项目是错误的。

@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
public class RedisClusterProperties {
    List<String> nodes;
}
Run Code Online (Sandbox Code Playgroud)

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

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

Den*_*aev 5

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

@Bean
public  RedisConnectionFactory connectionFactory(){

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

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

    return new JedisConnectionFactory(clusterConf);
}
Run Code Online (Sandbox Code Playgroud)