继续上一章的学习,因为以前安装的是ubuntu仓库中的redis-server,致使不少命令都没有,因此专门卸载掉了,从官网从新下载了一个解压版,也推荐你们使用解压版。node
集群的配置有两种方式,咱们逐一学习
1、使用redis-trib.rb
首先在redis目录下新建文件夹cluster-test,因为搭建集群最少须要6个节点,咱们在文件夹下建立6个文件夹,以端口命名redis
给每一个文件夹中拷贝redis-server和redis.conf文件ubuntu
其中的appendonly.aof、dump.rdb、nodes.conf文件是启动后自动生成的bash
redis.conf文件修改以下app
port 7000 pidfile /var/run/redis_7000.pid appendonly yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000
而后将服务所有启动学习
启动后能够看到以下输出,表明配置成功测试
14287:M 06 May 11:36:01.890 * No cluster configuration found, I'm b1f810c73555ca5de844b15eb5e87d2ba304090c
接下来建立集群,这里用到了redis-trib.rb文件code
执行命令,等待命令执行完成,集群就建立成功了server
./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \ 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
下面咱们测试一下,随便连接到一个master的客户端上,进行的操做会自动定位到对于的实例上面get
redis-cli -c -p 7000 127.0.0.1:7000> set foo bar -> Redirected to slot [12182] located at 127.0.0.1:7002 OK 127.0.0.1:7002> set hello world -> Redirected to slot [866] located at 127.0.0.1:7000 OK 127.0.0.1:7000> get foo -> Redirected to slot [12182] located at 127.0.0.1:7002 "bar" 127.0.0.1:7002> get hello -> Redirected to slot [866] located at 127.0.0.1:7000 "world"
测试下连接到7001上面,同样执行成功
redis-cli -c -p 7001 127.0.0.1:7001> set test1 anwser1 -> Redirected to slot [4768] located at 127.0.0.1:7000 OK 127.0.0.1:7000> get test1 "anwser1" 127.0.0.1:7000> set test2 anwser2 -> Redirected to slot [8899] located at 127.0.0.1:7001 OK 127.0.0.1:7001> get test2 "anwser2" 127.0.0.1:7001> get foo -> Redirected to slot [12182] located at 127.0.0.1:7002 "bar" 127.0.0.1:7002> get hello -> Redirected to slot [866] located at 127.0.0.1:7000 "world"
2、使用create-cluster脚本建立集群
进入redis文件夹下的utils>create-cluster目录下,执行命令
create-cluster create
会自动建立一套集群,默认端口是从30001-30006
一样执行启动和中止操做
create-cluster start create-cluster stop