我有一个带密码的简单redis服务器正在运行,我想从我的Spring Boot应用程序与其通信。我查看这里并发现有一个
spring.redis.password
,因此我的应用程序-local.yml(使用-Dspring.profiles.active=local
运行)看起来像...spring:
redis:
password: ...
但是当我运行时,会收到以下错误信息:
需要进行身份验证(NOAUTH Authentication required)
我漏掉了什么吗?我可以像这样通过node.js连接...
import redis from "redis";
const client = redis.createClient({
password: "..."
});
额外的代码
@Bean
LettuceConnectionFactory redisConnectionFactory(){
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
return new LettuceConnectionFactory(config);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(){
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
return template;
}
也尝试了...
@Bean
LettuceConnectionFactory redisConnectionFactory(){
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
if (redisPassword != null){
config.setPassword(redisPassword);
}
return new LettuceConnectionFactory(config);
}
虽然这样能够正常运行,但它似乎过于冗长,因为它只是一个标准属性。
RedisConnectionFactory
bean,让Spring为您创建一个。创建的RedisConnectionFactory
将使用来自application.yml的属性进行填充。 - nkrivenko