Springboot 项目 集成redis集群的踩坑(重点踩坑)

编程入门 行业动态 更新时间:2024-10-26 22:22:28

Springboot 项目 集成redis<a href=https://www.elefans.com/category/jswz/34/1771240.html style=集群的踩坑(重点踩坑)"/>

Springboot 项目 集成redis集群的踩坑(重点踩坑)

redis安装

redis安装6.2或以上的

集群搭建

搭建集群最少要运行6个redis服务,将redis.conf复制6个份,将里面的配置修改,

这个文件名要都不一样 (cluster-config-file nodes.conf ,cluster-config-file nodes-1.conf),

主库设置了密码的话从库 masterauth ****  这个要输入主库密码

#关闭redis的保护模式  no 允许外网访问
protected-mode no
#开启集群模式
cluster-enabled yes
#集群的配置文件,注:集群的配置文件不需要我们生成,redis会自动生成   
cluster-config-file nodes-7001.conf
#集群超时时间
cluster-node-timeout 5000
#开始数据持久化
appendonly yes 
#该错误跟个属性基本没有关系,只是为了让redis在后台运行,不用打印很多日志 yes 打印
daemonize yes 

#配置集群映射的公网IP 
cluster-announce-ip 172.29.xxx.xxx
#配置集群映射的端口
cluster-announce-port 7001
#集群节点总线端口,节点之间互相通信,常规端口+1万
cluster-announce-bus-port 17001

bind 0.0.0.0

#主库密码  (从库配置)
masterauth zairui123

一定记得不要使用redis-trib.rb搭建 这个已经淘汰了,要使用redis-cli搭建(踩坑重点)

ip用私网 redis-cli --cluster create  172.29.144.202:7001  172.29.144.202:7002   172.29.144.202:7003  172.29.144.202:7004  172.29.144.202:7005  172.29.144.202:7006   --cluster-replicas 1 

项目连接redis

redis:timeout: 5000  # 连接超时时长(毫秒)password: zairuicluster:nodes: - ip:7001- ip:7002- ip:7003- ip:7004- ip:7005- ip:7006connectionTimeout: 6000soTimeout: 6000maxAttempts: 5database: 1jedis:pool:max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)max-wait: -1ms    # 连接池最大阻塞等待时间(使用负值表示没有限制)max-idle: 10      # 连接池中的最大空闲连接min-idle: 5       # 连接池中的最小空闲连接

然后增加一个配置文件

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

就可以测试调用了

更多推荐

Springboot 项目 集成redis集群的踩坑(重点踩坑)

本文发布于:2024-02-26 16:14:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1703123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:集群   重点   项目   Springboot   redis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!