当前位置:   article > 正文

解决springboot2.x集成redis节点故障redisTemplate报错redis Command timed out_redis command timed out; nested exception is io.le

redis command timed out; nested exception is io.lettuce.core.rediscommandtim

pringboot2.x集成redis。redis节点故障,集群状态ok的情况下,程序使用redisTemplate操作redis一直报错:

Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 1 minute(s)

解决方案:捕获redisTemplate操作的异常,然后重新初始化redisTemplate的连接工厂connectionFactory

代码如下:

RedisService.java

  1. package com.harara;
  2. import com.harara.RedisConfig;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Primary;
  6. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.core.ValueOperations;
  9. import org.springframework.stereotype.Component;
  10. import java.io.Serializable;
  11. /**
  12. * @author: harara
  13. * @date: 2020-9-07 18:23
  14. * @description:
  15. * @version: 1.0
  16. */
  17. @Slf4j
  18. @Component
  19. public class RedisService {
  20. @Autowired
  21. private RedisTemplate redisTemplate;
  22. @Autowired
  23. private RedisConfig redisConfig;
  24. /**
  25. * 更新redisTemplate :处理集群宕机恢复后程序不恢复问题
  26. * 重新初始化redisTemplate的连接工厂connectionFactory
  27. */
  28. private RedisTemplate refreshRedisTemplate(){
  29. LettuceConnectionFactory connectionFactory = redisConfig.connectionFactory();
  30. connectionFactory.afterPropertiesSet();
  31. redisTemplate.setConnectionFactory(connectionFactory);
  32. return redisTemplate;
  33. }
  34. /**
  35. * @param key
  36. * @return
  37. */
  38. public Object get(final String key){
  39. ValueOperations<Serializable, Object> operations = null;
  40. try {
  41. operations= redisTemplate.opsForValue();
  42. return operations.get(key);
  43. }catch (Exception e){
  44. log.error("redis操作string get出现异常:{}",e.getMessage());
  45. operations = refreshRedisTemplate().opsForValue();
  46. return operations.get(key);
  47. }
  48. }
  49. }

RedisConfig.java

  1. package com.harara;
  2. import cn.hutool.core.util.StrUtil;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.core.annotation.Order;
  6. import org.springframework.core.env.MapPropertySource;
  7. import org.springframework.data.redis.connection.RedisClusterConfiguration;
  8. import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
  9. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. /**
  13. * redis 配置类
  14. * @author harara
  15. * @date 2020/9/7 11:34
  16. *
  17. */
  18. @Configuration
  19. @Order(value = 1)
  20. public class RedisConfig {
  21. //redis连接的database
  22. @Value("${spring.redis.database:0}")
  23. private int database;
  24. //集群节点配置
  25. @Value("${spring.redis.cluster.nodes:#{null}}")
  26. private String nodes;
  27. //集群最大连接转移数
  28. @Value("${spring.redis.cluster.max-redirects:3}")
  29. private int maxRedirects;
  30. //单节点情况下redis的ip
  31. @Value("${spring.redis.host:#{null}}")
  32. private String host;
  33. //单节点情况下redis的端口
  34. @Value("${spring.redis.port:#{null}}")
  35. private Integer port;
  36. //redis的连接密码
  37. @Value("${spring.redis.password:#{null}}")
  38. private String password;
  39. /**
  40. * 创建连接工厂LettuceConnectionFactory
  41. * @return
  42. */
  43. public LettuceConnectionFactory connectionFactory() {
  44. Map<String, Object> source = new HashMap<String, Object>();
  45. RedisClusterConfiguration redisClusterConfiguration;
  46. RedisStandaloneConfiguration redisStandaloneConfiguration;
  47. //集群模式
  48. if(nodes !=null){
  49. source.put("spring.redis.cluster.nodes", nodes);
  50. source.put("spring.redis.cluster.max-redirects", maxRedirects);
  51. redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
  52. if(!StrUtil.isEmpty(password)){
  53. redisClusterConfiguration.setPassword(password);
  54. }
  55. //创建连接工厂
  56. LettuceConnectionFactory lettuceConnectionFactory = new
  57. LettuceConnectionFactory(redisClusterConfiguration);
  58. return lettuceConnectionFactory;
  59. }else{
  60. //单机模式
  61. redisStandaloneConfiguration = new RedisStandaloneConfiguration(host,port);
  62. redisStandaloneConfiguration.setDatabase(database);
  63. if(!StrUtil.isEmpty(password)){
  64. redisStandaloneConfiguration.setPassword(password);
  65. }
  66. //创建连接工厂
  67. LettuceConnectionFactory lettuceConnectionFactory = new
  68. LettuceConnectionFactory(redisStandaloneConfiguration);
  69. return lettuceConnectionFactory;
  70. }
  71. }
  72. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/461719
推荐阅读
相关标签
  

闽ICP备14008679号