一、不指定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
来源:CSDN
原文:https://blog.csdn.net/qq_31256487/article/details/83144088
版权声明:本文为博主原创文章,转载请附上博文链接!

springboot2.x 整合redis集群的几种方式的更多相关文章

  1. SpringBoot2.0 整合 Redis集群 ,实现消息队列场景

    本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...

  2. redis集群的三种方式

    Redis三种集群方式:主从复制,哨兵模式,Cluster集群. 主从复制 基本原理 当新建立一个从服务器时,从服务器将向主服务器发送SYNC命令,接收到SYNC命令后的主服务器会进行一次BGSAVE ...

  3. 测试redis集群的两种方式:分片和哨兵

    import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; i ...

  4. Springboot2.x集成Redis集群模式

    Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...

  5. SpringBoot整合Redis集群

    一.环境搭建 Redis集群环境搭建:https://www.cnblogs.com/zwcry/p/9174233.html 二.Spring整合Redis集群 1.pom.xml <proj ...

  6. (七)整合 Redis集群 ,实现消息队列场景

    整合 Redis集群 ,实现消息队列场景 1.Redis集群简介 1.1 RedisCluster概念 2.SpringBoot整合Redis集群 2.1 核心依赖 2.2 核心配置 2.3 参数渲染 ...

  7. 05.haproxy+mysql负载均衡 整合 redis集群+ssm

    本篇重点讲解haproxy+mysql负载均衡,搭建完成后与之前搭建的redis+ssm进行整合 (注:这里用到了两台mysql数据库,分别安装两台虚拟机上,已经成功实现主主复制,如果有需要,请查看我 ...

  8. Springboot2.0访问Redis集群

    Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作高性能的key-value数据库.缓存和消息中间件,掌握它是程序员的必备技能,下面是一个springboot访问redis的 ...

  9. Redis(七)-- SpringMVC整合Redis集群

    1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

随机推荐

  1. 动态表和C++ vector

    动态表和C++ vector 最近课上刚刚学了可以根据表中元素的插入和删除动态调整表大小的动态表(dynamic table),就想看一下它有什么实际的应用,第一个想起来的就是C++的vector,直 ...

  2. pytorch中torch.narrow()函数

    torch.narrow(input, dim, start, length) → Tensor Returns a new tensor that is a narrowed version of  ...

  3. pytorch中的torch.repeat()函数与numpy.tile()

    repeat(*sizes) → Tensor Repeats this tensor along the specified dimensions. Unlike expand(), this fu ...

  4. golang利用beego框架orm操作mysql

    GO引入orm框架操作mysql 在beego框架中引入orm操作mysql需要进行的步骤: 第一步:导入orm框架依赖,导入mysql数据库的驱动依赖 import ( "github.c ...

  5. CSS-百分百布局

    1.照片随着大小变化: 这里面重点就是每个包裹盒子是25%,图片是100%显示: <div class="box2"> <p> //这里都是4个: < ...

  6. 使用HBuilder创建图表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 多线程编程-- part5.1 互斥锁ReentrantLock

    ReentrantLock简介 Reentrantlock是一个可重入的互斥锁,又被称为独占锁. Reentrantlock:分为公平锁和非公平锁,它们的区别体现在获取锁的机制上是否公平.“锁”是为了 ...

  8. monkey 进阶使用手册,monkey随机测试后怎么定位问题

    首先我们知道使用monkey后,我们可以查看三种类型的日志,一种是安卓内核日志,一种是安卓系统自己的日志,还有一种是monkey日志. 当我们使用monkey进行随机测试时,如何才知道我们这次随机测试 ...

  9. Delphi 有类型文件

  10. Little Tricks

    一直都计划好好学算法,一直都计划好好看书刷题,却几乎从来没更新过(算法)博客,几乎从来没有花苦功夫学过. 糜烂的四月,最后一天,更新一些自己看到的小 trick 吧,以后一定要多多更新算法博客. 1. ...