springboot几种开发模式_springboot2.x 整合redis集群的几种方式

一、不指定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 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 redisTemplate() {

RedisTemplate 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 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 redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {

RedisTemplate template = new RedisTemplate<>();

template.setKeySerializer(new StringRedisSerializer());

template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

template.setConnectionFactory(redisConnectionFactory);

return template;

}

}

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

@Autowired

private RedisTemplate redisTemplate;

---------------------

作者:qq_31256487

来源:CSDN

原文:https://blog.csdn.net/qq_31256487/article/details/83144088

版权声明:本文为博主原创文章,转载请附上博文链接!

以上就是springboot2.x 整合redis集群的几种方式的全部内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值