springboot连接阿里云服务器redis集群搭建详细介绍 | 避坑 | 绝对成功(全网最详细搭建最简洁)

springboot连接阿里云redis集群详细介绍(全网首发)

经过我一整天的排查与努力,终于解决了springboot与阿里云redis集群连接的各种问题,这里将详细的介绍如何连接!!

为了操作方便,诸位可以将redis中的应用程序以软链接的方式添加到舒服的位置,这样以后就不用切换目录啦,或者添加为环境变量

1.redis配置文件

redis安装完毕后,会有默认的配置文件,我们需要cp该配置文件。

找到redis的安装目录/redis-7.0.0/redis.conf

  1. 复制redis.conf文件
cp /opt/redis-7.0.0/redis.conf /homr/aguo/redis/conf/

注意:这里需设置redis.conf的一些初始化属性,修改如下

daemonize yes #开启后台启动
# bind=127.0.0.1 注释掉ip绑定的限制
protected-mode no #关闭保护模式

cluster-announce-ip <公网ip>

requirepass <密码>

大坑:在阿里云当中,需要设置cluster-announce-ip <公网ip>

即广播IP,否则springboot连接redis集群时候,上面值默认阿里云的内网IP,这样会导致无法连接。

  1. 自定义redis6379.conf文件

    这里以6379端口为例,其他的端口号,复制该文件 替换掉端口号即可、集群主结点最少3个,所以文件最少要3个(即没有从结点),有需要的可以复制多三分出来,作为3个从结点

    第一句表示引入redis.conf的基本属性

include /home/aguo/redis/conf/redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb
# 打开集群模式
cluster-enabled yes 
# 设置节点配置文件名称,需要更改
cluster-config-file nodes-6379.conf 
# 设置节点失联事件,超过该时间(ms),集群自动进行主从切换
cluster-node-timeout 15000 
  1. 然后我们copy两份,或者六份(3主3从时)
cp ./redis_6379.conf ./redis_6380.conf
cp ./redis_6379.conf ./redis_6381.conf
  1. vi打开复制出来的配置文件中,修改与文件名对应的端口号,即所有的6379均修改为6380

    :%s/6379/6380 这里可以快速查找并替换!冒号别漏了。

  2. 最后得到以下配置文件(如果3主3从则是7个文件)
    (redis_6382.conf忽略,我不小心弄多余的)
    image-20220518161019272

2. 构建集群系统

这一步就比较难,看着来操作,注意坑

1.启动所有的redis服务

redis-server ./conf/redis_6379.conf
redis-server ./conf/redis_6380.conf
redis-server ./conf/redis_6381.conf
...(如果有从机也要全部启动)
  1. 创建集群(这里非常多坑)
  • 需要开启所有结点防火墙端口,是所有主从结点都要开!

    否则会出现命令阻塞:

    [root@aguozi ~]# redis-cli  --cluster create ....
    
    [回车后卡住,没有任何反应]
    
  • 需要开启总线端口的防火墙,即端口号+10000,【所有】主从结点都要开启!!

    【官方解释】The cluster port is the port that the cluster bus will listen for inbound connections on. When set to the default value, 0, it will be bound to the command port + 10000. Setting this value requires you to specify the cluster bus port when executing cluster meet.

    集群端口是集群总线将侦听入站连接的端口。 当设置为默认值0时,它将绑定到命令端口+ 10000。 设置此值需要在执行集群满足时指定集群总线端口。

    否则出现[waiting for the cluster to join]:

    image-20220518165444754

  • 命令中的参数IP一定要是公网IP

    否则Java会报错连接失败,连接不存在!

  • Using a password with ‘-a’ or ‘-u’ option on the command line interface may 密码安全警告

    在执行的命令后面追加 --no-auth-warning 表示禁止密码安全的警告

  • 报错:[ERR] Node 127.0.0.1:6381 is not empty… 结点不为空

    表示该结点已经存在,也就说该结点已经被其他集群配置过了,或者说无法作为新的结点接入集群中

    办法:

    ​ kill掉该结点

    ​ 删除该结点的所有.aof、rdb本地存储文件,

    ​ 删除nodes-6381.conf配置文件

    ​ (一般可以解决问题,还不行)

    ​ redis-cli –p <对应端口> 然后flushdb

    执行命令(自己去掉换行):

    redis-cli 
    --cluster create #集群参数--创建
    --cluster-replicas 1 #一个主节点对应1个从结点
    120.1.1.1:6379 #这里一定要是公网ip
    120.1.1.1:6380
    120.1.1.1:6381 
    ... ... # 所有的主从结点都要写到这里来!
    -a <密码>
    
    --no-auth-warning #取消密码在命令行输入的警告
    

输入yes,即可。如果卡住,重新确认下总线端口防火墙有没设置好。

3.Springboot连接

  1. 依赖如下

    <!--redis启动器-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!--用来配置lettuce的redis连接池-->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>
    
  2. 配置redis

    有两种方式,一种是配置类,一种是application.yaml.根据自己需要配置

​ 配置类(不用继承任何超类)

@Bean
public RedisClusterConfiguration redisClusterConfig(){
    
    
    RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
    redisClusterConfiguration.clusterNode("120.76.1.15",6379)
        .clusterNode("120.76.1.15",6380)
        .clusterNode("120.76.1.15",6381);
    redisClusterConfiguration.setMaxRedirects(3);
    redisClusterConfiguration.setPassword("redis密码");
    return redisClusterConfiguration;
}

application.yaml

spring: 
  redis:
   #因为是集群 所以不用设置host、port等单机参数
    lettuce:
      pool: #使用连接池
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0
    cluster:
      nodes:
          - 120.76.1.15:6379
          - 120.76.1.15:6380
          - 120.76.1.15:6381
      max-redirects: 3
    password: <redis密码>
  1. 测试
@Autowired
private RedisTemplate redisTemplate;

...

猜你喜欢

转载自blog.csdn.net/qq15347747/article/details/124847316