{"id":201692,"date":"2025-05-10T10:34:55","date_gmt":"2025-05-10T02:34:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201692\/"},"modified":"2025-05-10T10:34:55","modified_gmt":"2025-05-10T02:34:55","slug":"redis%e5%88%86%e7%89%87%e9%9b%86%e7%be%a4%e7%9a%84%e6%90%ad%e5%bb%ba%e4%b8%8e%e4%bd%bf%e7%94%a8%e8%af%a5%e6%80%8e%e4%b9%88%e5%81%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201692\/","title":{"rendered":"&#8220;Redis\u5206\u7247\u96c6\u7fa4\u7684\u642d\u5efa\u4e0e\u4f7f\u7528\u8be5\u600e\u4e48\u505a&#8221;"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>&#8220;Redis\u5206\u7247\u96c6\u7fa4\u7684\u642d\u5efa\u4e0e\u4f7f\u7528\u8be5\u600e\u4e48\u505a&#8221;<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;font-family:\">\u300a&#8221;Redis\u5206\u7247\u96c6\u7fa4\u7684\u642d\u5efa\u4e0e\u4f7f\u7528\u8be5\u600e\u4e48\u505a&#8221;\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;font-family:\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105636459dbc3abcd3.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u524d\u8a00<\/h3>\n<p>redis\u53ef\u4ee5\u8bf4\u5728\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u4e2d\u4f7f\u7528\u7684\u975e\u5e38\u9891\u7e41\uff0c\u5728redis\u5e38\u7528\u96c6\u7fa4\u4e2d\uff0c\u6211\u4eec\u804a\u5230\u4e86redis\u5e38\u7528\u7684\u51e0\u79cd\u96c6\u7fa4\u65b9\u6848\uff0c\u4e0d\u540c\u7684\u96c6\u7fa4\u5bf9\u5e94\u7740\u4e0d\u540c\u7684\u573a\u666f\uff0c\u5e76\u4e14\u8be6\u7ec6\u8bf4\u660e\u4e86\u5404\u79cd\u96c6\u7fa4\u7684\u4f18\u52a3\uff0c\u672c\u7bc7\u5c06\u4ee5redis \u5206\u7247\u96c6\u7fa4\u4e3a\u5207\u5165\u70b9\uff0c\u4eceredis \u5206\u7247\u96c6\u7fa4\u7684\u642d\u5efa\u5f00\u59cb\uff0c\u8be6\u7ec6\u8bf4\u8bf4redis \u5206\u7247\u96c6\u7fa4\u76f8\u5173\u7684\u6280\u672f\u70b9\uff1b<\/p>\n<ul>\n<li>\n<p>\u5355\u70b9\u6545\u969c\uff1b<\/p>\n<\/li>\n<li>\n<p>\u5355\u673a\u5199\uff08\u9ad8\u5e76\u53d1\u5199\uff09\u74f6\u9888\uff1b<\/p>\n<\/li>\n<li>\n<p>\u5355\u673a\u5b58\u50a8\u6570\u636e\u5bb9\u91cf\u9650\u5236\uff1b<\/p>\n<\/li>\n<li>\n<p>\u96c6\u7fa4\u81ea\u52a8\u4f38\u7f29\uff1b<\/p>\n<\/li>\n<li>\n<p>\u96c6\u7fa4\u81ea\u52a8\u6545\u969c\u4e0b\u7ebf\u4e0e\u8f6c\u79fb\uff1b<\/p>\n<\/li>\n<\/ul>\n<h3>\u642d\u5efa\u96c6\u7fa4\u67b6\u6784\u56fe<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105636459dbc3b6ecd.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u524d\u7f6e\u51c6\u5907<\/h3>\n<p><strong>1\u3001\u51c6\u5907\u4e00\u53f0\uff08\u62166\u53f0\u670d\u52a1\u5668\uff0c\u6761\u4ef6\u5141\u8bb8\u7684\u60c5\u51b5\u4e0b\uff09\u865a\u62df\u673a\u6216\u670d\u52a1\u5668\uff1b<\/strong><\/p>\n<p>\u672c\u4f8b\u4e3a\u6f14\u793a\u6548\u679c\u51c6\u5907\u4e86\u4e00\u53f0\u5185\u5b588G\u7684\u670d\u52a1\u5668\uff0c\u5c06\u901a\u8fc7\u7aef\u53e3\u8fdb\u884cmaster\u548cslave\u7684\u5212\u5206<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105636459dbc3c2629.jpg\" class=\"aligncenter\"><\/p>\n<p>2\u3001\u63d0\u524d\u4e0b\u8f7d\u597dredis\u5b89\u88c5\u5305\uff1b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105636459dbc3ccd6f.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u642d\u5efa\u6b65\u9aa4<\/h3>\n<p>1\u3001\u521b\u5efa6\u4e2a\u76ee\u5f55\u6587\u4ef6\uff0c\u5206\u522b\u7528\u4e8e\u4fdd\u5b58\u5404\u4e2aredis\u793a\u4f8b\u7684\u6570\u636e<\/p>\n<pre>mkdir&nbsp;7001&nbsp;7002&nbsp;7003&nbsp;8001&nbsp;8002&nbsp;8003<\/pre>\n<p>2\u3001\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aredis.conf\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b<\/p>\n<pre>port&nbsp;6379\n#&nbsp;\u5f00\u542f\u96c6\u7fa4\u529f\u80fd\ncluster-enabled&nbsp;yes\n#&nbsp;\u96c6\u7fa4\u7684\u914d\u7f6e\u6587\u4ef6\u540d\u79f0\uff0c\u4e0d\u9700\u8981\u6211\u4eec\u521b\u5efa\uff0c\u7531redis\u81ea\u5df1\u7ef4\u62a4\ncluster-config-file&nbsp;\/usr\/local\/soft\/redis\/6379\/nodes.conf\n#&nbsp;\u8282\u70b9\u5fc3\u8df3\u5931\u8d25\u7684\u8d85\u65f6\u65f6\u95f4\ncluster-node-timeout&nbsp;5000\n#&nbsp;\u6301\u4e45\u5316\u6587\u4ef6\u5b58\u653e\u76ee\u5f55\ndir&nbsp;\/usr\/local\/soft\/redis\/6379\n#&nbsp;\u7ed1\u5b9a\u5730\u5740\nbind&nbsp;0.0.0.0\n#&nbsp;\u8ba9redis\u540e\u53f0\u8fd0\u884c\ndaemonize&nbsp;yes\n#&nbsp;\u6ce8\u518c\u7684\u5b9e\u4f8bip\nreplica-announce-ip&nbsp;\u672c\u673a\u516c\u7f51IP\n#&nbsp;\u4fdd\u62a4\u6a21\u5f0f\nprotected-mode&nbsp;no\n#&nbsp;\u6570\u636e\u5e93\u6570\u91cf\ndatabases&nbsp;1\n#&nbsp;\u65e5\u5fd7\nlogfile&nbsp;\/usr\/local\/soft\/redis\/6379\/run.log<\/pre>\n<p>3\u3001\u6279\u91cf\u5c06\u8be5\u914d\u7f6e\u6587\u4ef6\u62f7\u8d1d\u5230\u5404\u4e2a\u76ee\u5f55\u4e0b<\/p>\n<pre>echo&nbsp;7001&nbsp;7002&nbsp;7003&nbsp;8001&nbsp;8002&nbsp;8003&nbsp;|&nbsp;xargs&nbsp;-t&nbsp;-n&nbsp;1&nbsp;cp&nbsp;redis.conf<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105636459dbc3d6ef0.jpg\" class=\"aligncenter\"><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105636459dbc3e208b.jpg\" class=\"aligncenter\"><\/p>\n<p>4\u3001\u4fee\u6539\u6bcf\u4e2a\u76ee\u5f55\u4e0bredis.conf\uff0c\u5c06\u5176\u4e2d\u76846379\u4fee\u6539\u4e3a\u4e0e\u6240\u5728\u76ee\u5f55\u4e00\u81f4<\/p>\n<pre>printf&nbsp;'%s\\n'&nbsp;7001&nbsp;7002&nbsp;7003&nbsp;8001&nbsp;8002&nbsp;8003&nbsp;|&nbsp;xargs&nbsp;-I{}&nbsp;-t&nbsp;sed&nbsp;-i&nbsp;'s\/6379\/{}\/g'&nbsp;{}\/redis.conf<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105636459dbc3ea205.jpg\" class=\"aligncenter\"><\/p>\n<p>\u968f\u673a\u6253\u5f00\u67d0\u4e2aredis.conf\u770b\u770b\u662f\u5426\u4fee\u6539\u6210\u529f<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc400dc4.jpg\" class=\"aligncenter\"><\/p>\n<p>4\u3001\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u8fdb\u884c\u542f\u52a8<\/p>\n<pre>\/usr\/local\/soft\/redis\/redis60\/src\/redis-server&nbsp;\/usr\/local\/soft\/redis\/7001\/redis.conf\n\/usr\/local\/soft\/redis\/redis60\/src\/redis-server&nbsp;\/usr\/local\/soft\/redis\/7002\/redis.conf\n\/usr\/local\/soft\/redis\/redis60\/src\/redis-server&nbsp;\/usr\/local\/soft\/redis\/7003\/redis.conf\n\n\/usr\/local\/soft\/redis\/redis60\/src\/redis-server&nbsp;\/usr\/local\/soft\/redis\/8001\/redis.conf\n\/usr\/local\/soft\/redis\/redis60\/src\/redis-server&nbsp;\/usr\/local\/soft\/redis\/8002\/redis.conf\n\/usr\/local\/soft\/redis\/redis60\/src\/redis-server&nbsp;\/usr\/local\/soft\/redis\/8003\/redis.conf<\/pre>\n<p>\u6267\u884c\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u770b\u52306\u4e2aredis\u5b9e\u4f8b\u5df2\u7ecf\u5f00\u542f<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc40c2a4.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u521b\u5efa\u96c6\u7fa4<\/h3>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u6b65\u9aa4\uff0c\u5c31\u5f00\u542f\u4e866\u4e2aredis\u7684\u5b9e\u4f8b\uff0c\u4f46\u662f\u8fd9\u4e9b\u5b9e\u4f8b\u4e4b\u95f4\u5e76\u6ca1\u6709\u5f62\u6210\u4e00\u4e2a\u96c6\u7fa4\uff0c\u63a5\u4e0b\u6765\u5c31\u9700\u8981\u6267\u884c\u76f8\u5173\u7684\u547d\u4ee4\u5c06\u5b83\u4eec\u6784\u6210\u4e00\u4e2a\u96c6\u7fa4<\/p>\n<p><strong>1\u3001\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u6784\u5efa\u96c6\u7fa4\uff085.0\u4e4b\u540e\u7684\u7248\u672c\u7684\u547d\u4ee4\uff09<\/strong><\/p>\n<pre>redis-cli&nbsp;--cluster&nbsp;create&nbsp;--cluster-replicas&nbsp;1&nbsp;IP\u5730\u5740:7001&nbsp;IP\u5730\u5740:7002&nbsp;IP\u5730\u5740:7003&nbsp;IP\u5730\u5740:8001&nbsp;IP\u5730\u5740:8002&nbsp;IP\u5730\u5740:8003<\/pre>\n<p><strong>\u5173\u4e8e\u4e0a\u9762\u547d\u4ee4\u7684\u8bf4\u660e<\/strong><\/p>\n<ul>\n<li>\n<p>redis-cli &#8211;cluster \u6216\u8005 .\/redis-trib.rb\uff1a\u4ee3\u8868\u96c6\u7fa4\u64cd\u4f5c\u547d\u4ee4\uff1b<\/p>\n<\/li>\n<li>\n<p>create\uff1a\u4ee3\u8868\u662f\u521b\u5efa\u96c6\u7fa4\uff1b<\/p>\n<\/li>\n<li>\n<p>\u2013replicas 1\u6216\u8005\u2013cluster-replicas 1 \uff1a\u6307\u5b9a\u96c6\u7fa4\u4e2d\u6bcf\u4e2amaster\u7684\u526f\u672c\u4e2a\u6570\u4e3a1\uff0c\u6b64\u65f6\u8282\u70b9\u603b\u6570 \u00f7 (replicas + 1)\u5f97\u5230\u7684\u5c31\u662fmaster\u7684\u6570\u91cf\u3002\u56e0\u6b64\u8282\u70b9\u5217\u8868\u4e2d\u7684\u524dn\u4e2a\u5c31\u662fmaster\uff0c\u5176\u5b83\u8282\u70b9\u90fd\u662fslave\u8282\u70b9\uff0c\u968f\u673a\u5206\u914d\u5230\u4e0d\u540cmaster \uff1b<\/p>\n<\/li>\n<\/ul>\n<p>\u4e5f\u53ef\u4ee5\u901a\u8fc7 redis-cli &#8211;cluster help \u547d\u4ee4\u8fdb\u884c\u96c6\u7fa4\u547d\u4ee4\u7684\u67e5\u770b;<\/p>\n<p>\u6267\u884c\u547d\u4ee4\u540e\uff0c\u63a7\u5236\u53f0\u63d0\u793a\u5c06\u4f1a\u4f7f\u7528\u54ea\u4e2a\u793a\u4f8b\u4f5c\u4e3amaster\uff0c\u54ea\u4e2a\u4f5c\u4e3aslave\uff1b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4169b3.jpg\" class=\"aligncenter\"><\/p>\n<p>\u7ee7\u7eed\u8f93\u5165yes\u4e4b\u540e\uff0c\u5c06\u4f1a\u6309\u7167\u4e0a\u9762\u7684\u7b56\u7565\u521b\u5efa\u4e00\u4e2a\u96c6\u7fa4\u51fa\u6765<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc420405.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u901a\u8fc7\u8fd9\u4e2a\u8f93\u51fa\u7684\u65e5\u5fd7\u5927\u6982\u53ef\u4ee5\u5f97\u51fa\u5982\u4e0b\u4fe1\u606f\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u7aef\u53e3\u53f7\u4f9d\u6b21\u4e3a7001\uff0c7002\u548c7003\u7684\u4e3amaster\u8282\u70b9\uff1b<\/p>\n<\/li>\n<li>\n<p>\u7aef\u53e3\u53f7\u4f9d\u6b21\u4e3a8001\uff0c8002\u548c8003\u7684\u4e3aslave\u8282\u70b9\uff1b<\/p>\n<\/li>\n<li>\n<p>\u7aef\u53e3\u53f7\u4f9d\u6b21\u4e3a7001\uff0c7002\u548c7003\u7684master\u8282\u70b9\uff0c\u5404\u81ea\u5206\u914d\u4e86\u4e00\u5b9a\u533a\u95f4\u7684\u69fd\u4f4d\u6570\uff0c\u5206\u522b\u662f\uff1a[0-5460]\uff0c[5461-10922]\uff0c[10923-16383]\uff0c\u5373\u603b\u5171\u5206\u914d16384\u4e2a\u69fd\u4f4d\uff1b<\/p>\n<\/li>\n<\/ul>\n<p><strong>2\u3001\u67e5\u770b\u96c6\u7fa4\u72b6\u6001<\/strong><\/p>\n<pre>redis-cli&nbsp;-p&nbsp;7001&nbsp;cluster&nbsp;nodes<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc42a46b.jpg\" class=\"aligncenter\"><\/p>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u547d\u4ee4\uff0c\u53ef\u4ee5\u6e05\u695a\u7684\u770b\u5230\u96c6\u7fa4\u4e2d\u5404\u4e2a\u5b9e\u4f8b\u8282\u70b9\u7684\u4e3b\u4ece\u72b6\u6001\uff0c\u5b9e\u4f8bID\uff08\u552f\u4e00\u8eab\u4efd\u6807\u8bc6\uff09\uff0c\u69fd\u4f4d\u533a\u95f4\u7b49\u4fe1\u606f<\/p>\n<h3>Redis\u6563\u5217\u63d2\u69fd\u8bf4\u660e<\/h3>\n<p>\u5176\u5b9e\u5bf9redis\u5206\u7247\u96c6\u7fa4\u7a0d\u6709\u4e86\u89e3\u7684\u540c\u5b66\u5e94\u8be5\u77e5\u9053\uff0credis\u5206\u7247\u96c6\u7fa4\u5728\u903b\u8f91\u4e0a\u5c06\u96c6\u7fa4\u4e2d\u7684\u6240\u6709\u8282\u70b9\u6784\u6210\u4e86\u4e00\u5757\u5b8c\u6574\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u6570\u636e\u5199\u5165\u8fdb\u6765\u540e\uff0c\u5177\u4f53\u5b58\u653e\u5230\u54ea\u4e2a\u8282\u70b9\u5462\uff1f\u6240\u4ee5\u96c6\u7fa4\u5f15\u5165\u4e86\u4e00\u4e2a\u903b\u8f91\u5c1a\u7684\u63d2\u69fd\u6982\u5ff5\uff0c\u5373\u5c06\u96c6\u7fa4\u5212\u5206\u4e3a16384\u4e2a\u69fd\u4f4d\uff0c\u96c6\u7fa4\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u5360\u636e\u4e00\u90e8\u5206\u69fd\u4f4d\u6570\uff08\u8fd9\u4e2a\u65e5\u5fd7\u4e2d\u53ef\u4ee5\u770b\u51fa\u6765\uff09\uff1b<\/p>\n<p>\u90a3\u4e48\u5f53\u67d0\u4e2a\u5177\u4f53\u7684key\u5199\u5165\u7684\u65f6\u5019\uff0c\u96c6\u7fa4\u5c06\u4f1a\u901a\u8fc7\u4e00\u5b9a\u7684\u7b97\u6cd5\uff0c\u5c06\u8981\u5199\u5165\u7684\u6570\u636e\u8def\u7531\u5230\u6307\u5b9a\u7684\u63d2\u69fd\u4e0a\u53bb\uff1b<\/p>\n<p>\u8fd9\u91cc\u6709\u4e2a\u70b9\u9700\u8981\u6ce8\u610f\uff0c\u6570\u636ekey\u4e0d\u662f\u4e0e\u8282\u70b9\u7ed1\u5b9a\uff0c\u800c\u662f\u4e0e\u63d2\u69fd\u7ed1\u5b9a\u3002<\/p>\n<p><strong>redis\u96c6\u7fa4\u4f1a\u6839\u636ekey\u7684\u6709\u6548\u90e8\u5206\u8ba1\u7b97\u63d2\u69fd\u503c\uff0c\u5206\u4e24\u79cd\u60c5\u51b5\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>key\u4e2d\u5305\u542b&#8221;{}&#8221;\uff0c\u4e14\u201c{}\u201d\u4e2d\u81f3\u5c11\u5305\u542b1\u4e2a\u5b57\u7b26\uff0c\u201c{}\u201d\u4e2d\u7684\u90e8\u5206\u662f\u6709\u6548\u90e8\u5206\uff1b<\/p>\n<\/li>\n<li>\n<p>key\u4e2d\u4e0d\u5305\u542b\u201c{}\u201d\uff0c\u6574\u4e2akey\u90fd\u662f\u6709\u6548\u90e8\u5206\uff1b<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u4e3e\u4f8b\u6765\u8bf4\uff1akey\u662fnum\uff0c\u90a3\u4e48\u5c31\u6839\u636enum\u8ba1\u7b97\uff0c\u5982\u679c\u662f{\u5e94\u7528\u540d\u79f0}num\uff0c\u5219\u6839\u636e\u201c\u5e94\u7528\u540d\u79f0\u201d\u8ba1\u7b97\u3002\u8ba1\u7b97\u65b9\u5f0f\u662f\u5229\u7528CRC16\u7b97\u6cd5\u5f97\u5230\u4e00\u4e2ahash\u503c\uff0c\u7136\u540e\u5bf916384\u53d6\u4f59\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u5c31\u662fslot\u503c<\/p>\n<\/blockquote>\n<p>\u4e0b\u9762\u4e0d\u59a8\u770b\u4e0b\u6548\u679c<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc435319.jpg\" class=\"aligncenter\"><\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u6f14\u793a\u53ef\u4ee5\u53d1\u73b0\uff0c\u7ecf\u8fc7\u96c6\u7fa4\u8ba1\u7b97\u7684key\u5c06\u4f1a\u5206\u914d\u5230\u4e0d\u540c\u7684\u63d2\u69fd\u4e0a\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0ckey\u662f\u4e0e\u63d2\u69fd\u7ed1\u5b9a\uff0c\u800c\u4e0d\u662f\u4e0e\u67d0\u4e2a\u8282\u70b9\u7ed1\u5b9a\uff0c\u60f3\u60f3\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\u5462\uff1f<\/p>\n<p>\u601d\u8003\u4e0b\u9762\u8fd9\u4e2a\u9700\u6c42<\/p>\n<blockquote>\n<p>\u5982\u4f55\u5c06\u540c\u4e00\u7c7b\u6570\u636e\u56fa\u5b9a\u7684\u4fdd\u5b58\u5728\u540c\u4e00\u4e2aRedis\u5b9e\u4f8b\uff1f<\/p>\n<\/blockquote>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5982\u679ckey\u7684\u5206\u914d\u5b8c\u5168\u6ca1\u6709\u89c4\u5219\u7684\u8bdd\uff0c\u5f53\u6d89\u53ca\u5230\u67d0\u4e2a\u4e1a\u52a1\u7c7b\u7684\u6570\u636e\u5bf9\u5e94\u7684key\u968f\u673a\u5206\u914d\u5230\u4e0d\u540c\u7684\u8282\u70b9\u4e0a\u9762\u65f6\uff0c\u53d6\u503c\u7684\u65f6\u5019\u5c31\u4f1a\u51fa\u73b0\u50cf\u4e0a\u9762\u7684\u91cd\u5b9a\u5411\u8de8\u8282\u70b9\u7684\u95ee\u9898\uff0c\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u5347\u6027\u80fd\uff1b<\/p>\n<p><strong>\u89e3\u51b3\u529e\u6cd5<\/strong><\/p>\n<blockquote>\n<p>\u8fd9\u4e00\u7c7b\u6570\u636e\u4f7f\u7528\u76f8\u540c\u7684\u6709\u6548\u90e8\u5206\uff0c\u4f8b\u5982key\u90fd\u4ee5{\u4e1a\u52a1ID}\u4e3a\u524d\u7f00<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4406ed.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u96c6\u7fa4\u4f38\u7f29\uff08\u6dfb\u52a0\u8282\u70b9\uff09<\/h3>\n<p>reids\u7684cluster\u6a21\u5f0f\u4e0b\uff0c\u6709\u4e00\u4e2a\u6bd4\u8f83\u5f3a\u7684\u529f\u80fd\u5c31\u662f\u96c6\u7fa4\u7684\u4f38\u7f29\u80fd\u529b\uff0c\u5373\u5728\u73b0\u6709\u7684\u96c6\u7fa4\u57fa\u7840\u4e0a\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u8fdb\u884c\u96c6\u7fa4\u7684\u4f38\u7f29\uff0c\u4e0b\u9762\u6765\u6f14\u793a\u4e0b\u7ed9\u4e0a\u9762\u7684\u96c6\u7fa4\u6dfb\u52a0\u4e00\u4e2a\u65b0\u8282\u70b9\u7684\u6b65\u9aa4\uff1b<\/p>\n<p><strong>1\u3001\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u62f7\u8d1d\u4e00\u4e2a\u76ee\u5f55<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4490d3.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>2\u3001\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u7684\u7aef\u53e3<\/strong><\/p>\n<pre>sed&nbsp;-i&nbsp;s\/6379\/7004\/g&nbsp;7004\/redis.conf<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc452ad5.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>3\u3001\u542f\u52a8\u8fd9\u4e2a\u5b9e\u4f8b<\/strong><\/p>\n<pre>\/usr\/local\/soft\/redis\/redis60\/src\/redis-server&nbsp;\/usr\/local\/soft\/redis\/7004\/redis.conf<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc45d6f7.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>4\u3001\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u5c067004\u5b9e\u4f8b\u5047\u5982\u96c6\u7fa4<\/strong><\/p>\n<pre>redis-cli&nbsp;--cluster&nbsp;add-node&nbsp;IP:7004&nbsp;IP:7001<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc468aae.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>5\u3001\u518d\u6b21\u67e5\u770b\u96c6\u7fa4\u72b6\u6001<\/strong><\/p>\n<pre>redis-cli&nbsp;-p&nbsp;7001&nbsp;cluster&nbsp;nodes<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4730d5.jpg\" class=\"aligncenter\"><\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u72b6\u6001\u4fe1\u606f\u53d1\u73b0\uff0c7004\u8fd9\u4e2a\u8282\u70b9\u867d\u7136\u52a0\u5165\u4e86\u96c6\u7fa4\uff0c\u5e76\u6210\u4e86master\uff0c\u4f46\u662f\u96c6\u7fa4\u5e76\u6ca1\u6709\u7ed9\u5b83\u5206\u914d\u4efb\u4f55\u7684\u63d2\u69fd<\/p>\n<p><strong>6\u3001\u5206\u914d\u63d2\u69fd<\/strong><\/p>\n<p>\u5206\u914d\u63d2\u69fd\u7684\u57fa\u672c\u601d\u8def\u662f\uff0c\u4ece\u73b0\u6709\u7684\u67d0\u4e2a\u8282\u70b9\u4e0a\u8fc1\u79fb\u90e8\u5206\u63d2\u69fd\u5230\u65b0\u7684\u8282\u70b9\u5373\u53ef\uff0c\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u8fdb\u884c\u63d2\u69fd\u5206\u914d<\/p>\n<pre>redis-cli&nbsp;--cluster&nbsp;reshard&nbsp;101.34.23.80:7001<\/pre>\n<p>\u6267\u884c\u547d\u4ee4\u540e\u5c06\u51fa\u73b0\u4e0b\u9762\u7684\u63d0\u793a\uff0c\u8fd9\u91cc\u8f93\u5165\u4f60\u8981\u8f6c\u79fb\u7684\u63d2\u69fd\u6570\u91cf<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc47d93e.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5c067004\u5bf9\u5e94\u7684\u5b9e\u4f8bID\u8f93\u5165\u5373\u53ef<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc48934b.jpg\" class=\"aligncenter\"><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4935c1.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8f93\u5165yes\u540e\u5f00\u59cb\u79fb\u52a8\u63d2\u69fd<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc49d53d.jpg\" class=\"aligncenter\"><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4a7e2f.jpg\" class=\"aligncenter\"><\/p>\n<p>\u79fb\u52a8\u5b8c\u6210\u540e\uff0c\u518d\u6b21\u67e5\u770b\u96c6\u7fa4\u72b6\u6001\uff0c\u8fd9\u65f6\u50197004\u5bf9\u5e94\u7684\u8282\u70b9\u5c31\u5206\u914d\u5230\u4e86\u4ece0 ~ 500\u7684\u6570\u91cf\u7684\u63d2\u69fd<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4b21e7.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u6545\u969c\u8f6c\u79fb<\/h3>\n<p>redis\u7684cluster\u6a21\u5f0f\u7684\u96c6\u7fa4\uff0c\u8fd8\u5177\u5907\u4e00\u5b9a\u7684\u6545\u969c\u8f6c\u79fb\u80fd\u529b\uff0c\u6bd4\u5982\u5728\u4e0a\u9762\u7684\u67b6\u6784\u6a21\u5f0f\u4e0b\uff0c\u5f53\u96c6\u7fa4\u4e2d\u7684\u67d0\u4e2amaster\u8282\u70b9\u5b95\u673a\u4e4b\u540e\uff0c\u4f1a\u51fa\u73b0\u4ec0\u4e48\u60c5\u51b5\u5462\uff1f\u4e0b\u9762\u6211\u4eec\u6765\u6a21\u62df\u4e0b\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u770b\u770b\u6548\u679c\u5982\u4f55<\/p>\n<p><strong>1\u3001\u4f7f\u7528watch\u547d\u4ee4\u76d1\u63a7\u4e0b\u96c6\u7fa4\u7684\u72b6\u6001<\/strong><\/p>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u547d\u4ee4\u53ef\u4ee5\u5b9e\u65f6\u67e5\u770b\u96c6\u7fa4\u7684\u52a8\u6001\u65e5\u5fd7\u53d8\u5316<\/p>\n<pre>watch&nbsp;redis-cli&nbsp;-p&nbsp;7001&nbsp;cluster&nbsp;nodes<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4bc38f.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>2\u3001\u624b\u52a8\u5c067002\u5b9e\u4f8b\u7684master\u5b95\u673a<\/strong><\/p>\n<pre>redis-cli&nbsp;-p&nbsp;7002&nbsp;shutdown<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4c5e53.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4ece\u4e0a\u9762\u7684\u76d1\u63a7\u65e5\u5fd7\u4e0d\u96be\u53d1\u73b0\uff0c\u5f537002\u6302\u6389\u540e\uff0c\u8fc7\u4e86\u4e00\u4f1a\u513f\u4e0e\u96c6\u7fa4\u4ee5\u53ca\u5b83\u7684\u4ece\u8282\u70b98003\u5931\u53bb\u4e86\u8054\u7cfb\uff0c\u7136\u540e8003\u8fd9\u4e2a\u8282\u70b9\u5347\u7ea7\u4e3amaster\u8282\u70b9\uff1b<\/p>\n<p><strong>3\u3001\u624b\u52a8\u5c067002\u5b9e\u4f8b\u542f\u52a8\u8d77\u6765<\/strong><\/p>\n<pre>\/usr\/local\/soft\/redis\/redis60\/src\/redis-server&nbsp;\/usr\/local\/soft\/redis\/7002\/redis.conf<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4d08c5.jpg\" class=\"aligncenter\"><\/p>\n<p>\u518d\u6b21\u5206\u6790\u65e5\u5fd7\uff0c\u53ef\u4ee5\u770b\u5230\u8fd9\u65f6\u50197002\u53ea\u80fd\u4ee5slave\u7684\u8eab\u4efd\u52a0\u5165\u4e86\u96c6\u7fa4\uff0c\u4e8b\u5b9e\u4e0a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4ecd\u7136\u5e0c\u671b\u8fd9\u4e2a\u5b95\u673a\u7684\u8282\u70b9\u6062\u590d\u540e\u4f9d\u7136\u662f\u4e3b\u8282\u70b9\uff0c\u8fd9\u8be5\u600e\u4e48\u529e\u5462\uff1f\u8fd9\u5c31\u6d89\u53ca\u5230\u4e86\u624b\u52a8\u6545\u969c\u8f6c\u79fb\uff0c\u4e3b\u8981\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528redis-cli\u8fde\u63a57002\u8282\u70b9\uff1b<\/p>\n<\/li>\n<li>\n<p>\u6267\u884ccluster failover\u547d\u4ee4\uff1b<\/p>\n<\/li>\n<\/ul>\n<p>\u5728\u4e0a\u9762\u6267\u884c\u4e4b\u540e\u7684\u60c5\u51b5\u4e0b\uff0c7002\u662f\u4e00\u4e2aslave\u8282\u70b9<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4db050.jpg\" class=\"aligncenter\"><\/p>\n<p>\u6267\u884c CLUSTER FAILOVER \u547d\u4ee4\uff0c\u89c2\u5bdf\u65e5\u5fd7\u52a8\u6001\u53d8\u5316\uff0c\u901a\u8fc7\u65e5\u5fd7\u53d8\u5316\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u547d\u4ee4\u6267\u884c\u5b8c\u6bd5\u540e\uff0c7002\u5f88\u5feb\u5c31\u53d8\u6210\u4e86master\uff0c\u800c8003\u6210\u4e86slave\uff1b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230509\/16836105646459dbc4e535c.jpg\" class=\"aligncenter\"><\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u5c31\u5b8c\u6210\u4e86redis\u7684cluster\u6a21\u5f0f\u4e0b\u7684\u6545\u969c\u8f6c\u79fb\u7684\u8fc7\u7a0b<\/p>\n<h3>\u4f7f\u7528redistemplate\u8bbf\u95ee\u5206\u7247\u96c6\u7fa4<\/h3>\n<p>1\u3001\u5f15\u5165\u4f9d\u8d56<\/p>\n<pre>\n   \n    \n&nbsp;&nbsp;&nbsp;&nbsp;\n    \n     org.springframework.boot\n    \n&nbsp;&nbsp;&nbsp;&nbsp;\n    \n     spring-boot-starter-data-redis\n    \n   <\/pre>\n<p>2\u3001\u5c06\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u96c6\u7fa4\u5730\u5740\u4fee\u6539\u6210\u4e0b\u9762\u8fd9\u6837\u5373\u53ef<\/p>\n<pre>spring:\n&nbsp;&nbsp;redis:\n&nbsp;&nbsp;&nbsp;&nbsp;cluster:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nodes:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;\u96c6\u7fa4IP:7001\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;\u96c6\u7fa4IP:7002\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;\u96c6\u7fa4IP:7003\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;\u96c6\u7fa4IP:7004\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;\u96c6\u7fa4IP:8001\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;\u96c6\u7fa4IP:8002\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;\u96c6\u7fa4IP:8003<\/pre>\n<p>\u81f3\u4e8e\u5177\u4f53\u7684\u4ee3\u7801\u90e8\u5206\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u8fd9\u4e2a\u6d4b\u8bd5\u6848\u4f8b<\/p>\n<pre>\t@Autowired\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;RedisTemplate\n   \n    &nbsp;redisTemplate;\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/localhost:8083\/set?key=b&amp;value=123\n&nbsp;&nbsp;&nbsp;&nbsp;@GetMapping(\"\/set\")\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;set(@RequestParam&nbsp;String&nbsp;key,@RequestParam&nbsp;String&nbsp;value){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redisTemplate.opsForValue().set(key,value);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/localhost:8083\/get?key=b\n&nbsp;&nbsp;&nbsp;&nbsp;@GetMapping(\"\/get\")\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;get(@RequestParam&nbsp;String&nbsp;key){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;redisTemplate.opsForValue().get(key);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n   <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;Redis\u5206\u7247\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-201692","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201692","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=201692"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201692\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}