springboot2.x 整合redis集群的几种方式

mac2022-06-30  68

一、不指定redis连接池#系统默认连接池

yml配置文件: spring: redis: cluster: nodes: - 192.168.1.236:7001 - 192.168.1.236:7002 - 192.168.1.236:7003 - 192.168.1.244:7004 - 192.168.1.244:7005 - 192.168.1.244:7006 max-redirects: 3 # 获取失败 最大重定向次数 pool: max-active: 1000 # 连接池最大连接数(使用负值表示没有限制) max-idle: 10 # 连接池中的最大空闲连接 max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) min-idle: 5 # 连接池中的最小空闲连接 timeout: 6000 # 连接超时时长(毫秒)

 

这种方式 redisTemplate 可直接使用默认,

在使用的地方直接注入即可

@Autowired private RedisTemplate<String, Object> redisTemplate;

 

二、使用jedis连接池# 使用jedis连接池

yml配置文件: spring: redis: password: # 密码(默认为空) timeout: 6000ms # 连接超时时长(毫秒) cluster: nodes: - 192.168.1.236:7001 - 192.168.1.236:7002 - 192.168.1.236:7003 - 192.168.1.244:7004 - 192.168.1.244:7005 - 192.168.1.244:7006 jedis: pool: max-active: 1000 # 连接池最大连接数(使用负值表示没有限制) max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制) max-idle: 10 # 连接池中的最大空闲连接 min-idle: 5 # 连接池中的最小空闲连接

 

//连接池注入配置信息

@Configuration public class RedisConfig { @Autowired private RedisConnectionFactory factory; @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setConnectionFactory(factory); return redisTemplate; } }

 

在使用的地方直接注入即可

@Autowired private RedisTemplate<String, Object> redisTemplate;

 

三、使用lettuce连接池(推荐)# 使用lettuce连接池

yml配置文件:

spring: redis: timeout: 6000ms password: cluster: max-redirects: 3 # 获取失败 最大重定向次数 nodes: - 192.168.1.236:7001 - 192.168.1.236:7002 - 192.168.1.236:7003 - 192.168.1.244:7004 - 192.168.1.244:7005 - 192.168.1.244:7006 lettuce: pool: max-active: 1000 #连接池最大连接数(使用负值表示没有限制) max-idle: 10 # 连接池中的最大空闲连接 min-idle: 5 # 连接池中的最小空闲连接 max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)

 

//连接池注入配置信息

@Configuration @AutoConfigureAfter(RedisAutoConfiguration.class) public class RedisConfig { @Bean public RedisTemplate<String, Object> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); return template; } }

在使用的地方直接注入即可

@Autowired private RedisTemplate<String, Object> redisTemplate;

 

 

————————————————版权声明:本文为博主「qq_31256487」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/qq_31256487/article/details/83144088

转载于:https://www.cnblogs.com/powerwu/articles/11510637.html

最新回复(0)