CentOS 单机安装Redis集群

一、下载最新版Redis安装包解压到当前目录

cd /home
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
tar -zxvf redis-6.2.6.tar.gz

二、将解压好的文件夹移动到 /usr/local 路径下

mv redis-6.2.6 /usr/local/

三、进入 /us/local/redis-6.2.6 路径,执行以下命令进行安装,安装完成可以在

/usr/local/redis-6.2.6/src 下看到 redis-server、 redis-cli工具,也可以在安装时通过PREFIX参数指定安装目录:make install PREFIX=/usr/local/redis,这样会把redis-server、 redis-cli工具复制到对应的目录中。

cd /usr/local/redis-6.2.6
make
make install

四、单机模式

单机模式下,只要修改一下redis.conf配置文件,修改对应的ip地址和端口,将daemonize的值改为yes。指定配置文件启动。

cd /usr/local/redis-6.2.6/src
./redis-server ../redis.conf

使用redis-cli客户端连接redis

cd /usr/local/redis-6.2.6/src
./redis-cli -p 6379
./redis-cli shudown

五、单机多实例集群模式,通过多个端口在一台机器上启动多个redis实例,然后创建redis集群

1、创建集群节点配置

cd /usr/local
mkdir redis-cluster
cd redis-cluster
mkdir 6374 6375 6376 6377 6378 6379
cp /usr/local/redis-6.2.6/redis.conf /usr/local/redis-cluster/6374
mv redis.conf redis-6374.conf
cp /usr/local/redis-6.2.6/redis.conf /usr/local/redis-cluster/6375
mv redis.conf redis-6375.conf
cp /usr/local/redis-6.2.6/redis.conf /usr/local/redis-cluster/6376
mv redis.conf redis-6376.conf
cp /usr/local/redis-6.2.6/redis.conf /usr/local/redis-cluster/6377
mv redis.conf redis-6377.conf 
cp /usr/local/redis-6.2.6/redis.conf /usr/local/redis-cluster/6378
mv redis.conf redis-6378.conf 
cp /usr/local/redis-6.2.6/redis.conf /usr/local/redis-cluster/6379
mv redis.conf redis-6379.conf 

2、修改每一个集群节点的配置文件,主要修改点如下:

注意:cluster-config-file配置的文件将在参数dir配置的路径下自动生成,与redis-6374.conf不同

port 6374
daemonize no
pidfile /var/run/redis_6374.pid
logfile /usr/local/redis-cluster/6374/redis-6374.log
dir /usr/local/redis-cluster/6374/
masterauth test-pass
masteruser test
requirepass test-pass
appendonly yes
cluster-enabled yes
cluster-config-file nodes-6374.conf
cluster-node-timeout 15000

 3、启动每个redis节点

cd /usr/local/redis-6.2.6/src
./redis-server /usr/local/redis-cluster/6374/redis-6374.conf 
./redis-server /usr/local/redis-cluster/6375/redis-6375.conf 
./redis-server /usr/local/redis-cluster/6376/redis-6376.conf 
./redis-server /usr/local/redis-cluster/6377/redis-6377.conf 
./redis-server /usr/local/redis-cluster/6378/redis-6378.conf 
./redis-server /usr/local/redis-cluster/6379/redis-6379.conf 

 注意:如果在阿里云环境配置Redis集群,需要在安全组内开放 6374,6375,6376,6377,6378,6379和 16374,16375,16376,16377,16378,16379(集群总线端口)。

正常的客户端通信端口(通常是6379)用于和所有可到达集群的所有客户端通信
集群总线端口(the client port + 10000)必须对所有的其它节点是可到达的
也就是,要想集群正常工作,集群中的每个节点需要做到以下两点:

正常的客户端通信端口(通常是6379)必须对所有的客户端都开放,换言之,所有的客户端都可以访问
集群总线端口(客户端通信端口 + 10000)必须对集群中的其它节点开放,换言之,其它任意节点都可以访问

4、创建集群

注意:需要使用具体的IP地址,因为配置了密码,需要通过 -a test-pass 指定密码

cd /usr/local/redis-6.2.6/src

./redis-cli --cluster create 172.19.184.179:6374 172.19.184.179:6375 172.19.184.179:6376 172.19.184.179:6377 172.19.184.179:6378 172.19.184.179:6379 --cluster-replicas 1 -a test-pass

集群启动成功后,显示如下信息: 

# ./redis-cli --cluster create 172.19.184.179:6374 172.19.184.179:6375 172.19.184.179:6376 172.19.184.179:6377 172.19.184.179:6378 172.19.184.179:6379 --cluster-replicas 1 -a test-pass
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.19.184.179:6378 to 172.19.184.179:6374
Adding replica 172.19.184.179:6379 to 172.19.184.179:6375
Adding replica 172.19.184.179:6377 to 172.19.184.179:6376
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 7a6bba623eb71b7ba460ff77e8d8d25f06c4811d 172.19.184.179:6374
   slots:[0-5460] (5461 slots) master
M: 6f431cbe4b20f31a695fb8586f6fe72271910cee 172.19.184.179:6375
   slots:[5461-10922] (5462 slots) master
M: eed46871f40c1043126018680058d36bfff189b1 172.19.184.179:6376
   slots:[10923-16383] (5461 slots) master
S: 1a1e75e66bd3ecdc629b1259e459019949402ea6 172.19.184.179:6377
   replicates 7a6bba623eb71b7ba460ff77e8d8d25f06c4811d
S: 116c7729471402a78a2dbcd707a05126e88bceaf 172.19.184.179:6378
   replicates 6f431cbe4b20f31a695fb8586f6fe72271910cee
S: 49cfcaa45163202d452065921ea5d1c9802b6d50 172.19.184.179:6379
   replicates eed46871f40c1043126018680058d36bfff189b1
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.

Could not connect to Redis at 139.196.111.145:6379: Connection timed out
>>> Performing Cluster Check (using node 172.19.184.179:6374)
M: 7a6bba623eb71b7ba460ff77e8d8d25f06c4811d 172.19.184.179:6374
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 6f431cbe4b20f31a695fb8586f6fe72271910cee 139.196.111.145:6375
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 1a1e75e66bd3ecdc629b1259e459019949402ea6 139.196.111.145:6377
   slots: (0 slots) slave
   replicates 7a6bba623eb71b7ba460ff77e8d8d25f06c4811d
M: eed46871f40c1043126018680058d36bfff189b1 139.196.111.145:6376
   slots:[10923-16383] (5461 slots) master
S: 116c7729471402a78a2dbcd707a05126e88bceaf 139.196.111.145:6378
   slots: (0 slots) slave
   replicates 6f431cbe4b20f31a695fb8586f6fe72271910cee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 

六、简单测试

新建一个maven项目,引入Jedis,测试代码如下:

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.9.3</version>
</dependency>
public class TestJedis {
    public static void main(String[] args) {
        //ip地址使用外网地址
        JedisPool pool = new JedisPool("ip", 6375);
        Jedis jedis =  pool.getResource();
        jedis.auth("test-pass");
        jedis.set("test","test");

        System.out.println(jedis.get("test"));
    }
}

七、spring-boot-starter-data-redis 使用 lettuce 报连接超时问题解决方法

连接超时信息:

2020-08-14 14:57:49.180  WARN 22012 --- [ioEventLoop-6-4] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [172.17.0.13:6374]: connection timed out: /172.17.0.13:6374
2020-08-14 14:57:49.180  WARN 22012 --- [ioEventLoop-6-3] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [172.17.0.13:6375]: connection timed out: /172.17.0.13:6375
2020-08-14 14:57:49.182  WARN 22012 --- [ioEventLoop-6-2] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [172.17.0.13:6376]: connection timed out: /172.17.0.13:6376
2020-08-14 14:57:49.182  WARN 22012 --- [ioEventLoop-6-1] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [172.17.0.13:6377]: connection timed out: /172.17.0.13:6377
2020-08-14 14:57:49.190  WARN 22012 --- [ioEventLoop-6-1] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [172.17.0.13:6378]: connection timed out: /172.17.0.13:6378
2020-08-14 14:57:49.191  WARN 22012 --- [ioEventLoop-6-2] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to [172.17.0.13:6379]: connection timed out: /172.17.0.13:6379
2020-08-14 14:57:59.389  WARN 22012 --- [ioEventLoop-6-3] i.l.core.cluster.RedisClusterClient      : connection timed out: /172.17.0.13:6376
2020-08-14 14:58:09.391  WARN 22012 --- [ioEventLoop-6-4] i.l.core.cluster.RedisClusterClient      : connection timed out: /172.17.0.13:6377
2020-08-14 14:58:19.393  WARN 22012 --- [ioEventLoop-6-1] i.l.core.cluster.RedisClusterClient      : connection timed out: /172.17.0.13:6375
2020-08-14 14:58:29.396  WARN 22012 --- [ioEventLoop-6-2] i.l.core.cluster.RedisClusterClient      : connection timed out: /172.17.0.13:6374
2020-08-14 14:58:39.399  WARN 22012 --- [ioEventLoop-6-3] i.l.core.cluster.RedisClusterClient      : connection timed out: /172.17.0.13:6379
2020-08-14 14:58:49.402  WARN 22012 --- [ioEventLoop-6-4] i.l.core.cluster.RedisClusterClient      : connection timed out: /172.17.0.13:6378

解决方法:

将每个redis-637*.conf文件中,增加一个配置:cluster-announce-ip 10.1.1.5 ,值设置为对应的外网地址,杀掉每个redis进程,删除每个node下的文件 637*/appendonly.aof, 637*/nodes-637*.conf,637*/redis-637*.log,重新执行步骤三、步骤四即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值