记一个RedisConfig坑
springboot整合redis,RedisConfig配置lettuceConnectionFactory时遇到的一个坑。
配置文件:
spring: redis: host: 127.0.0.1 port: 6379 password: 123456
RedisConfig:
@Configuration public class RedisConfig { @Bean public LettuceConnectionFactory redisConnectionFactory() { LettuceConnectionFactory factory = new LettuceConnectionFactory(); return factory; } @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } }
连接URI和密码正确且Redis服务正常运行,但是调用 redisTemplate 时报错:Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: NOAUTH Authentication required.
百思不得其姐,遂在RedisConfig手动将密码set进去,则可正常运行。
@Bean public LettuceConnectionFactory redisConnectionFactory() { LettuceConnectionFactory factory = new LettuceConnectionFactory(); factory.setPassword("123456"); return factory; }
用自动注入的方式注入LettuceConnectionFactory也可以正常运行。
// 替代上面的bean @Resource private LettuceConnectionFactory lettuceConnectionFactory;
标签:RedisConfig,redisConnectionFactory,factory,LettuceConnectionFactory,一个,new,redis From: https://www.cnblogs.com/ZDY-XJ/p/17837882.html