{"id":201113,"date":"2025-05-10T09:39:21","date_gmt":"2025-05-10T01:39:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201113\/"},"modified":"2025-05-10T09:39:21","modified_gmt":"2025-05-10T01:39:21","slug":"redis-cluster-%e9%9b%86%e7%be%a4%e6%90%ad%e5%bb%ba%e4%bd%a0%e4%bc%9a%e5%90%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201113\/","title":{"rendered":"Redis Cluster \u96c6\u7fa4\u642d\u5efa\u4f60\u4f1a\u5417"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis Cluster \u96c6\u7fa4\u642d\u5efa\u4f60\u4f1a\u5417<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728\u6570\u636e\u5e93\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u5c31\u6574\u7406\u5206\u4eab\u300aRedis Cluster \u96c6\u7fa4\u642d\u5efa\u4f60\u4f1a\u5417\u300b\uff0c\u804a\u804a\u96c6\u7fa4\u3001RedisCluster\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>\u4e09\u53f0\u673a\u5668 201\u3001202\u3001203\uff0c\u6bcf\u53f0\u673a\u5668\u88c5\u4e24\u4e2a redis \u5b9e\u4f8b\uff0c\u6784\u5efa redis cluster \u96c6\u7fa4\u3002<\/p>\n<h2>1. \u5b89\u88c5<\/h2>\n<p>\u6dfb\u52a0 redis-cluster \u76ee\u5f55\uff0c\u5c06 redis \u538b\u7f29\u5305\u62f7\u8d1d\u5230\u8be5\u76ee\u5f55\u4e0b\uff0c\u89e3\u538b\u538b\u7f29\u5305\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232150863ad99e455265.jpg\" class=\"aligncenter\"><\/p>\n<p>\u89e3\u538b\u5b8c\u540e\uff0c\u5c06\u6587\u4ef6\u5939 redis-5.0.3 \u91cd\u547d\u540d\u4e3a redis1\u3002<\/p>\n<pre>\n[root@test201 redis-cluster]# mv redis-5.0.3 redis1<\/pre>\n<p>\u9700\u8981\u5728 redis1 \u76ee\u5f55\u4e0b\u4f7f\u7528 make \u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\u3002<\/p>\n<pre>\n[root@test201 redis-cluster]# cd redis1\n[root@test201 redis1]# make<\/pre>\n<p>\u7136\u540e\u518d\u6267\u884c cp \u547d\u4ee4\u62f7\u8d1d\u53e6\u4e00\u4efd redis2\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232150863ad99e4e4d62.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5220\u9664\u5bf9\u5e94\u7684\u538b\u7f29\u5305\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151163ad99e7275b1.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5176\u4ed6\u4e24\u4e2a\u670d\u52a1\u5668\u4e5f\u662f\u6309\u7167\u4e0a\u9762\u7684\u64cd\u4f5c\u6765\u4e00\u904d\u3002<\/p>\n<h2>2. \u914d\u7f6e<\/h2>\n<p>\u5728 redis-cluster \u76ee\u5f55\u4e0b\u521b\u5efa\u6570\u636e\u6587\u4ef6\u5939 redis-data\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151163ad99e7933d1.jpg\" class=\"aligncenter\"><\/p>\n<p>\u518d\u5728\u8be5\u6587\u4ef6\u5939\u4e2d\u5efa\u4e24\u4e2a\u6587\u4ef6\u5939 7001 \u548c 7002\uff0c\u5206\u522b\u5b58\u653e\u4e24\u4e2a redis \u7684\u6570\u636e\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151163ad99e7f0cba.jpg\" class=\"aligncenter\"><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6 redis-1.conf\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151263ad99e86ea03.jpg\" class=\"aligncenter\"><\/p>\n<p>\u7f16\u8f91\u914d\u7f6e\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<pre>\n# \u6dfb\u52a0\u7aef\u53e3\u53f7\nport 7001\n# \u8bbe\u7f6e\u4e3a\u5b88\u62a4\u8fdb\u7a0b\uff0c\u914d\u7f6e redis \u540e\u53f0\u8fd0\u884c\ndaemonize yes\n# pid \u6587\u4ef6\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\u7684\uff0c\u76f4\u63a5\u6307\u5b9a\u76ee\u5f55\npidfile \/var\/run\/redis_7001.pid\n# \u6570\u636e\u4fdd\u5b58\u4f4d\u7f6e\ndir \/opt\/yidian\/redis-cluster\/redis-data\/7001\n# \u5173\u95ed\u4fdd\u62a4\u6a21\u5f0f\nprotected-mode no\n# \u5f00\u542f\u96c6\u7fa4\ncluster-enabled yes\n# \u96c6\u7fa4\u914d\u7f6e\u6587\u4ef6\uff0c\u4e0d\u9700\u8981\u6211\u4eec\u7ef4\u62a4\uff0c\u9996\u6b21\u542f\u52a8\u7684\u65f6\u5019\u4f1a\u81ea\u52a8\u751f\u6210\ncluster-config-file nodes_7001.conf\n# \u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4\ncluster-node-timeout 10100\n# \u5f00\u542f AOF \u65e5\u5fd7\nappendonly yes<\/pre>\n<p>\u590d\u5236\u4e00\u4efd redis-7002.conf \u914d\u7f6e\u6587\u4ef6\uff0c\u628a\u521a\u624d\u90a3\u4e2a redis-1.conf \u6587\u4ef6\u6539\u540d\u4e3a redis-7001.conf\uff0c\u63a5\u4e0b\u6765\u4fee\u6539 redis-7002.conf \u914d\u7f6e\u6587\u4ef6\u4e2d\u5bf9\u5e94\u5185\u5bb9\u4e3a 7002\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151263ad99e8d0716.jpg\" class=\"aligncenter\"><\/p>\n<p>\u7f16\u8f91\u914d\u7f6e\u6587\u4ef6 redis-7002.conf \u5185\u5bb9\uff1a<\/p>\n<pre>\n# \u6dfb\u52a0\u7aef\u53e3\u53f7\nport 7002\n# \u8bbe\u7f6e\u4e3a\u5b88\u62a4\u8fdb\u7a0b\uff0c\u914d\u7f6e redis \u540e\u53f0\u8fd0\u884c\ndaemonize yes\n# pid \u6587\u4ef6\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\u7684\uff0c\u76f4\u63a5\u6307\u5b9a\u76ee\u5f55\npidfile \/var\/run\/redis_7002.pid\n# \u6570\u636e\u4fdd\u5b58\u4f4d\u7f6e\ndir \/opt\/yidian\/redis-cluster\/redis-data\/7002\n# \u5173\u95ed\u4fdd\u62a4\u6a21\u5f0f\nprotected-mode no\n# \u5f00\u542f\u96c6\u7fa4\ncluster-enabled yes\n# \u96c6\u7fa4\u914d\u7f6e\u6587\u4ef6\uff0c\u4e0d\u9700\u8981\u6211\u4eec\u7ef4\u62a4\uff0c\u9996\u6b21\u542f\u52a8\u7684\u65f6\u5019\u4f1a\u81ea\u52a8\u751f\u6210\ncluster-config-file nodes_7002.conf\n# \u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4\ncluster-node-timeout 10100\n# \u5f00\u542f AOF \u65e5\u5fd7\nappendonly yes<\/pre>\n<p>\u521b\u5efa redis \u670d\u52a1\u7684\u542f\u52a8\u811a\u672c start-all.sh\uff0c\u5e76\u4e14\u4fee\u6539\u6267\u884c\u6743\u9650\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151363ad99e9b644c.jpg\" class=\"aligncenter\"><\/p>\n<p>\u811a\u672c start-all.sh \u5185\u5bb9\uff1a<\/p>\n<pre>\n\/opt\/yidian\/redis-cluster\/redis1\/src\/redis-server \/opt\/yidian\/redis-cluster\/redis-7001.conf\n\/opt\/yidian\/redis-cluster\/redis2\/src\/redis-server \/opt\/yidian\/redis-cluster\/redis-7002.conf<\/pre>\n<p>\u540c\u6837\u7684\u65b9\u5f0f\u53ef\u4ee5\u521b\u5efa redis \u670d\u52a1\u5173\u95ed\u811a\u672c stop-all.sh\uff1a<\/p>\n<pre>\n\/opt\/yidian\/redis-cluster\/redis1\/src\/redis-cli -p 7001 shutdown\n\/opt\/yidian\/redis-cluster\/redis2\/src\/redis-cli -p 7002 shutdown<\/pre>\n<p>\u5176\u4ed6\u4e24\u53f0\u670d\u52a1\u5668\u4e0a\u91cd\u590d\u4e0a\u8ff0\u914d\u7f6e\u8fc7\u7a0b\u3002<\/p>\n<h2>3. \u542f\u52a8<\/h2>\n<p>\u5728\u4e09\u53f0\u670d\u52a1\u5668\u4e0a\u5206\u522b\u6267\u884c\u542f\u52a8\u811a\u672c\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151463ad99ea7d5ed.jpg\" class=\"aligncenter\"><\/p>\n<p style=\"text-align: left\">\u542f\u52a8\u5b8c\u540e\u5206\u522b\u67e5\u770b\uff0c\u53ef\u770b\u5230\u5982\u4e0b\u8fdb\u7a0b\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151463ad99ead627b.jpg\" class=\"aligncenter\"><\/p>\n<p style=\"text-align: left\">\u968f\u4fbf\u627e\u4e00\u53f0\u670d\u52a1\u5668\u767b\u5f55\uff0c\u8fd9\u91cc\u767b\u5f55 201\uff0c\u7136\u540e\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\uff1a<\/p>\n<pre>\n.\/redis-cli --cluster create 192.168.57.201:7001 192.168.57.201:7002 192.168.57.202:7001 192.168.57.202:7002 192.168.57.203:7001 192.168.57.203:7002 --cluster-replicas 1 <\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u5f00\u59cb\u521d\u59cb\u5316\u521b\u5efa\u96c6\u7fa4\u4e86\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151563ad99eb1396a.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4e2d\u95f4\u7684\u4e00\u4e9b\u8be2\u95ee\u8f93\u5165 yes\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151563ad99ebc0ebf.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5230\u8fd9\u91cc redis cluster \u96c6\u7fa4\u5c31\u521b\u5efa\u5b8c\u6bd5\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u767b\u5f55\u67e5\u770b\u96c6\u7fa4\u4fe1\u606f\u3002<\/p>\n<h2>4. \u767b\u5f55<\/h2>\n<p>\u767b\u5f55\u96c6\u7fa4\uff08-c \u8868\u793a\u4ee5\u96c6\u7fa4\u6a21\u5f0f\u767b\u5f55\uff09\uff1a<\/p>\n<pre>\n.\/redis-cli -c -h 127.0.0.1 -p 7001<\/pre>\n<p>\u67e5\u770b\u96c6\u7fa4\u4fe1\u606f\uff1a<\/p>\n<pre>\n127.0.0.1:7001&gt; cluster info<\/pre>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151663ad99ec2192d.jpg\" class=\"aligncenter\"><\/p>\n<p>\u67e5\u770b\u8282\u70b9\u4fe1\u606f\uff1a<\/p>\n<pre>\n127.0.0.1:7001&gt; cluster nodes<\/pre>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221229\/167232151663ad99ec93da5.jpg\" class=\"aligncenter\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis Cluster \u96c6\u7fa4&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201113","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201113"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201113\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}