一、不指定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. RSA 加密长度计算公式

    The length of data that can be encrypted using RSA is determined primarily by the size of the key yo ...

  2. python之开发规范

    命名规范 Python之父推荐的规范 Type Public Internal Modules lower_with_under _lower_with_under Packages lower_wi ...

  3. sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1091, "Can't DROP 'users_ibfk_1'; check that column/key exists") [SQL: ALTER TABLE users DROP FOREIGN KEY users_ibfk_1]

    flask 迁移数据库报错 报错: sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1091, "Can't DROP ...

  4. React源码深度解析视频 某课网(完整版)

    <ignore_js_op> [课程介绍]:        React毫无疑问是前端界主流的框架,而框架本身就是热点.课程以讲解React实现原理为主,并在实现过程中讲解这么做的原因,带来 ...

  5. 数据库 (二):MySQL密码策略与用户管理

    为了加强安全性,MySQL5.7为root用户随机生成了一个密码可通过# grep "password" /var/log/mysqld.log 命令获取MySQL的临时密码用该密 ...

  6. linux下ifconfig只剩下lo的解决方法

    运行以下命令,删除旧的网络配置,重新让networkmanager自动配置即可 sudo service network-manager stop sudo rm /var/lib/NetworkMa ...

  7. Delphi 程序调试

  8. 分布式特点理解-Zookeeper准备

    分布式环境特点 分布性 地域,区域,机房,服务器不同导致分布性 并发性 程序运行中,并发性操作很常见,比如同一个分布式系统中的多个节点,同时访问一个共享资源(数据库,分布式存储) 无序性 进程之间的消 ...

  9. Flight HDU - 3499 (分层最短路)

    Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to ...

  10. backtop返回页面顶部jquery代码

    <div id="toTop" style="width:30px;height:110px;border:1px solid #74B9DC; border-ra ...