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

2 篇文章 0 订阅
1 篇文章 0 订阅

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: zairui
    cluster:
      nodes: 
        - ip:7001
        - ip:7002
        - ip:7003
        - ip:7004
        - ip:7005
        - ip:7006
      connectionTimeout: 6000
      soTimeout: 6000
      maxAttempts: 5
    database: 1
    jedis:
      pool:
        max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)
        max-wait: -1ms    # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 10      # 连接池中的最大空闲连接
        min-idle: 5       # 连接池中的最小空闲连接

然后增加一个配置文件

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {

    @Bean
    public 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;
    }
}

就可以测试调用了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值