{"id":8920,"date":"2024-05-21T11:01:01","date_gmt":"2024-05-21T03:01:01","guid":{"rendered":""},"modified":"2024-05-21T11:01:01","modified_gmt":"2024-05-21T03:01:01","slug":"Day12_08_Redis\u6559\u7a0b\u4e4bRedis\u96c6\u7fa4\u539f\u7406\u53ca\u96c6\u7fa4\u642d\u5efa","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8920.html","title":{"rendered":"Day12_08_Redis\u6559\u7a0b\u4e4bRedis\u96c6\u7fa4\u539f\u7406\u53ca\u96c6\u7fa4\u642d\u5efa"},"content":{"rendered":"
\u4e3a\u4e86\u63d0\u9ad8\u7f51\u7ad9\u7684\u54cd\u5e94\u901f\u5ea6,\u6211\u4eec\u53ef\u4ee5\u628a\u7ecf\u5e38\u7528\u5230\u7684\u6570\u636e\u653e\u5230\u5185\u5b58\u4e2d\u8fdb\u884c\u7f13\u5b58,\u800c\u4e0d\u662f\u653e\u5230\u6570\u636e\u5e93\u4e2d\u8fdb\u884c\u6301\u4e45\u5316.Redis\u662f\u4e00\u4e2a\u5f88\u597d\u7684Cache\u5de5\u5177,\u5f53\u7136\u4e86\u8fd8\u6709Memcached.\u5728\u6211\u4eec\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d,\u70ed\u70b9\u6570\u636e\u5f80\u5f80\u5de8\u5927,\u6bd4\u5982\u5355\u70b9\u767b\u5f55\u3001\u7528\u6237\u6d4f\u89c8\u5546\u54c1\u7684\u4fe1\u606f\u3001\u7528\u6237\u4fe1\u606f\u3001\u7528\u6237\u6536\u85cf\u7684\u5546\u54c1\u4fe1\u606f\u3001\u77ed\u4fe1\u63d0\u9192\u6570\u636e\u7b49,\u4e5f\u90fd\u7528\u5230\u4e86redis.\u5982\u4f55\u4f7fredis\u53ef\u4ee5\u6a2a\u5411\u53ef\u4f38\u7f29\u6269\u5c55,\u8fd9\u9700\u8981\u7531\u591a\u53f0\u673a\u5668\u534f\u540c\u63d0\u4f9b\u670d\u52a1,\u4e00\u53f0\u6302\u6389\u4e86,\u53e6\u4e00\u53f0\u9a6c\u4e0a\u9876\u4e0a\u53bb,\u5373\u5206\u5e03\u5f0f\u7684redis\u96c6\u7fa4,\u5c31\u5bf9\u63d0\u5347\u7cfb\u7edf\u7684\u6027\u80fd\u975e\u5e38\u91cd\u8981.<\/p>\n
<\/p>\n
Redis\u96c6\u7fa4\u5728\u542f\u52a8\u7684\u65f6\u5019\u5c31\u81ea\u52a8\u5728\u591a\u4e2a\u8282\u70b9\u95f4\u5206\u597d\u7247,\u540c\u65f6\u63d0\u4f9b\u4e86\u5206\u7247\u4e4b\u95f4\u7684\u53ef\u7528\u6027:\u5f53\u4e00\u90e8\u5206redis\u8282\u70b9\u6545\u969c\u6216\u7f51\u7edc\u4e2d\u65ad,\u96c6\u7fa4\u4e5f\u80fd\u7ee7\u7eed\u5de5\u4f5c.\u4f46\u662f,\u5f53\u5927\u9762\u79ef\u7684\u8282\u70b9\u6545\u969c\u6216\u7f51\u7edc\u4e2d\u65ad(\u6bd4\u5982\u5927\u90e8\u5206\u7684\u4e3b\u8282\u70b9\u90fd\u4e0d\u53ef\u7528\u4e86),\u96c6\u7fa4\u5c31\u4e0d\u80fd\u4f7f\u7528\u4e86.<\/p>\n
\u6240\u4ee5,\u4ece\u5b9e\u7528\u6027\u7684\u89d2\u5ea6\u6765\u770b,Redis\u96c6\u7fa4\u63d0\u4f9b\u4ee5\u4e0b\u529f\u80fd:<\/p>\n
1\ufe0f\u20e3.\u81ea\u52a8\u628a\u6570\u636e\u5207\u5206\u5230\u591a\u4e2aRedis\u8282\u70b9\u4e2d;<\/p>\n<\/li>\n
2\ufe0f\u20e3.\u5f53\u4e00\u90e8\u5206\u8282\u70b9\u6302\u4e86\u6216\u4e0d\u53ef\u8fbe,\u96c6\u7fa4\u4f9d\u7136\u80fd\u7ee7\u7eed\u5de5\u4f5c.<\/p>\n<\/li>\n<\/ul>\n
\ufffc<\/p>\n
\u4e3a\u4e86\u4fdd\u8bc1\u5728\u90e8\u5206\u8282\u70b9\u6545\u969c\u6216\u7f51\u7edc\u4e0d\u901a\u65f6\u96c6\u7fa4\u4f9d\u7136\u80fd\u6b63\u5e38\u5de5\u4f5c,\u96c6\u7fa4\u4f7f\u7528\u4e86\u4e3b\u4ece\u6a21\u578b,\u6bcf\u4e2a\u54c8\u5e0c\u69fd\u6709\u4e00\u4e3b\u8282\u70b9 \u5230 N\u4e2a\u526f\u672c (N-1\u4e2a\u4ece\u8282\u70b9). \u5728\u6211\u4eec\u521a\u624d\u7684\u96c6\u7fa4\u4f8b\u5b50\u4e2d,\u6709A,B,C\u4e09\u4e2a\u8282\u70b9,\u5982\u679cB\u8282\u70b9\u6545\u969c\u96c6\u7fa4\u5c31\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86,\u56e0\u4e3a\uff22\u8282\u70b9\u4e2d\u7684\u54c8\u5e0c\u69fd\u6570\u636e\u6ca1\u6cd5\u64cd\u4f5c.\u4f46\u662f,\u5982\u679c\u6211\u4eec\u7ed9\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u589e\u52a0\u4e00\u4e2a\u4ece\u8282\u70b9,\u5c31\u53d8\u6210\u4e86: A,B,C\u4e09\u4e2a\u8282\u70b9\u662f\u4e3b\u8282\u70b9, A1, B1, C1 \u5206\u522b\u662f\u4ed6\u4eec\u7684\u4ece\u8282\u70b9,\u5f53B\u8282\u70b9\u5b95\u673a\u65f6,\u6211\u4eec\u7684\u96c6\u7fa4\u4e5f\u80fd\u6b63\u5e38\u8fd0\u4f5c. B1\u8282\u70b9\u662fB\u8282\u70b9\u7684\u526f\u672c,\u5982\u679cB\u8282\u70b9\u6545\u969c,\u96c6\u7fa4\u4f1a\u63d0\u5347B1\u4e3a\u4e3b\u8282\u70b9,\u4ece\u800c\u8ba9\u96c6\u7fa4\u7ee7\u7eed\u6b63\u5e38\u5de5\u4f5c.\u4f46\u662f,\u5982\u679cB\u548cB1\u540c\u65f6\u6545\u969c,\u96c6\u7fa4\u5c31\u4e0d\u80fd\u7ee7\u7eed\u5de5\u4f5c\u4e86.<\/p>\n
1\ufe0f\u20e3.\u6240\u6709\u7684redis\u8282\u70b9\u5f7c\u6b64\u4e92\u8054(PING-PONG\u673a\u5236),\u5185\u90e8\u4f7f\u7528\u4e8c\u8fdb\u5236\u534f\u8bae\u4f18\u5316\u4f20\u8f93\u901f\u5ea6\u548c\u5e26\u5bbd;<\/p>\n
2\ufe0f\u20e3.\u8282\u70b9\u7684fail\u662f\u5f53\u96c6\u7fa4\u4e2d\u8d85\u8fc7\u534a\u6570\u7684\u8282\u70b9\u68c0\u6d4b\u5931\u6548\u65f6\u624d\u751f\u6548;<\/p>\n
3\ufe0f\u20e3.\u5ba2\u6237\u7aef\u4e0eredis\u8282\u70b9\u76f4\u8fde,\u4e0d\u9700\u8981\u4e2d\u95f4proxy\u5c42.\u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u8fde\u63a5\u96c6\u7fa4\u4e2d\u6240\u6709\u7684\u8282\u70b9,\u8fde\u63a5\u96c6\u7fa4\u4e2d\u4efb\u4f55\u4e00\u4e2a\u53ef\u7528\u8282\u70b9\u5373\u53ef;<\/p>\n
4\ufe0f\u20e3.redis-cluster\u4f1a\u628a\u6240\u6709\u7684\u7269\u7406\u8282\u70b9\u6620\u5c04\u5230[0-16383]\u4e2aslot\u54c8\u5e0c\u69fd\u4e0a,cluster\u8d1f\u8d23\u7ef4\u62a4\u54c8\u5e0c\u69fd\u91cc\u7684\u503c.Redis \u96c6\u7fa4\u4e2d\u5185\u7f6e\u4e86 16384 \u4e2a\u54c8\u5e0c\u69fd,\u5f53\u9700\u8981\u5728 Redis \u96c6\u7fa4\u4e2d\u653e\u7f6e\u4e00\u4e2a key-value \u65f6,redis \u5148\u5bf9 key \u4f7f\u7528 crc16 \u7b97\u6cd5\u7b97\u51fa\u4e00\u4e2a\u7ed3\u679c,\u7136\u540e\u628a\u7ed3\u679c\u5bf9 16384 \u6c42\u4f59\u6570,\u8fd9\u6837\u6bcf\u4e2a key \u90fd\u4f1a\u5bf9\u5e94\u4e00\u4e2a\u7f16\u53f7\u5728 0-16383 \u4e4b\u95f4\u7684\u54c8\u5e0c\u69fd,redis \u4f1a\u6839\u636e\u8282\u70b9\u6570\u91cf\u5927\u81f4\u5747\u7b49\u7684\u5c06\u54c8\u5e0c\u69fd\u6620\u5c04\u5230\u4e0d\u540c\u7684\u8282\u70b9.<\/p>\n
1\ufe0f\u20e3.Redis \u7684\u96c6\u7fa4\u5206\u7247\u662f\u5c06key\u6240\u5728\u7684\u7a7a\u95f4\u62c6\u5206\u621016384\u4e2a\u69fd\u4f4d,\u6bcf\u4e00\u4e2a\u8282\u70b9\u8d1f\u8d23\u5176\u4e2d\u4e00\u90e8\u5206\u69fd\u4f4d;<\/p>\n
2\ufe0f\u20e3.Redis\u63d0\u4f9b\u4e00\u5b9a\u7a0b\u5ea6\u7684\u53ef\u7528\u6027,\u53ef\u4ee5\u5728\u67d0\u4e2a\u8282\u70b9\u5b95\u673a\u6216\u8005\u4e0d\u53ef\u8fbe\u7684\u60c5\u51b5\u4e0b\u7ee7\u7eed\u5904\u7406\u547d\u4ee4;<\/p>\n
3\ufe0f\u20e3.Redis \u96c6\u7fa4\u4e2d\u4e0d\u5b58\u5728\u4e2d\u5fc3(central)\u8282\u70b9\u6216\u8005\u4ee3\u7406(proxy)\u8282\u70b9,\u96c6\u7fa4\u7684\u5176\u4e2d\u4e00\u4e2a\u4e3b\u8981\u8bbe\u8ba1\u76ee\u6807\u662f\u8fbe\u5230\u7ebf\u6027\u53ef\u6269\u5c55\u6027(linear scalability);<\/p>\n
4\ufe0f\u20e3.Redis\u96c6\u7fa4\u8981\u60f3\u6b63\u5e38\u5de5\u4f5c,\u5fc5\u987b\u8981\u4e09\u4e2a\u4ee5\u4e0a\u7684\u4e3b\u8282\u70b9,\u5728\u6211\u4eec\u642d\u5efa\u7684redis\u96c6\u7fa4\u73af\u5883\u4e2d,\u4e09\u4e2a\u4e3b\u8282\u70b9\u6bcf\u4e2a\u90fd\u9700\u8981\u6709\u4e00\u4e2a\u4ece\u8282\u70b9,\u6240\u4ee5\u4e00\u5171\u6709\u516d\u4e2a\u8282\u70b9,\u4e5f\u5c31\u662f\u8bf4\u9700\u8981\u5f00\u542f6\u4e2aredis\u670d\u52a1.<\/p>\n
\ufffc<\/p>\n
Redis\u96c6\u7fa4\u4e0d\u662f\u4f7f\u7528\u4e00\u81f4\u6027\u54c8\u5e0c,\u800c\u662f\u4f7f\u7528\u54c8\u5e0c\u69fd,\u6574\u4e2aRedis\u96c6\u7fa4\u670916384\u4e2a\u54c8\u5e0c\u69fd<\/strong>.\u51b3\u5b9a\u4e00\u4e2akey\u5e94\u8be5\u5206\u914d\u5230\u54ea\u4e2a\u69fd\u7684\u7b97\u6cd5\u662f:\u8ba1\u7b97\u8be5key\u7684CRC16\u7ed3\u679c\u518d\u6a2116384 .<\/p>\n \u96c6\u7fa4\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u8d1f\u8d23\u4e00\u90e8\u5206\u54c8\u5e0c\u69fd,\u6bd4\u5982\u96c6\u7fa4\u4e2d\u6709\uff13\u4e2a\u8282\u70b9,\u5219:<\/p>\n \u8282\u70b9\uff21\u5b58\u50a8\u7684\u54c8\u5e0c\u69fd\u8303\u56f4\u662f: 0 \u2013 5500;<\/p>\n<\/li>\n \u8282\u70b9\uff22\u5b58\u50a8\u7684\u54c8\u5e0c\u69fd\u8303\u56f4\u662f:5501 \u2013 11000;<\/p>\n<\/li>\n \u8282\u70b9\uff23\u5b58\u50a8\u7684\u54c8\u5e0c\u69fd\u8303\u56f4\u662f: 11001 \u2013 16384.<\/p>\n<\/li>\n<\/ul>\n \u8fd9\u6837\u7684\u5206\u5e03\u65b9\u5f0f\u65b9\u4fbf\u8282\u70b9\u7684\u6dfb\u52a0\u548c\u5220\u9664.\u6bd4\u5982,\u6b64\u65f6\u5982\u679c\u9700\u8981\u65b0\u589e\u4e00\u4e2a\u8282\u70b9\uff24,\u53ea\u9700\u8981\u628a\uff21\u3001\uff22\u3001\uff23\u4e2d\u7684\u90e8\u5206\u54c8\u5e0c\u69fd\u6570\u636e\u79fb\u5230\uff24\u8282\u70b9.\u540c\u6837,\u5982\u679c\u5e0c\u671b\u5728\u96c6\u7fa4\u4e2d\u5220\u9664\uff21\u8282\u70b9,\u53ea\u9700\u8981\u628a\uff21\u8282\u70b9\u4e0a\u54c8\u5e0c\u69fd\u7684\u6570\u636e\u79fb\u5230\uff22\u548c\uff23\u8282\u70b9,\u5f53\uff21\u8282\u70b9\u7684\u6570\u636e\u5168\u90e8\u88ab\u79fb\u8d70\u540e,\uff21\u8282\u70b9\u5c31\u53ef\u4ee5\u5b8c\u5168\u4ece\u96c6\u7fa4\u4e2d\u5220\u9664.<\/p>\n \u56e0\u4e3a\u628a\u54c8\u5e0c\u69fd\u4ece\u4e00\u4e2a\u8282\u70b9\u79fb\u5230\u53e6\u4e00\u4e2a\u8282\u70b9\u662f\u4e0d\u9700\u8981\u505c\u673a\u7684,\u6240\u4ee5\u589e\u52a0\u6216\u5220\u9664\u8282\u70b9,\u6216\u66f4\u6539\u8282\u70b9\u4e0a\u7684\u54c8\u5e0c\u69fd,\u4e5f\u662f\u4e0d\u9700\u8981\u505c\u673a\u7684.<\/p>\n \u5982\u679c\u591a\u4e2akey\u90fd\u5c5e\u4e8e\u4e00\u4e2a\u54c8\u5e0c\u69fd,\u96c6\u7fa4\u652f\u6301\u901a\u8fc7\u4e00\u4e2a\u547d\u4ee4(\u6216\u4e8b\u52a1,\u6216lua\u811a\u672c)\u540c\u65f6\u64cd\u4f5c\u8fd9\u4e9bkey.\u901a\u8fc7\u201c\u54c8\u5e0c\u6807\u7b7e\u201d<\/strong>\u7684\u6982\u5ff5,\u7528\u6237\u53ef\u4ee5\u8ba9\u591a\u4e2akey\u5206\u914d\u5230\u540c\u4e00\u4e2a\u54c8\u5e0c\u69fd.\u54c8\u5e0c\u6807\u7b7e\u5728\u96c6\u7fa4\u8be6\u7ec6\u6587\u6863\u4e2d\u6709\u63cf\u8ff0,\u8fd9\u91cc\u505a\u4e2a\u7b80\u5355\u4ecb\u7ecd: \u5982\u679ckey\u542b\u6709\u5927\u62ec\u53f7\u201d{}\u201d,\u5219\u53ea\u6709\u5927\u62ec\u53f7\u4e2d\u7684\u5b57\u7b26\u4e32\u4f1a\u53c2\u4e0e\u54c8\u5e0c,\u6bd4\u5982\u201dthis{foo}\u201d\u548c\u201danother{foo}\u201d\u8fd9\uff12\u4e2akey\u4f1a\u5206\u914d\u5230\u540c\u4e00\u4e2a\u54c8\u5e0c\u69fd,\u6240\u4ee5\u53ef\u4ee5\u5728\u4e00\u4e2a\u547d\u4ee4\u4e2d\u540c\u65f6\u64cd\u4f5c\u4ed6\u4eec.<\/p>\n Redis\u96c6\u7fa4\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u9700\u8981\u5efa\u7acb2\u4e2aTCP\u8fde\u63a5,\u6240\u4ee5\u9700\u8981\u76d1\u542c2\u4e2a\u7aef\u53e3: \u4e00\u4e2a\u7aef\u53e3\u79f0\u4e4b\u4e3a\u201c\u5ba2\u6237\u7aef\u7aef\u53e3\u201d,\u7528\u4e8e\u63a5\u53d7\u5ba2\u6237\u7aef\u6307\u4ee4,\u4e0e\u5ba2\u6237\u7aef\u4ea4\u4e92,\u6bd4\u59826379;\u53e6\u4e00\u4e2a\u7aef\u53e3\u79f0\u4e4b\u4e3a\u201c\u96c6\u7fa4\u603b\u7ebf\u7aef\u53e3\u201d,\u662f\u5728\u5ba2\u6237\u7aef\u7aef\u53e3\u53f7\u4e0a\u52a010000,\u6bd4\u598216379,\u7528\u4e8e\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u4e8c\u8fdb\u5236\u534f\u8bae\u901a\u8baf.<\/p>\n \u5404\u8282\u70b9\u901a\u8fc7\u96c6\u7fa4\u603b\u7ebf\u68c0\u6d4b\u5b95\u673a\u8282\u70b9\u3001\u66f4\u65b0\u914d\u7f6e\u3001\u6545\u969c\u8f6c\u79fb\u9a8c\u8bc1\u7b49.\u5ba2\u6237\u7aef\u53ea\u80fd\u4f7f\u7528\u5ba2\u6237\u7aef\u7aef\u53e3,\u4e0d\u80fd\u4f7f\u7528\u96c6\u7fa4\u603b\u7ebf\u7aef\u53e3.\u8bf7\u786e\u4fdd\u4f60\u7684\u9632\u706b\u5899\u5141\u8bb8\u6253\u5f00\u8fd9\u4e24\u4e2a\u7aef\u53e3,\u5426\u5219Redis\u96c6\u7fa4\u6ca1\u6cd5\u5de5\u4f5c.\u5ba2\u6237\u7aef\u7aef\u53e3\u548c\u96c6\u7fa4\u603b\u7ebf\u7aef\u53e3\u4e4b\u95f4\u7684\u5dee\u503c\u662f\u56fa\u5b9a\u7684,\u96c6\u7fa4\u603b\u7ebf\u7aef\u53e3\u6bd4\u5ba2\u6237\u7aef\u7aef\u53e3\u9ad810000.<\/p>\n \u6ce8\u610f,\u5173\u4e8e\u96c6\u7fa4\u7684\uff12\u4e2a\u7aef\u53e3:<\/p>\n 1\ufe0f\u20e3.\u5ba2\u6237\u7aef\u7aef\u53e3(\u4e00\u822c\u662f6379)\u9700\u8981\u5bf9\u6240\u6709\u5ba2\u6237\u7aef\u548c\u96c6\u7fa4\u8282\u70b9\u5f00\u653e,\u56e0\u4e3a\u96c6\u7fa4\u8282\u70b9\u9700\u8981\u901a\u8fc7\u8be5\u7aef\u53e3\u8f6c\u79fb\u6570\u636e;<\/p>\n<\/li>\n 2\ufe0f\u20e3.\u96c6\u7fa4\u603b\u7ebf\u7aef\u53e3(\u4e00\u822c\u662f16379)\u53ea\u9700\u5bf9\u96c6\u7fa4\u4e2d\u7684\u6240\u6709\u8282\u70b9\u5f00\u653e.<\/p>\n<\/li>\n<\/ul>\n \u8fd9\uff12\u4e2a\u7aef\u53e3\u5fc5\u987b\u6253\u5f00,\u5426\u5219\u96c6\u7fa4\u6ca1\u6cd5\u6b63\u5e38\u5de5\u4f5c.<\/p>\n \u96c6\u7fa4\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u96c6\u7fa4\u603b\u7ebf\u7aef\u53e3\u4ea4\u4e92\u6570\u636e,\u4f7f\u7528\u7684\u534f\u8bae\u4e0d\u540c\u4e8e\u5ba2\u6237\u7aef\u7684\u534f\u8bae,\u662f\u4e8c\u8fdb\u5236\u534f\u8bae,\u8fd9\u53ef\u4ee5\u51cf\u5c11\u5e26\u5bbd\u548c\u5904\u7406\u65f6\u95f4.<\/p>\n Redis\u96c6\u7fa4\u4e0d\u80fd\u4fdd\u8bc1\u5f3a\u4e00\u81f4\u6027.<\/strong>\u4e00\u4e9b\u5df2\u7ecf\u5411\u5ba2\u6237\u7aef\u786e\u8ba4\u5199\u6210\u529f\u7684\u64cd\u4f5c,\u4f1a\u5728\u67d0\u4e9b\u4e0d\u786e\u5b9a\u7684\u60c5\u51b5\u4e0b\u4e22\u5931.<\/p>\n \u4ea7\u751f\u5199\u64cd\u4f5c\u4e22\u5931\u7684\u7b2c\u4e00\u4e2a\u539f\u56e0,\u662f\u56e0\u4e3a\u4e3b\u4ece\u8282\u70b9\u4e4b\u95f4\u4f7f\u7528\u4e86\u5f02\u6b65\u7684\u65b9\u5f0f\u6765\u540c\u6b65\u6570\u636e<\/strong>.<\/p>\n \u4e00\u4e2a\u5199\u64cd\u4f5c\u662f\u8fd9\u6837\u4e00\u4e2a\u6d41\u7a0b:<\/p>\n 1\ufe0f\u20e3.\u5ba2\u6237\u7aef\u5411\u4e3b\u8282\u70b9B\u53d1\u8d77\u5199\u7684\u64cd\u4f5c;<\/p>\n<\/li>\n 2\ufe0f\u20e3.\u4e3b\u8282\u70b9B\u56de\u5e94\u5ba2\u6237\u7aef\u5199\u64cd\u4f5c\u6210\u529f;<\/p>\n<\/li>\n 3\ufe0f\u20e3.\u4e3b\u8282\u70b9B\u5411\u5b83\u7684\u4ece\u8282\u70b9B1,B2,B3\u540c\u6b65\u8be5\u5199\u64cd\u4f5c.<\/p>\n<\/li>\n<\/ul>\n \u4ece\u4e0a\u9762\u7684\u6d41\u7a0b\u53ef\u4ee5\u770b\u51fa\u6765,\u4e3b\u8282\u70b9B\u5e76\u6ca1\u6709\u7b49\u4ece\u8282\u70b9B1,B2,B3\u5199\u5b8c\u4e4b\u540e\u518d\u56de\u590d\u5ba2\u6237\u7aef\u8fd9\u6b21\u64cd\u4f5c\u7684\u7ed3\u679c.\u6240\u4ee5,\u5982\u679c\u4e3b\u8282\u70b9B\u5728\u901a\u77e5\u5ba2\u6237\u7aef\u5199\u64cd\u4f5c\u6210\u529f\u4e4b\u540e,\u4f46\u540c\u6b65\u7ed9\u4ece\u8282\u70b9\u4e4b\u524d,\u4e3b\u8282\u70b9\uff22\u6545\u969c\u4e86,\u5176\u4e2d\u4e00\u4e2a\u6ca1\u6709\u6536\u5230\u8be5\u5199\u64cd\u4f5c\u7684\u4ece\u8282\u70b9\u4f1a\u664b\u5347\u6210\u4e3b\u8282\u70b9,\u8be5\u5199\u64cd\u4f5c\u5c31\u8fd9\u6837\u6c38\u8fdc\u4e22\u5931\u4e86.<\/p>\n \u5c31\u50cf\u4f20\u7edf\u7684\u6570\u636e\u5e93,\u5728\u4e0d\u6d89\u53ca\u5230\u5206\u5e03\u5f0f\u7684\u60c5\u51b5\u4e0b,\u5b83\u6bcf\u79d2\u5199\u56de\u78c1\u76d8.\u4e3a\u4e86\u63d0\u9ad8\u4e00\u81f4\u6027,\u53ef\u4ee5\u5728\u5199\u76d8\u5b8c\u6210\u4e4b\u540e\u518d\u56de\u590d\u5ba2\u6237\u7aef,\u4f46\u8fd9\u6837\u5c31\u8981\u635f\u5931\u6027\u80fd.\u8fd9\u79cd\u65b9\u5f0f\u5c31\u7b49\u4e8eRedis\u96c6\u7fa4\u4f7f\u7528\u540c\u6b65\u590d\u5236\u7684\u65b9\u5f0f.<\/p>\n \u57fa\u672c\u4e0a,\u5728\u6027\u80fd\u548c\u4e00\u81f4\u6027\u4e4b\u95f4,\u9700\u8981\u4e00\u4e2a\u6743\u8861.<\/p>\n \u5982\u679c\u771f\u7684\u9700\u8981,Redis\u96c6\u7fa4\u652f\u6301\u540c\u6b65\u590d\u5236\u7684\u65b9\u5f0f,\u901a\u8fc7WAIT\u6307\u4ee4\u6765\u5b9e\u73b0,\u8fd9\u53ef\u4ee5\u8ba9\u4e22\u5931\u5199\u64cd\u4f5c\u7684\u53ef\u80fd\u6027\u964d\u5230\u5f88\u4f4e.\u4f46\u5c31\u7b97\u4f7f\u7528\u4e86\u540c\u6b65\u590d\u5236\u7684\u65b9\u5f0f,Redis\u96c6\u7fa4\u4f9d\u7136\u4e0d\u662f\u5f3a\u4e00\u81f4\u6027\u7684.<\/strong>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u60c5\u51b5\u4e0b,\u6bd4\u5982\u4ece\u8282\u70b9\u5728\u4e0e\u4e3b\u8282\u70b9\u5931\u53bb\u8fde\u63a5\u4e4b\u540e\u88ab\u9009\u4e3a\u4e3b\u8282\u70b9,\u4e0d\u4e00\u81f4\u6027\u8fd8\u662f\u4f1a\u53d1\u751f.<\/p>\n \u8fd9\u79cd\u4e0d\u4e00\u81f4\u6027\u53d1\u751f\u7684\u60c5\u51b5\u662f\u8fd9\u6837\u7684,\u5f53\u5ba2\u6237\u7aef\u4e0e\u5c11\u6570\u7684\u8282\u70b9(\u81f3\u5c11\u542b\u6709\u4e00\u4e2a\u4e3b\u8282\u70b9)\u7f51\u7edc\u8054\u901a,\u4f46\u4ed6\u4eec\u4e0e\u5176\u4ed6\u5927\u591a\u6570\u8282\u70b9\u7f51\u7edc\u4e0d\u901a.\u6bd4\u5982\uff16\u4e2a\u8282\u70b9,A,B,C\u662f\u4e3b\u8282\u70b9,A1,B1,C1\u5206\u522b\u662f\u4ed6\u4eec\u7684\u4ece\u8282\u70b9,\u4e00\u4e2a\u5ba2\u6237\u7aef\u79f0\u4e4b\u4e3aZ1.<\/p>\n \u5f53\u7f51\u7edc\u51fa\u95ee\u9898\u65f6,\u4ed6\u4eec\u88ab\u5206\u6210\uff12\u7ec4\u7f51\u7edc,\u7ec4\u5185\u7f51\u7edc\u8054\u901a,\u4f46\u4e24\u4e2a\u7ec4\u4e4b\u95f4\u7684\u7f51\u7edc\u4e0d\u901a.\u5047\u8bbeA\/B\/C,A1\/B1\/C1\u5f7c\u6b64\u4e4b\u95f4\u662f\u8054\u901a\u7684,\u53e6\u4e00\u8fb9,B\u548cZ1\u7684\u7f51\u7edc\u662f\u8054\u901a\u7684.Z1\u53ef\u4ee5\u7ee7\u7eed\u5f80B\u53d1\u8d77\u5199\u64cd\u4f5c,\uff22\u4e5f\u63a5\u53d7Z1\u7684\u5199\u64cd\u4f5c.\u5f53\u7f51\u7edc\u6062\u590d\u65f6,\u5982\u679c\u8fd9\u4e2a\u65f6\u95f4\u95f4\u9694\u8db3\u591f\u77ed,\u96c6\u7fa4\u4ecd\u7136\u80fd\u7ee7\u7eed\u6b63\u5e38\u5de5\u4f5c.\u5982\u679c\u65f6\u95f4\u6bd4\u8f83\u957f,\u4ee5\u81f4B1\u5728\u5927\u591a\u6570\u7684\u8fd9\u8fb9\u88ab\u9009\u4e3a\u4e3b\u8282\u70b9,\u90a3\u521a\u624dZ1\u53d1\u7ed9\uff22\u7684\u5199\u64cd\u4f5c\u90fd\u5c06\u4e22\u5931.<\/p>\n \u6ce8\u610f,Z1\u7ed9\uff22\u53d1\u9001\u5199\u64cd\u4f5c\u662f\u6709\u4e00\u4e2a\u9650\u5236\u7684,\u5982\u679c\u65f6\u95f4\u957f\u5ea6\u8fbe\u5230\u4e86\u5927\u591a\u6570\u8282\u70b9\u90a3\u8fb9\u53ef\u4ee5\u9009\u51fa\u4e00\u4e2a\u65b0\u7684\u4e3b\u8282\u70b9\u65f6,\u5c11\u6570\u8fd9\u8fb9\u7684\u6240\u6709\u4e3b\u8282\u70b9\u90fd\u4e0d\u63a5\u53d7\u5199\u64cd\u4f5c.<\/p>\n \u8fd9\u4e2a\u65f6\u95f4\u7684\u914d\u7f6e,\u79f0\u4e4b\u4e3a\u8282\u70b9\u8d85\u65f6(node timeout),\u5bf9\u96c6\u7fa4\u6765\u8bf4\u975e\u5e38\u91cd\u8981,\u5f53\u8fbe\u5230\u4e86\u8fd9\u4e2a\u8282\u70b9\u8d85\u65f6\u7684\u65f6\u95f4\u4e4b\u540e,\u4e3b\u8282\u70b9\u88ab\u8ba4\u4e3a\u5df2\u7ecf\u5b95\u673a,\u53ef\u4ee5\u7528\u5b83\u7684\u4e00\u4e2a\u4ece\u8282\u70b9\u6765\u4ee3\u66ff.\u540c\u6837,\u5728\u8282\u70b9\u8d85\u65f6\u65f6,\u5982\u679c\u4e3b\u8282\u70b9\u4f9d\u7136\u4e0d\u80fd\u8054\u7cfb\u5230\u5176\u4ed6\u4e3b\u8282\u70b9,\u5b83\u5c06\u8fdb\u5165\u9519\u8bef\u72b6\u6001,\u4e0d\u518d\u63a5\u53d7\u5199\u64cd\u4f5c.<\/p>\n \ufffc<\/p>\n redis cluster\u5728\u8bbe\u8ba1\u7684\u65f6\u5019,\u5c31\u8003\u8651\u5230\u4e86\u53bb\u4e2d\u5fc3\u5316,\u53bb\u4e2d\u95f4\u4ef6.\u4e5f\u5c31\u662f\u8bf4,\u96c6\u7fa4\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u662f\u5e73\u7b49\u7684\u5173\u7cfb,\u90fd\u662f\u5bf9\u7b49\u7684,\u6bcf\u4e2a\u8282\u70b9\u90fd\u4fdd\u5b58\u5404\u81ea\u7684\u6570\u636e\u548c\u6574\u4e2a\u96c6\u7fa4\u7684\u72b6\u6001.\u6bcf\u4e2a\u8282\u70b9\u90fd\u548c\u5176\u4ed6\u6240\u6709\u8282\u70b9\u8fde\u63a5,\u800c\u4e14\u8fd9\u4e9b\u8fde\u63a5\u4fdd\u6301\u6d3b\u8dc3,\u8fd9\u6837\u5c31\u4fdd\u8bc1\u4e86\u6211\u4eec\u53ea\u9700\u8981\u8fde\u63a5\u96c6\u7fa4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u8282\u70b9,\u5c31\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u4ed6\u8282\u70b9\u7684\u6570\u636e.<\/p>\n Redis \u96c6\u7fa4\u6ca1\u6709\u5e76\u4f7f\u7528\u4f20\u7edf\u7684\u4e00\u81f4\u6027\u54c8\u5e0c\u6765\u5206\u914d\u6570\u636e,\u800c\u662f\u91c7\u7528\u53e6\u5916\u4e00\u79cd\u53eb\u505a\u54c8\u5e0c\u69fd(hash slot)\u7684\u65b9\u5f0f\u6765\u5206\u914d\u6570\u636e.redis cluster \u9ed8\u8ba4\u5206\u914d\u4e86 16384 \u4e2aslot,\u5f53\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a key \u7684\u65f6\u5019,redis\u96c6\u7fa4\u4f1a\u5148\u7528CRC16\u7b97\u6cd5\u5bf9key\u8ba1\u7b97\u51fa\u4e00\u4e2a\u7ed3\u679c,\u7136\u540e\u5229\u7528\u8be5\u7ed3\u679c\u6765\u5bf916384\u53d6\u6a21\u5f97\u5230\u6240\u5c5e\u7684slot\u4f4d,\u6700\u540e\u5c06\u8fd9\u4e2akey \u5206\u5230\u54c8\u5e0c\u69fd\u533a\u95f4\u6240\u5728\u7684\u8282\u70b9\u4e0a.\u5177\u4f53\u7b97\u6cd5\u5c31\u662f: CRC16(key) % 16384 .\u6240\u4ee5\u6211\u4eec\u5728\u6d4b\u8bd5\u7684\u65f6\u5019\u770b\u5230set \u548c get \u7684\u65f6\u5019,\u76f4\u63a5\u8df3\u8f6c\u5230\u4e867001\u7aef\u53e3\u6240\u5728\u7684\u8282\u70b9.<\/p>\n Redis \u96c6\u7fa4\u4f1a\u628a\u6570\u636e\u5b58\u5728\u4e00\u4e2a master \u8282\u70b9\u4e0a,\u7136\u540e\u5728\u8fd9\u4e2a master \u8282\u70b9\u548c\u5176\u5bf9\u5e94\u7684 slave \u8282\u70b9\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u540c\u6b65.\u5f53\u8bfb\u53d6\u6570\u636e\u65f6,\u4f1a\u6839\u636e\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u5f97\u5230\u5bf9\u5e94\u7684 master \u8282\u70b9\u6765\u83b7\u53d6\u6570\u636e,\u53ea\u6709\u5f53\u4e00\u4e2amaster \u6302\u6389\u4e4b\u540e,\u624d\u4f1a\u542f\u52a8\u4e00\u4e2a\u5bf9\u5e94\u7684 slave \u8282\u70b9,\u5c06\u5176\u63d0\u5347\u4e3amaster .<\/p>\n \u5fc5\u987b\u8981\u521b\u5efa3\u4e2a\u6216\u4ee5\u4e0a\u7684\u4e3b\u8282\u70b9,\u5426\u5219\u5728\u521b\u5efa\u96c6\u7fa4\u65f6\u4f1a\u5931\u8d25,\u5e76\u4e14\u5f53\u5b58\u6d3b\u7684\u4e3b\u8282\u70b9\u6570\u5c0f\u4e8e\u603b\u8282\u70b9\u6570\u7684\u4e00\u534a\u65f6,\u6574\u4e2a\u96c6\u7fa4\u5c31\u65e0\u6cd5\u63d0\u4f9b\u670d\u52a1\u4e86.<\/p>\n Redis\u96c6\u7fa4\u4e2d\u81f3\u5c11\u5e94\u8be5\u6709\u4e09\u4e2a\u8282\u70b9,\u8981\u4fdd\u8bc1\u96c6\u7fa4\u7684\u9ad8\u53ef\u7528,\u9700\u8981\u6bcf\u4e2a\u8282\u70b9\u6709\u4e00\u4e2a\u5907\u4efd\u673a,\u6240\u4ee5\u4e00\u4e2aRedis\u96c6\u7fa4\u81f3\u5c11\u9700\u89816\u53f0\u670d\u52a1\u5668.<\/p>\n \u642d\u5efa\u4f2a\u5206\u5e03\u5f0f\u96c6\u7fa4,\u53ef\u4ee5\u4f7f\u7528\u4e00\u53f0\u865a\u62df\u673a\u8fd0\u884c6\u4e2aredis\u5b9e\u4f8b,\u7136\u540e\u4fee\u6539redis\u7684\u7aef\u53e3\u53f77001-7006(\u81ea\u5b9a\u4e49\u7aef\u53e3,\u53ea\u8981\u4e0d\u548c\u5176\u4ed6\u7a0b\u5e8f\u91cd\u590d).<\/p>\n \u4e00\u822c\u60c5\u51b5\u4e0b,\u6211\u4eec\u9700\u89816\u53f0redis\u670d\u52a1\u5668,\u642d\u5efa\u51fa\u4e00\u4e2a\u5206\u5e03\u5f0f\u7684redis\u96c6\u7fa4.\u5728\u6211\u4eec\u7684\u6848\u4f8b\u4e2d,\u662f\u7528\u4e00\u53f0\u865a\u62df\u673a\u6a21\u62df\u51fa6\u4e2a\u8282\u70b9,\u5206\u522b\u521b\u5efa\u51fa3\u4e2amaster\u8282\u70b9,3\u4e2aslave\u8282\u70b9,\u8fd9\u51e0\u4e2a\u670d\u52a1\u5668\u5206\u522b\u8fd0\u884c\u5728\u4e0d\u540c\u7684\u7aef\u53e37001-7006\u4e0a,\u4ee5\u6b64\u6784\u5efa\u51fa\u4e00\u4e2a\u5206\u5e03\u5f0f\u7684\u4f2a\u96c6\u7fa4\u73af\u5883.<\/p>\n \u4ecb\u7ecd\u4e00\u4e0bredis\u96c6\u7fa4\u4e2dredis.conf\u6587\u4ef6\u4e2d\u7684\u5e38\u7528\u53c2\u6570.<\/p>\n cluster-enabled :<\/strong>\u5982\u679c\u914d\u7f6e\u201dyes\u201d\u5219\u5f00\u542f\u96c6\u7fa4\u529f\u80fd,\u6b64redis\u5b9e\u4f8b\u4f5c\u4e3a\u96c6\u7fa4\u7684\u4e00\u4e2a\u8282\u70b9,\u5426\u5219,\u5b83\u662f\u4e00\u4e2a\u666e\u901a\u7684\u5355\u4e00\u7684redis\u5b9e\u4f8b;<\/p>\n<\/li>\n cluster-config-file :<\/strong>\u6ce8\u610f:\u867d\u7136\u6b64\u914d\u7f6e\u7684\u540d\u5b57\u53eb\u201c\u96c6\u7fa4\u914d\u7f6e\u6587\u4ef6\u201d,\u4f46\u662f\u6b64\u914d\u7f6e\u6587\u4ef6\u4e0d\u80fd\u4eba\u5de5\u7f16\u8f91,\u5b83\u662f\u96c6\u7fa4\u8282\u70b9\u81ea\u52a8\u7ef4\u62a4\u7684\u6587\u4ef6,\u4e3b\u8981\u7528\u4e8e\u8bb0\u5f55\u96c6\u7fa4\u4e2d\u6709\u54ea\u4e9b\u8282\u70b9\u3001\u4ed6\u4eec\u7684\u72b6\u6001\u4ee5\u53ca\u4e00\u4e9b\u6301\u4e45\u5316\u53c2\u6570\u7b49,\u65b9\u4fbf\u5728\u91cd\u542f\u65f6\u6062\u590d\u8fd9\u4e9b\u72b6\u6001.\u901a\u5e38\u662f\u5728\u6536\u5230\u8bf7\u6c42\u4e4b\u540e\u8fd9\u4e2a\u6587\u4ef6\u5c31\u4f1a\u88ab\u66f4\u65b0.<\/p>\n<\/li>\n cluster-node-timeout :<\/strong> \u8fd9\u662f\u96c6\u7fa4\u4e2d\u7684\u8282\u70b9\u80fd\u591f\u5931\u8054\u7684\u6700\u5927\u65f6\u95f4,\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4,\u8be5\u8282\u70b9\u5c31\u4f1a\u88ab\u8ba4\u4e3a\u6545\u969c.\u5982\u679c\u4e3b\u8282\u70b9\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\u8fd8\u662f\u4e0d\u53ef\u8fbe,\u5219\u7528\u5b83\u7684\u4ece\u8282\u70b9\u5c06\u542f\u52a8\u6545\u969c\u8fc1\u79fb,\u5347\u7ea7\u6210\u4e3b\u8282\u70b9.\u6ce8\u610f,\u4efb\u4f55\u4e00\u4e2a\u8282\u70b9\u5728\u8fd9\u4e2a\u65f6\u95f4\u4e4b\u5185\u5982\u679c\u8fd8\u662f\u6ca1\u6709\u8fde\u4e0a\u5927\u90e8\u5206\u7684\u4e3b\u8282\u70b9,\u5219\u6b64\u8282\u70b9\u5c06\u505c\u6b62\u63a5\u6536\u4efb\u4f55\u8bf7\u6c42.<\/p>\n<\/li>\n cluster-slave-validity-factor :<\/strong> \u5982\u679c\u8bbe\u7f6e\u6210\uff10,\u5219\u65e0\u8bba\u4ece\u8282\u70b9\u4e0e\u4e3b\u8282\u70b9\u5931\u8054\u591a\u4e45,\u4ece\u8282\u70b9\u90fd\u4f1a\u5c1d\u8bd5\u5347\u7ea7\u6210\u4e3b\u8282\u70b9.\u5982\u679c\u8bbe\u7f6e\u6210\u6b63\u6570,\u5219 cluster-migration-barrier :<\/strong>\u4e3b\u8282\u70b9\u9700\u8981\u7684\u6700\u5c0f\u4ece\u8282\u70b9\u6570,\u53ea\u6709\u8fbe\u5230\u8fd9\u4e2a\u6570,\u4e3b\u8282\u70b9\u5931\u8d25\u65f6,\u5b83\u4ece\u8282\u70b9\u624d\u4f1a\u8fdb\u884c\u8fc1\u79fb.\u66f4\u8be6\u7ec6\u4ecb\u7ecd\u53ef\u4ee5\u770b\u672c\u6559\u7a0b\u540e\u9762\u5173\u4e8e\u526f\u672c\u8fc1\u79fb\u5230\u90e8\u5206.<\/p>\n<\/li>\n cluster-require-full-coverage :<\/strong>\u5728\u90e8\u5206key\u6240\u5728\u7684\u8282\u70b9\u4e0d\u53ef\u7528\u65f6,\u5982\u679c\u6b64\u53c2\u6570\u8bbe\u7f6e\u4e3a\u201dyes\u201d(\u9ed8\u8ba4\u503c),\u5219\u6574\u4e2a\u96c6\u7fa4\u505c\u6b62\u63a5\u53d7\u64cd\u4f5c;\u5982\u679c\u6b64\u53c2\u6570\u8bbe\u7f6e\u4e3a\u201dno\u201d,\u5219\u96c6\u7fa4\u4f9d\u7136\u4e3a\u53ef\u8fbe\u8282\u70b9\u4e0a\u7684key\u63d0\u4f9b\u8bfb\u64cd\u4f5c.<\/p>\n<\/li>\n<\/ul>\n \u5728\/url\/local\/\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aredis-cluster\u76ee\u5f55.<\/p>\n \u7136\u540e\u5728redis-cluster\u76ee\u5f55\u4e0b,\u521b\u5efa\u51fa\u540d\u4e3aredis01,redis02,redis03,redis04,redis05,redis06\u76846\u4e2a\u76ee\u5f55,\u5e76\u5c06\/etc\/redis\/redis.conf\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u62f7\u8d1d\u5230\u8fd96\u4e2a\u76ee\u5f55\u4e2d.<\/p>\n <\/p>\n 1\ufe0f\u20e3. redis01\/redis.conf\u5185\u5bb9<\/p>\n 2\ufe0f\u20e3. redis02\/redis.conf\u5185\u5bb9<\/p>\n 3\ufe0f\u20e3. redis03\/redis.conf\u5185\u5bb9<\/p>\n 4\ufe0f\u20e3. redis04\/redis.conf\u5185\u5bb9<\/p>\n 5\ufe0f\u20e3. redis05\/redis.conf\u5185\u5bb9<\/p>\n 6\ufe0f\u20e3. redis06\/redis.conf\u5185\u5bb9<\/p>\n \u4e00\u4e2a\u8282\u70b9\u4e00\u4e2a\u8282\u70b9\u7684\u542f\u52a8\u547d\u4ee4:<\/p>\n \u5982\u679c\u6211\u4eec\u4e00\u4e2a\u8282\u70b9\u4e00\u4e2a\u8282\u70b9\u8fdb\u884c\u542f\u52a8,\u4f1a\u7279\u522b\u9ebb\u70e6,\u6240\u4ee5\u6211\u4eec\u63a5\u4e0b\u6765\u521b\u5efa\u4e00\u4e2a\u811a\u672c\u6587\u4ef6,\u7528\u8fd9\u4e2a\u811a\u672c\u6587\u4ef6\u540c\u65f6\u542f\u52a8\u6240\u6709\u7684redis\u8282\u70b9.<\/p>\n \u4e3a\u4e86\u65b9\u4fbf\u6bcf\u4e2aredis\u7684\u542f\u52a8,\u53ef\u4ee5\u521b\u5efa\u542f\u52a8redis\u670d\u52a1\u7684shell\u811a\u672c.<\/p>\n Redis \u5b98\u65b9\u63d0\u4f9b\u4e86redis-trib.rb\u8fd9\u4e2a\u5de5\u5177,\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e2a\u547d\u4ee4\u5373\u53ef\u5b8c\u6210redis\u96c6\u7fa4\u5b89\u88c5.<\/p>\n \u53d1\u73b0redis-trib.rb\u6587\u4ef6\u5728 \ufffc<\/p>\n \ufffc<\/p>\n \u6ce8\u610f:\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9700\u8981\u8f93\u5165 yes ,\u7136\u540e\u51fa\u73b0\u5982\u4e0b\u5185\u5bb9,\u8bf4\u660e\u5b89\u88c5\u6210\u529f.<\/p>\n \ufffc<\/p>\n \u6bd4\u5982\u5728\u7b2c\u4e00\u53f0\u673a\u5668\u4e0a\u8fde\u63a5\u96c6\u7fa4\u76847003\u7aef\u53e3\u6240\u5728\u7684\u8282\u70b9,\u5728\u53e6\u5916\u4e00\u53f0\u673a\u5668\u4e0a\u8fde\u63a57005\u8282\u70b9,\u8fde\u63a5\u65b9\u5f0f\u4e3a redis-cli -h 127.0.0.1 -c -p 7003,\u52a0\u53c2\u6570 -c \u53ef\u8fde\u63a5\u5230\u96c6\u7fa4,\u56e0\u4e3a\u4e0a\u9762 redis.conf \u5c06 bind \u6539\u4e3a\u4e86ip\u5730\u5740,\u6240\u4ee5 -h \u53c2\u6570\u4e0d\u53ef\u4ee5\u7701\u7565.<\/p>\n \u6ce8\u610f:\u4e00\u5b9a\u8981\u6dfb\u52a0 -c \u53c2\u6570,\u624d\u80fd\u8fde\u63a5\u5230redis\u96c6\u7fa4!!!<\/p>\n <\/p>\n \u8bf4\u660e\u96c6\u7fa4\u8fd0\u4f5c\u6b63\u5e38.<\/p>\n \u89e3\u51b3\u529e\u6cd5:<\/strong> \u68c0\u67e5\u7aef\u53e3\u53f7\u662f\u5426\u6709\u9519,\u6211\u4e00\u5f00\u59cb\u521b\u5efa\u7684redis\u6587\u4ef6\u5939,\u5e76\u6709\u628aredis.conf\u4e0b\u7684\u7aef\u53e3\u53f7\u6539\u4e3a7000,\u4f46\u662f\u6d4b\u8bd5\u7684\u65f6\u5019\u53ea\u80fd\u8fde\u63a56379\u7684\u7aef\u53e3,\u660e\u660e\u662f\u4fee\u6539\u8fc7\u4e86,\u6240\u4ee5\u4e2a\u4eba\u731c\u60f3,\u53ef\u80fdredis\u8fd9\u4e2a\u6587\u4ef6\u540d,\u7cfb\u7edf\u4f1a\u9ed8\u8ba4\u6253\u5f006379\u7aef\u53e3,\u6240\u4ee5\u6211\u6539\u540d\u4e3aredis1,\u7aef\u53e3\u53f7\u6539\u4e3a7001;<\/p>\n \u89e3\u51b3\u529e\u6cd5:<\/strong> \u6740\u6b7b\u6539redis\u8fdb\u7a0b, \u7528.\/redis.conf \u914d\u7f6e\u6587\u4ef6\u65b9\u5f0f\u767b\u5f55\u8fd9\u4e2a\u8282\u70b9,\u7136\u540e\u6267\u884cflushall\u548ccluster reset\u5373\u53ef,\u7136\u540e\u91cd\u65b0\u6267\u884c\u7fa4\u96c6\u811a\u672c\u547d\u4ee4: .\/redis-trib.rb create --replicas 1 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 127.0.0.1:7006<\/p>\n \u518d\u521b\u5efa\u96c6\u7fa4,\u5426\u5219\u4f1a\u51fa\u8fd9\u4e2a\u9519: \u8bf4\u660e\u4e0a\u6b21\u521b\u5efa\u7684\u96c6\u7fa4\u8fd8\u5728,\u5374\u4e0d\u4e3a\u7a7a,\u76f4\u63a5redis1\/redis-cli -h 127.0.0.1 -p 7002 -c\u8fdb\u5165\u8fde\u63a5\u5373\u53ef.<\/p>\n \u5982\u679c\u4f60\u8981\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u96c6\u7fa4,\u5c31\u8981\u8fdb\u53bb\u4e0a\u6b21\u7684\u90a3\u4e2a\u96c6\u7fa4\u8fde\u63a5\u4e2d,\u4f7f\u7528flushdb,\u6e05\u9664\u6570\u636e\u5e93,\u5728\u6267\u884c\u521b\u5efa.<\/p>\n \u539f\u56e0\u662f\u56e0\u4e3a\u5f53\u524d\u7684\u6570\u636e\u4e0d\u662f\u5b58\u50a8\u5728\u8fd9\u4e2a\u8282\u70b9\u4e0a,\u4f60\u9700\u8981get key\u83b7\u5f97\u8be5\u6570\u636e,\u5e76\u8df3\u5f80\u90a3\u4e2a\u5b58\u50a8\u6570\u636e\u7684\u771f\u6b63\u8282\u70b9,\u518d\u6267\u884cflushdb\u547d\u4ee4.<\/p>\n \u539f\u56e0\u662f\u6ca1\u529e\u6cd5\u5230\u8fbe\u5b58\u50a8\u6570\u636e\u7684\u90a3\u4e2a\u8282\u70b9,\u56e0\u4e3a\u4f60\u5ba2\u6237\u7aef\u8fde\u63a5\u65f6\u6ca1\u6709\u6307\u660e -c \u53c2\u6570,\u6240\u4ee5\u8fde\u63a5\u7684\u662f\u5355\u4e2a\u8282\u70b9.<\/p>\n \u8981\u6267\u884c\u8fd9\u4e2a \u8981\u786e\u4fdd\u4f7f\u7528 <\/p>\n","protected":false},"excerpt":{"rendered":"Day12_08_Redis\u6559\u7a0b\u4e4bRedis\u96c6\u7fa4\u539f\u7406\u53ca\u96c6\u7fa4\u642d\u5efaRedis\u96c6\u7fa4\u539f\u7406\u53ca\u96c6\u7fa4\u642d\u5efa\u4e00.Redis\u96c6\u7fa4\u7b80\u4ecb1.\u4e3a\u4ec0\u4e48\u8981\u642d\u5efaredis\u96c6\u7fa4?\u4e3a\u4e86\u63d0\u9ad8\u7f51...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8920"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8920"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8920\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\n
8. Redis\u96c6\u7fa4\u7684TCP\u7aef\u53e3<\/h4>\n
\n
9. Redis\u96c6\u7fa4\u7684\u4e00\u81f4\u6027\u4fdd\u8bc1<\/h4>\n
\n
\u4e8c. Redis\u96c6\u7fa4\u8fd0\u884c\u539f\u7406<\/h3>\n
\u6ce8\u610f<\/h4>\n
\u4e09. Redis\u96c6\u7fa4\u7684\u642d\u5efa<\/h3>\n
\u56db. Redis\u96c6\u7fa4\u642d\u5efa\u6b65\u9aa4<\/h3>\n
1. Redis\u96c6\u7fa4\u9700\u6c42<\/h4>\n
2. Redis\u96c6\u7fa4\u53c2\u6570\u914d\u7f6e<\/h4>\n
\n
cluster-node-timeout<\/code>\u4e58\u4ee5
cluster-slave-validity-factor<\/code>\u5f97\u5230\u7684\u65f6\u95f4,\u662f\u4ece\u8282\u70b9\u4e0e\u4e3b\u8282\u70b9\u5931\u8054\u540e,\u6b64\u4ece\u8282\u70b9\u6570\u636e\u6709\u6548\u7684\u6700\u957f\u65f6\u95f4,\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4,\u4ece\u8282\u70b9\u4e0d\u4f1a\u542f\u52a8\u6545\u969c\u8fc1\u79fb.\u5047\u8bbecluster-node-timeout=5,cluster-slave-validity-factor=10,\u5219\u5982\u679c\u4ece\u8282\u70b9\u8ddf\u4e3b\u8282\u70b9\u5931\u8054\u8d85\u8fc750\u79d2,\u6b64\u4ece\u8282\u70b9\u4e0d\u80fd\u6210\u4e3a\u4e3b\u8282\u70b9.\u6ce8\u610f,\u5982\u679c\u6b64\u53c2\u6570\u914d\u7f6e\u4e3a\u975e0,\u5c06\u53ef\u80fd\u51fa\u73b0\u7531\u4e8e\u67d0\u4e3b\u8282\u70b9\u5931\u8054\u5374\u6ca1\u6709\u4ece\u8282\u70b9\u80fd\u9876\u4e0a\u7684\u60c5\u51b5,\u4ece\u800c\u5bfc\u81f4\u96c6\u7fa4\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c.\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b,\u53ea\u6709\u7b49\u5230\u539f\u6765\u7684\u4e3b\u8282\u70b9\u91cd\u65b0\u56de\u5f52\u5230\u96c6\u7fa4,\u96c6\u7fa4\u624d\u6062\u590d\u8fd0\u4f5c.<\/p>\n<\/li>\n
3. \u521b\u5efa\u51fa6\u4e2aredis\u8282\u70b9<\/h4>\n
mkdir redis-cluster\n<\/code><\/pre>\n
#\u5207\u6362\u5230redis-cluster\u76ee\u5f55\ncd \/usr\/local\/redis-cluster\n\n#\u521b\u5efa6\u4e2a\u6587\u4ef6\u5939\nmkdir redis01 redis02 redis03 redis04 redis05 redis06\n\n#\u5207\u6362\u5230\/etc\/redis\/\u76ee\u5f55\ncd \/etc\/redis\/\n\n#\u590d\u5236redis.conf\u914d\u7f6e\u6587\u4ef6\ncp redis.conf redis-cluster\/redis01\/\ncp redis.conf redis-cluster\/redis02\/\ncp redis.conf redis-cluster\/redis03\/\ncp redis.conf redis-cluster\/redis04\/\ncp redis.conf redis-cluster\/redis05\/\ncp redis.conf redis-cluster\/redis06\/\n<\/code><\/pre>\n
4. \u5206\u522b\u4fee\u6539\u8fd96\u4e2a\u914d\u7f6e\u6587\u4ef6,\u4e3b\u8981\u4fee\u6539\u5982\u4e0b\u5185\u5bb9<\/h4>\n
port 7001\ncluster-enabled yes\n#\u6ce8\u610f:\u8be5\u6587\u4ef6\u4e00\u5b9a\u4e0d\u80fd\u540c\u540d\ncluster-config-file nodes01.conf\ncluster-node-timeout 5000\nappendonly yes\ndaemonize yes\n<\/code><\/pre>\n
port 7002\ncluster-enabled yes\n#\u6ce8\u610f:\u8be5\u6587\u4ef6\u4e00\u5b9a\u4e0d\u80fd\u540c\u540d\ncluster-config-file nodes02.conf\ncluster-node-timeout 5000\nappendonly yes\ndaemonize yes\n<\/code><\/pre>\n
port 7003\ncluster-enabled yes\n#\u6ce8\u610f:\u8be5\u6587\u4ef6\u4e00\u5b9a\u4e0d\u80fd\u540c\u540d\ncluster-config-file nodes03.conf\ncluster-node-timeout 5000\nappendonly yes\ndaemonize yes\n<\/code><\/pre>\n
port 7004\ncluster-enabled yes\n#\u6ce8\u610f:\u8be5\u6587\u4ef6\u4e00\u5b9a\u4e0d\u80fd\u540c\u540d\ncluster-config-file nodes04.conf\ncluster-node-timeout 5000\nappendonly yes\ndaemonize yes\n<\/code><\/pre>\n
port 7005\ncluster-enabled yes\n#\u6ce8\u610f:\u8be5\u6587\u4ef6\u4e00\u5b9a\u4e0d\u80fd\u540c\u540d\ncluster-config-file nodes05.conf\ncluster-node-timeout 5000\nappendonly yes\ndaemonize yes\n<\/code><\/pre>\n
port 7006\ncluster-enabled yes\n#\u6ce8\u610f:\u8be5\u6587\u4ef6\u4e00\u5b9a\u4e0d\u80fd\u540c\u540d\ncluster-config-file nodes06.conf\ncluster-node-timeout 5000\nappendonly yes\ndaemonize yes\n<\/code><\/pre>\n
5. \u542f\u52a8\u5404\u4e2a\u8282\u70b9(\u4e86\u89e3)<\/h4>\n
sudo redis-server \/usr\/local\/redis-cluster\/redis01\/redis.conf\n\nsudo redis-server \/usr\/local\/redis-cluster\/redis02\/redis.conf\n\nsudo redis-server \/usr\/local\/redis-cluster\/redis03\/redis.conf\n \nsudo redis-server \/usr\/local\/redis-cluster\/redis04\/redis.conf\n\nsudo redis-server \/usr\/local\/redis-cluster\/redis05\/redis.conf\n\nsudo redis-server \/usr\/local\/redis-cluster\/redis06\/redis.conf \n<\/code><\/pre>\n
6. \u521b\u5efa\u542f\u52a8\u6240\u6709\u8282\u70b9\u7684\u811a\u672c<\/h4>\n
#\u5207\u6362\u5230\u8be5\u76ee\u5f55\ncd \/usr\/local\/redis-cluster\/\n\n#\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\ntouch start-all-redis.sh\n\n#\u6ce8\u610f:\u4ee5\u4e0b\u5185\u5bb9\u662fshell\u811a\u672c\u7684\u5185\u5bb9.\n\n#!\/bin\/sh\nredis-server \/usr\/local\/redis-cluster\/redis01\/redis.conf\nredis-server \/usr\/local\/redis-cluster\/redis02\/redis.conf\nredis-server \/usr\/local\/redis-cluster\/redis03\/redis.conf\nredis-server \/usr\/local\/redis-cluster\/redis04\/redis.conf\nredis-server \/usr\/local\/redis-cluster\/redis05\/redis.conf\nredis-server \/usr\/local\/redis-cluster\/redis06\/redis.conf\n\n#\u66f4\u6539shell\u811a\u672c\u7684\u6743\u9650\nchmod +x start-all-redis.sh\n<\/code><\/pre>\n
7. \u521b\u5efa\u5173\u95ed\u6240\u6709redis\u670d\u52a1\u7684shell\u811a\u672c<\/h4>\n
#\u5207\u6362\u5230redis-cluster\u76ee\u5f55\ncd \/usr\/local\/redis-cluster\/\n\n#\u521b\u5efa\u4e00\u4e2a\u811a\u672c\u6587\u4ef6\ntouch stop-all-redis.sh\n\n#!\/bin\/sh\nredis-cli -h 127.0.0.1 -p 7001 shutdown\nredis-cli -h 127.0.0.1 -p 7002 shutdown\nredis-cli -h 127.0.0.1 -p 7003 shutdown\nredis-cli -h 127.0.0.1 -p 7004 shutdown\nredis-cli -h 127.0.0.1 -p 7005 shutdown\nredis-cli -h 127.0.0.1 -p 7006 shutdown\n\n#\u66f4\u6539shell\u811a\u672c\u7684\u6743\u9650\nchmod +x stop-all-redis.sh\n<\/code><\/pre>\n
8. \u5b89\u88c5\u96c6\u7fa4\u6240\u9700\u7684ruby\u73af\u5883<\/h4>\n
8.1 \u4f7f\u7528ruby\u811a\u672c\u642d\u5efa\u96c6\u7fa4,\u9700\u8981ruby\u7684\u8fd0\u884c\u73af\u5883.<\/h4>\n
apt-get install ruby\napt-get install rubygems\n<\/code><\/pre>\n
8.2 \u5b89\u88c5ruby\u811a\u672c\u8fd0\u884c\u4f7f\u7528\u7684\u5305<\/h4>\n
gem install redis\n<\/code><\/pre>\n
9. \u5229\u7528redis-trib.rb\u521b\u5efa\u96c6\u7fa4<\/h4>\n
9.1 \u67e5\u627eredis-trib.rb\u6587\u4ef6\u6240\u5728\u4f4d\u7f6e<\/h4>\n
find \/ -name \"redis-trib.rb\"\n<\/code><\/pre>\n
\/usr\/share\/doc\/redis-tools\/examples\/redis-trib.rb<\/code>\u4f4d\u7f6e\u4e0a.<\/p>\n
9.2 \u628aredis-trib.rb\u590d\u5236\u5230\/usr\/local\/redis-cluster\/\u76ee\u5f55\u4e0b<\/h4>\n
#\u5207\u6362\u76ee\u5f55\ncd \/usr\/share\/doc\/redis-tools\/examples\/\n\n#\u590d\u5236redis-trib.rb\u6587\u4ef6\ncp redis-trib.rb \/usr\/local\/redis-cluster\/\n<\/code><\/pre>\n
9.3 redis-trib.rb\u521b\u5efa\u96c6\u7fa4<\/h4>\n
.\/redis-trib.rb create --replicas 1 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 127.0.0.1:7006\n<\/code><\/pre>\n
\u5f53\u96c6\u7fa4\u5b89\u88c5\u597d\u4e4b\u540e,\u901a\u8fc7\u6211\u4eec\u4e4b\u524d\u521b\u5efa\u7684redis\u96c6\u7fa4\u542f\u52a8\u811a\u672c,\u5f00\u542fredis\u96c6\u7fa4\u8282\u70b9.<\/h4>\n
.\/start-all-redis.sh<\/code><\/pre>\n
10. \u68c0\u67e5redis\u542f\u52a8\u60c5\u51b5<\/h4>\n
ps -ef | grep redis\n\nroot 61020 1 0 02:14 ? 00:00:01 redis-server 127.0.0.1:7001 [cluster] \nroot 61024 1 0 02:14 ? 00:00:01 redis-server 127.0.0.1:7002 [cluster] \nroot 61029 1 0 02:14 ? 00:00:01 redis-server 127.0.0.1:7003 [cluster] \n \n#\u76d1\u542c\u7aef\u53e3 \nnetstat -tnlp | grep redis\ntcp 0 0 127.0.0.1:17001 0.0.0.0:* LISTEN 61020\/redis-server \ntcp 0 0 127.0.0.1:17002 0.0.0.0:* LISTEN 61024\/redis-server \ntcp 0 0 127.0.0.1:17003 0.0.0.0:* LISTEN 61029\/redis-server \ntcp 0 0 127.0.0.1:7001 0.0.0.0:* LISTEN 61020\/redis-server \ntcp 0 0 127.0.0.1:7002 0.0.0.0:* LISTEN 61024\/redis-server\ntcp 0 0 127.0.0.1:7003 0.0.0.0:* LISTEN 61029\/redis-server\n<\/code><\/pre>\n
11. \u96c6\u7fa4\u9a8c\u8bc1<\/h4>\n
\u4e94. \u96c6\u7fa4\u642d\u5efa\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898<\/h3>\n
1. Could not connect to Redis at 127.0.0.1:6379: Connection refused<\/h4>\n
2. \u6267\u884cset\u547d\u540d,\u51fa\u73b0ERR Slot 0 is already busy(Redis::CommandError)<\/h4>\n
3. \u7b2c\u4e8c\u6b21\u5f00\u542fredis\u96c6\u7fa4\u65f6,\u5c31\u4e0d\u7528\u518d\u4f7f\u7528\u8fd9\u4e2a\u547d\u4ee4<\/h4>\n
.\/redis-trib.rb create --replicas 1 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 127.0.0.1:7006\n<\/code><\/pre>\n
[ERR] Node 127.0.0.1:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.<\/p>\n4. \u5982\u679c\u4f7f\u7528flushdb,\u51fa\u73b0(error) READONLY You can\u2019t write against a read only slave.<\/h4>\n
5. \u5982\u679c\u51fa\u73b0\u8bbf\u95ee\u6570\u636e\u65f6,\u51fa\u73b0(error) MOVED 12539 127.0.0.1:7006<\/h4>\n
redis1\/redis-cli -h 127.0.0.1 -p 7002 -c<\/code> \u547d\u4ee4.<\/p>\n
6. \u542f\u52a8\u9879\u76eeredis\u5e94\u7528\u62a5\u9519JedisClusterException: CLUSTERDOWN Hash slot not served\u89e3\u51b3<\/h4>\n
.\/redis-trib.rb create --replicas 1<\/code> \u547d\u4ee4\u521b\u5efaredis\u96c6\u7fa4\u6210\u529f.<\/p>\n