-1-Redis3.x集群配置操作文档-2-目录1Redis集群简介......................................-1-2配置步骤............................................-1-2.1创建目录......................................-1-2.2创建通用配置...................................-2-2.3创建节点实例目录................................-3-2.4创建节点实例配置................................-4-2.5启动节点服务...................................-5-2.6初始化集群.....................................-5-3测试...............................................-7-4重新配置............................................-7-4.1停止节点服务...................................-7-4.2删除配置......................................-7--1-1Redis集群简介此处的集群指的是Cluster,通过分区/分片来实现一定程度的分布式与高可用部署。以下是官网文档RedisClusterSpecification–Redis以下是上文链接的翻译文档Redis集群规范—Redis命令参考配置步骤要让集群正常运作至少需要三个主节点此处使用六个节点:其中三个为主节点,其余三个则是各个主节点的从节点所有实例位于同一机器,主节点端口7001,7002,7003,从节点8001,8002,8003。Redis使用3.2.0,编译安装,可执行文件在/usr/local/redis-3.2.0/bin/下。参考文档Redisclustertutorial–Redis集群教程—Redis命令参考创建目录#mkdircluster-test-2-#cdcluster-test2.2创建通用配置参考附件注释comm-cluster.conf双击选择工具打开,复制内容.如果是直接复制文件再打开,文件第一行会多出一部分.#绑定主机IP,根据具体情况配置,可以在后文节点配置文件redis.conf中覆盖bind192.168.40.202127.0.0.1protected-modeyestcp-backlog511timeout0tcp-keepalive0#后台运行,对应后文节点配置文件redis.conf的pid和log配置daemonizeyessupervisednologlevelnoticelogfiledatabases16save9001save30010save6010000stop-writes-on-bgsave-erroryesrdbcompressionyesrdbchecksumyesdbfilenamedump.rdbdir./slave-serve-stale-datayesslave-read-onlyyesrepl-diskless-syncnorepl-diskless-sync-delay5repl-disable-tcp-nodelaynoslave-priority100appendonlyyesappendfilenameappendonly.aofappendfsynceverysecno-appendfsync-on-rewritenoauto-aof-rewrite-percentage100auto-aof-rewrite-min-size64mb-3-aof-load-truncatedyeslua-time-limit5000cluster-enabledyescluster-config-filenodes.confcluster-node-timeout5000slowlog-log-slower-than10000slowlog-max-len128latency-monitor-threshold0notify-keyspace-eventshash-max-ziplist-entries512hash-max-ziplist-value64list-max-ziplist-size-2list-compress-depth0set-max-intset-entries512zset-max-ziplist-entries128zset-max-ziplist-value64hll-sparse-max-bytes3000activerehashingyesclient-output-buffer-limitnormal000client-output-buffer-limitslave256mb64mb60client-output-buffer-limitpubsub32mb8mb60hz10aof-rewrite-incremental-fsyncyes2.3创建节点实例目录newdir.sh#!/bin/bashend=4for((i=1;iend;i++))domkdir700$imkdir800$idone#bashnewdir.sh-4-2.4创建节点实例配置newconf.sh根据节点具体IP,决定bind指令是否使用不同IP#!/bin/bashend=4#创建配置文件for((i=1;iend;i++))docur=700$icd$curif[-fredis.conf];thenecho'filealreadyexists!'rmredis.conffiecho`pwd`createnewfiletouchredis.confecho-einclude/data/redis/cluster-test/comm-cluster.conf\n#bind192.168.40.202127.0.0.1\nport$cur\npidfile/var/run/redis$cur.pid\nlogfile/var/log/redis$cur.log\n#dir./redis.confcd../done#创建配置文件for((i=1;iend;i++))docur=800$icd$curif[-fredis.conf];thenecho'filealreadyexists!'rmredis.conffiecho`pwd`createnewfiletouchredis.confecho-einclude/data/redis/cluster-test/comm-cluster.conf\n#bind192.168.40.202-5-127.0.0.1\nport$cur\npidfile/var/run/redis$cur.pid\nlogfile/var/log/redis$cur.log\n#dir./redis.confcd../done#bashnewconf.sh2.5启动节点服务start.sh#bashstart.sh如果不使用sh执行,一定要进入各个node的目录执行启动,否则会①提示配置文件指令不正确,如下***FATALCONFIGFILEERROR***Readingtheconfigurationfile,atline1'cluster-enabledyes'Baddirectiveorwrongnumberofarguments②redis各个节点数据文件和集群节点配置文件nodes.conf会在执行命令的目录下生成,相互覆盖2.6初始化集群安装ruby和redis驱动#yuminstallruby#geminstallredisinit.sh#bashinit.sh执行命令后redis-trib会打印出一份配置信息,如果你觉得没问题的话,就可以输入yes,redis-trib就会将这份配置应用到集群当中。-6-具体参考上文链接,类似下列图片-7-命令中的--replicas1表示给每个主节点分配一个从节点。命令参数中的前3个节点自动是主节点。如非本机访问,需要开启防火墙端口,只开启主节点端口即可。3测试上文配置结果,主节点192.168.40.202:7001,192.168.40.202:7002,192.168.40.202:7003#firewall-cmd--add-port=7001-7003/tcpjava测试,jedis2.8.1ClusterTest.java4重新配置如果要重新配置集群,停止服务,删除节点配置文件,重新执行初始化4.1停止节点服务stop.sh#bashstop.sh4.2删除配置delconf.sh#bashdelconf.sh