springboot使用redis集群整合redisTemplate

蔚落 2022-04-08 09:26 563阅读 0赞

找了好久都没有找到相关的资料,所以就根据xml配置redis集群的方式,自己对比源码搞了一下,成功整合。分享一下
直接贴出代码:

代码中的参数都是通过
@Value注解进行注入

  1. import org.apache.commons.lang3.StringUtils;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.cache.annotation.CachingConfigurerSupport;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.data.redis.cache.RedisCacheManager;
  8. import org.springframework.data.redis.connection.RedisClusterConfiguration;
  9. import org.springframework.data.redis.connection.RedisNode;
  10. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  11. import org.springframework.data.redis.core.RedisTemplate;
  12. import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
  13. import org.springframework.data.redis.serializer.StringRedisSerializer;
  14. import org.springframework.web.client.RestTemplate;
  15. import java.util.HashSet;
  16. import java.util.Set;
  17. /**
  18. 使用redis集群或者单机初始化redisTemplate
  19. */
  20. @Configuration
  21. public class RedisConfig extends CachingConfigurerSupport {
  22. @Value("${redis.model:single}")
  23. private String model;
  24. @Value("${redis.cluster:127.0.0.1:6379}")
  25. private String clusterNodes;
  26. @Value("${redis.single:127.0.0.1:6379}")
  27. private String singNode;
  28. @Value("${redis.password:111111}")
  29. private String password;
  30. /**
  31. * Redis集群的配置
  32. *
  33. * @return RedisClusterConfiguration
  34. * @throws
  35. */
  36. @Bean
  37. public RedisClusterConfiguration redisClusterConfiguration() {
  38. RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
  39. String[] serverArray = clusterNodes.split(",");
  40. Set<RedisNode> nodes = new HashSet<RedisNode>();
  41. for (String ipPort : serverArray) {
  42. String[] ipAndPort = ipPort.split(":");
  43. nodes.add(new RedisNode(ipAndPort[0].trim(), Integer.valueOf(ipAndPort[1])));
  44. }
  45. redisClusterConfiguration.setClusterNodes(nodes);
  46. return redisClusterConfiguration;
  47. }
  48. /**
  49. * @param
  50. * @return
  51. * @Description:redis连接工厂类
  52. * @date 2018/10/25 19:45
  53. */
  54. @Bean
  55. public JedisConnectionFactory jedisConnectionFactory() {
  56. JedisConnectionFactory factory = null;
  57. //集群模式
  58. if (StringUtils.equals("cluster", model)) {
  59. factory = new JedisConnectionFactory(redisClusterConfiguration());
  60. } else {
  61. String[] ipAndPort = singNode.split(":");
  62. factory = new JedisConnectionFactory();
  63. factory.setHostName(ipAndPort[0]);
  64. factory.setPassword(password);
  65. factory.setPort(Integer.parseInt(ipAndPort[1]));
  66. }
  67. factory.setUsePool(true);
  68. return factory;
  69. }
  70. /**
  71. * 实例化 RedisTemplate 对象
  72. *
  73. * @return
  74. */
  75. @Bean
  76. public RedisTemplate<String, Object> redisTemplate() {
  77. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  78. initDomainRedisTemplate(redisTemplate);
  79. return redisTemplate;
  80. }
  81. /**
  82. * 设置数据存入 redis 的序列化方式,并开启事务
  83. */
  84. private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
  85. redisTemplate.setKeySerializer(new StringRedisSerializer());
  86. redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
  87. redisTemplate.setEnableTransactionSupport(false);
  88. redisTemplate.setConnectionFactory(jedisConnectionFactory());
  89. }
  90. /**
  91. * @param
  92. * @return
  93. * @Description:redis缓存管理器
  94. * @date 2018/10/25 21:18
  95. */
  96. @Override
  97. @Bean
  98. public RedisCacheManager cacheManager() {
  99. RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
  100. redisCacheManager.setUsePrefix(true);
  101. return redisCacheManager;
  102. }
  103. @Bean
  104. public RestTemplate restTemplate() {
  105. return new RestTemplate();
  106. }
  107. }

redis配置文件配置如下:

  1. ##redis集群
  2. spring.redis.cluster.nodes=168.181.211.220:6379,168.181.211.221:6380,168.181.211.222:6379,168.181.211.223:6380,168.181.211.225:6379,168.181.211.228:6380
  3. spring.redis.cluster.max-redirects=6
  4. #客户端超时时间单位是毫秒 默认是2000
  5. redis.timeout=10000
  6. #最大空闲数
  7. redis.maxIdle=300
  8. #控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性
  9. redis.maxTotal=1000
  10. #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
  11. redis.maxWaitMillis=1000
  12. #连接的最小空闲时间 默认1800000毫秒(30分钟)
  13. redis.minEvictableIdleTimeMillis=300000
  14. #每次释放连接的最大数目,默认3
  15. redis.numTestsPerEvictionRun=1024
  16. #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
  17. redis.timeBetweenEvictionRunsMillis=30000
  18. #是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
  19. redis.testOnBorrow=true
  20. #在空闲时检查有效性, 默认false
  21. redis.testWhileIdle=true

在需要使用redisTemplate的地方直接注入即可使用:

  1. @Autowired
  2. RedisTemplate<String, Object> redisTemplate; //泛型可以根据自己的需求进行定义

发表评论

表情:
评论列表 (有 0 条评论,563人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot整合redis

    一:缓存的应用场景 1:什么是缓存? 在互联网场景下,尤其 2C 端大流量场景下,需要将一些经常展现和不会频繁变更的数据,存放在存取速率更快的地方。缓存就是一个存储器,在技

    相关 springboot整合redis

    一、redis集群原理 redis集群中的所有节点彼此互联,节点内部采用二进制协议优化传输速度和带宽,每个节点都可以与Java客户端连接。redis集群的数据分配采用哈希