{"id":198618,"date":"2020-08-24T08:09:20","date_gmt":"2020-08-24T00:09:20","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=198618"},"modified":"2020-08-19T17:10:03","modified_gmt":"2020-08-19T09:10:03","slug":"load-balance","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/load-balance.html","title":{"rendered":"\u7b80\u8ff0\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u8d1f\u8f7d\u5747\u8861"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u8d1f\u8f7d\u5747\u8861\u5efa\u7acb\u5728\u73b0\u6709\u7f51\u7edc\u7ed3\u6784\u4e4b\u4e0a\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5ec9\u4ef7\u6709\u6548\u900f\u660e\u7684\u65b9\u6cd5\u6269\u5c55\u7f51\u7edc\u8bbe\u5907\u548c\u670d\u52a1\u5668\u7684\u5e26\u5bbd\u3001\u589e\u52a0\u541e\u5410\u91cf\u3001\u52a0\u5f3a\u7f51\u7edc\u6570\u636e\u5904\u7406\u80fd\u529b\u3001\u63d0\u9ad8\u7f51\u7edc\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7528\u6027\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u4ec0\u4e48\u662f\u8d1f\u8f7d\u5747\u8861\uff1f<\/strong><\/div>\n \u8bb0\u5f97\u7b2c\u4e00\u6b21\u63a5\u89e6 Nginx \u662f\u5728\u5b9e\u9a8c\u5ba4\uff0c\u90a3\u65f6\u5019\u5728\u670d\u52a1\u5668\u90e8\u7f72\u7f51\u7ad9\u9700\u8981\u7528 Nginx \u3002Nginx \u662f\u4e00\u4e2a\u670d\u52a1\u7ec4\u4ef6\uff0c\u7528\u6765\u53cd\u5411\u4ee3\u7406\u3001\u8d1f\u8f7d\u5e73\u8861\u548c HTTP \u7f13\u5b58\u7b49\u3002\u90a3\u4e48\u8fd9\u91cc\u7684 \u8d1f\u8f7d\u5747\u8861 \u662f\u4ec0\u4e48\uff1f<\/p>\n \u8d1f\u8f7d\u5747\u8861\uff08LB\uff0cLoad Balance\uff09\uff0c\u662f\u4e00\u79cd\u6280\u672f\u89e3\u51b3\u65b9\u6848\u3002\u7528\u6765\u5728\u591a\u4e2a\u8d44\u6e90\uff08\u4e00\u822c\u662f\u670d\u52a1\u5668\uff09\u4e2d\u5206\u914d\u8d1f\u8f7d\uff0c\u8fbe\u5230\u6700\u4f18\u5316\u8d44\u6e90\u4f7f\u7528\uff0c\u907f\u514d\u8fc7\u8f7d\u3002<\/p>\n <\/p>\n \u8d44\u6e90\uff0c\u76f8\u5f53\u4e8e\u6bcf\u4e2a\u670d\u52a1\u5b9e\u4f8b\u7684\u6267\u884c\u64cd\u4f5c\u5355\u5143\uff0c\u8d1f\u8f7d\u5747\u8861\u5c31\u662f\u5c06\u5927\u91cf\u7684\u6570\u636e\u5904\u7406\u64cd\u4f5c\u5206\u644a\u5230\u591a\u4e2a\u64cd\u4f5c\u5355\u5143\u8fdb\u884c\u6267\u884c\uff0c\u7528\u6765\u89e3\u51b3\u4e92\u8054\u7f51\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u5927\u6d41\u91cf\u3001\u9ad8\u5e76\u53d1\u548c\u9ad8\u53ef\u7528\u7684\u95ee\u9898\u3002\u90a3\u4ec0\u4e48\u662f\u9ad8\u53ef\u7528\u5462\uff1f<\/p>\n \u4ec0\u4e48\u662f\u9ad8\u53ef\u7528\uff1f<\/strong><\/div>\n \u9996\u5148\u4e86\u89e3\u4ec0\u4e48\u662f\u9ad8\u53ef\u7528\uff1f<\/p>\n \u8fd9\u662f CAP \u5b9a\u7406\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u57fa\u7840\uff0c\u4e5f\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684 3 \u4e2a\u6307\u6807\uff1a<\/p>\n Consistency\uff08\u4e00\u81f4\u6027\uff09<\/p>\n Availability\uff08\u53ef\u7528\u6027\uff09<\/p>\n Partition tolerance\uff08\u5206\u533a\u5bb9\u9519\u6027\uff09<\/p>\n \u90a3\u9ad8\u53ef\u7528\uff08High Availability\uff09\u662f\u4ec0\u4e48\uff1f\u9ad8\u53ef\u7528\uff0c\u7b80\u79f0 HA\uff0c\u662f\u7cfb\u7edf\u4e00\u79cd\u7279\u5f81\u6216\u8005\u6307\u6807\uff0c\u901a\u5e38\u662f\u6307\uff0c\u63d0\u4f9b\u4e00\u5b9a\u6027\u80fd\u4e0a\u7684\u670d\u52a1\u8fd0\u884c\u65f6\u95f4\uff0c\u9ad8\u4e8e\u5e73\u5747\u6b63\u5e38\u65f6\u95f4\u6bb5\u3002\u53cd\u4e4b\uff0c\u6d88\u9664\u7cfb\u7edf\u670d\u52a1\u4e0d\u53ef\u7528\u7684\u65f6\u95f4\u3002<\/p>\n \u8861\u91cf\u7cfb\u7edf\u662f\u5426\u6ee1\u8db3\u9ad8\u53ef\u7528\uff0c\u5c31\u662f\u5f53\u4e00\u53f0\u6216\u8005\u591a\u53f0\u670d\u52a1\u5668\u5b95\u673a\u7684\u65f6\u5019\uff0c\u7cfb\u7edf\u6574\u4f53\u548c\u670d\u52a1\u4f9d\u7136\u6b63\u5e38\u53ef\u7528\u3002<\/p>\n \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e00\u4e9b\u77e5\u540d\u7684\u7f51\u7ad9\u4fdd\u8bc1 4 \u4e2a 9 \u4ee5\u4e0a\u7684\u53ef\u7528\u6027\uff0c\u4e5f\u5c31\u662f\u53ef\u7528\u6027\u8d85\u8fc7 99.99%\u3002\u90a3 0.01% \u5c31\u662f\u6240\u8c13\u6545\u969c\u65f6\u95f4\u7684\u767e\u5206\u6bd4\u3002\u6bd4\u5982\u7535\u5546\u7f51\u7ad9\u6709\u8d5e\uff0c\u670d\u52a1\u4e0d\u53ef\u7528\u4f1a\u9020\u6210\u5546\u5bb6\u635f\u5931\u91d1\u94b1\u548c\u7528\u6237\u3002\u90a3\u4e48\u5728\u63d0\u9ad8\u53ef\u7528\u6027\u57fa\u7840\u4e0a\u540c\u65f6\uff0c\u5bf9\u7cfb\u7edf\u5b95\u673a\u548c\u670d\u52a1\u4e0d\u53ef\u7528\u4f1a\u6709\u8865\u507f\u3002<\/p>\n <\/p>\n \u6bd4\u5982\u4e0b\u5355\u670d\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e26\u6709\u8d1f\u8f7d\u5747\u8861\u7684\u591a\u4e2a\u4e0b\u5355\u670d\u52a1\u5b9e\u4f8b\uff0c\u4ee3\u66ff\u5355\u4e00\u7684\u4e0b\u5355\u670d\u52a1\u5b9e\u4f8b\uff0c\u5373\u4f7f\u7528\u5197\u4f59\u7684\u65b9\u5f0f\u6765\u63d0\u9ad8\u53ef\u9760\u6027\u3002<\/p>\n \u603b\u800c\u8a00\u4e4b\uff0c\u8d1f\u8f7d\u5747\u8861\uff08Load Balance\uff09\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf\u67b6\u6784\u8bbe\u8ba1\u4e2d\u5fc5\u987b\u8003\u8651\u7684\u56e0\u7d20\u4e4b\u4e00\u3002\u4e00\u822c\u901a\u8fc7\u8d1f\u8f7d\u5747\u8861\uff0c\u5197\u4f59\u540c\u4e00\u4e2a\u670d\u52a1\u5b9e\u4f8b\u7684\u65b9\u5f0f\uff0c\u89e3\u51b3\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u5927\u6d41\u91cf\u3001\u9ad8\u5e76\u53d1\u548c\u9ad8\u53ef\u7528\u7684\u95ee\u9898\u3002\u8d1f\u8f7d\u5747\u8861\u6838\u5fc3\u5173\u952e\uff1a\u5728\u4e8e\u662f\u5426\u5206\u914d\u5747\u5300\u3002<\/p>\n \u5e38\u89c1\u7684\u8d1f\u8f7d\u5747\u8861\u6848\u4f8b<\/strong><\/div>\n \u573a\u666f1\uff1a\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u7f51\u5173\u8def\u7531\u5230\u5177\u4f53\u7684\u670d\u52a1\u5b9e\u4f8b hello\uff1a<\/p>\n \u2013 \u4e24\u4e2a\u76f8\u540c\u7684\u670d\u52a1\u5b9e\u4f8b hello service \uff0c\u4e00\u4e2a\u7aef\u53e3 8000 \uff0c\u53e6\u4e00\u4e2a\u7aef\u53e3 8082<\/p>\n \u2013 \u901a\u8fc7 Kong \u7684\u8d1f\u8f7d\u5747\u8861 LB \u529f\u80fd\uff0c\u8ba9\u8bf7\u6c42\u5747\u5300\u7684\u5206\u53d1\u5230\u4e24\u4e2a hello \u670d\u52a1\u5b9e\u4f8b<\/p>\n \u2013 Kong \u7684\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u7b97\u6cd5\u5f88\u591a\uff1a\u9ed8\u8ba4 weighted-round-robin \u7b97\u6cd5\uff0c\u8fd8\u6709 consumer: consumer id \u4f5c\u4e3a hash \u7b97\u6cd5\u8f93\u5165\u503c\u7b49<\/p>\n <\/p>\n \u573a\u666f2\uff1a\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0cA \u670d\u52a1\u8c03\u7528 B \u670d\u52a1\u7684\u96c6\u7fa4\u3002\u901a\u8fc7\u4e86 Ribbon \u5ba2\u6237\u7aef\u8d1f\u8f7d\u5747\u8861\u7ec4\u4ef6\uff1a<\/p>\n \u2013 \u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u7b97\u6cd5\u5e76\u4e0d\u9ad8\u7ea7\uff0c\u6700\u7b80\u5355\u7684\u662f\u968f\u673a\u9009\u62e9\u548c\u8f6e\u5faa<\/p>\n \u4e92\u8054\u7f51\u5206\u5e03\u5f0f\u7cfb\u7edf\u89e3\u51b3\u65b9\u6848<\/strong><\/div>\n <\/p>\n \u5e38\u89c1\u7684\u4e92\u8054\u7f51\u5206\u5e03\u5f0f\u7cfb\u7edf\u67b6\u6784\u5206\u4e3a\u51e0\u5c42\uff0c\u4e00\u822c\u5982\u4e0b\uff1a<\/p>\n \u5ba2\u6237\u7aef\u5c42\uff1a\u6bd4\u5982\u7528\u6237\u6d4f\u89c8\u5668\u3001APP \u7aef<\/p>\n \u53cd\u5411\u4ee3\u7406\u5c42\uff1a\u6280\u672f\u9009\u578b Nignx \u6216\u8005 F5 \u7b49<\/p>\n Web \u5c42\uff1a\u524d\u540e\u7aef\u5206\u79bb\u573a\u666f\u4e0b\uff0c Web \u7aef\u53ef\u4ee5\u7528 NodeJS \u3001 RN \u3001Vue<\/p>\n \u4e1a\u52a1\u670d\u52a1\u5c42\uff1a\u7528 Java \u3001Go\uff0c\u4e00\u822c\u4e92\u8054\u7f51\u516c\u53f8\uff0c\u6280\u672f\u65b9\u6848\u9009\u578b\u5c31\u662f SC \u6216\u8005 Spring Boot + Dubbo \u670d\u52a1\u5316<\/p>\n \u6570\u636e\u5b58\u50a8\u5c42\uff1aDB \u9009\u578b MySQL \uff0cCache \u9009\u578b Redis \uff0c\u641c\u7d22\u9009\u578b ES \u7b49<\/p>\n \u4e00\u4e2a\u8bf7\u6c42\u4ece\u7b2c 1 \u5c42\u5230\u7b2c 4 \u5c42\uff0c\u5c42\u5c42\u8bbf\u95ee\u90fd\u9700\u8981\u8d1f\u8f7d\u5747\u8861\u3002\u5373\u6bcf\u4e2a\u4e0a\u6e38\u8c03\u7528\u4e0b\u6e38\u591a\u4e2a\u4e1a\u52a1\u65b9\u7684\u65f6\u5019\uff0c\u9700\u8981\u5747\u5300\u8c03\u7528\u3002\u8fd9\u6837\u6574\u4f53\u7cfb\u7edf\u6765\u770b\uff0c\u5c31\u6bd4\u8f83\u8d1f\u8f7d\u5747\u8861<\/p>\n \u7b2c 1 \u5c42\uff1a\u5ba2\u6237\u7aef\u5c42 -> \u53cd\u5411\u4ee3\u7406\u5c42 \u7684\u8d1f\u8f7d\u5747\u8861<\/strong><\/span><\/div>\n \u5ba2\u6237\u7aef\u5c42 -> \u53cd\u5411\u4ee3\u7406\u5c42\u7684\u8d1f\u8f7d\u5747\u8861\u5982\u4f55\u5b9e\u73b0\u5462\uff1f<\/p>\n \u7b54\u6848\u662f\uff1aDNS \u7684\u8f6e\u8be2\u3002 DNS \u53ef\u4ee5\u901a\u8fc7 A \uff08Address\uff0c\u8fd4\u56de\u57df\u540d\u6307\u5411\u7684 IP \u5730\u5740\uff09\u8bbe\u7f6e\u591a\u4e2a IP \u5730\u5740\u3002\u6bd4\u5982\u8fd9\u91cc\u8bbf\u95ee bysocket.com \u7684 DNS \u914d\u7f6e\u4e86 ip1 \u548c ip2 \u3002\u4e3a\u4e86\u53cd\u5411\u4ee3\u7406\u5c42\u7684\u9ad8\u53ef\u7528\uff0c\u81f3\u5c11\u4f1a\u6709\u4e24\u6761 A \u8bb0\u5f55\u3002\u8fd9\u6837\u5197\u4f59\u7684\u4e24\u4e2a ip \u5bf9\u5e94\u7684 nginx \u670d\u52a1\u5b9e\u4f8b\uff0c\u9632\u6b62\u5355\u70b9\u6545\u969c\u3002<\/p>\n \u6bcf\u6b21\u8bf7\u6c42 bysocket.com \u57df\u540d\u7684\u65f6\u5019\uff0c\u901a\u8fc7 DNS \u8f6e\u8be2\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684 ip \u5730\u5740\uff0c\u6bcf\u4e2a ip \u5bf9\u5e94\u7684\u53cd\u5411\u4ee3\u7406\u5c42\u7684\u670d\u52a1\u5b9e\u4f8b\uff0c\u4e5f\u5c31\u662f nginx \u7684\u5916\u7f51ip\u3002\u8fd9\u6837\u53ef\u4ee5\u505a\u5230\u6bcf\u4e00\u4e2a\u53cd\u5411\u4ee3\u7406\u5c42\u5b9e\u4f8b\u5f97\u5230\u7684\u8bf7\u6c42\u5206\u914d\u662f\u5747\u8861\u7684\u3002<\/p>\n \u7b2c 2 \u5c42\uff1a\u53cd\u5411\u4ee3\u7406\u5c42 -> Web \u5c42 \u7684\u8d1f\u8f7d\u5747\u8861<\/strong><\/span><\/div>\n \u53cd\u5411\u4ee3\u7406\u5c42 -> Web \u5c42 \u7684\u8d1f\u8f7d\u5747\u8861\u5982\u4f55\u5b9e\u73b0\u5462\uff1f<\/p>\n \u662f\u901a\u8fc7\u53cd\u5411\u4ee3\u7406\u5c42\u7684\u8d1f\u8f7d\u5747\u8861\u6a21\u5757\u5904\u7406\u3002\u6bd4\u5982 nginx \u6709\u591a\u79cd\u5747\u8861\u65b9\u6cd5\uff1a<\/p>\n \u8bf7\u6c42\u8f6e\u8be2\u3002\u8bf7\u6c42\u6309\u65f6\u95f4\u987a\u5e8f\uff0c\u9010\u4e00\u5206\u914d\u5230 web \u5c42\u670d\u52a1\uff0c\u7136\u540e\u5468\u800c\u590d\u59cb\u3002\u5982\u679c web \u5c42\u670d\u52a1 down \u6389\uff0c\u81ea\u52a8\u5254\u9664<\/p>\n \r\nupstream web-server {\r\n server ip3;\r\n server ip4;\r\n}\r\n<\/pre>\n |