{"id":113531,"date":"2018-08-04T08:18:14","date_gmt":"2018-08-04T00:18:14","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=113531"},"modified":"2018-07-27T10:18:57","modified_gmt":"2018-07-27T02:18:57","slug":"loadbalancer-realize-theory","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/loadbalancer-realize-theory.html","title":{"rendered":"\u51e0\u79cd\u8d1f\u8f7d\u5747\u8861\u7684\u7b97\u6cd5\u539f\u7406\u53ca\u4ee3\u7801\u5b9e\u73b0"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8f6e\u8be2\u7b97\u6cd5\uff1a \u5c06\u63a5\u6536\u5230\u7684\u8bf7\u6c42\u4f9d\u6b21\u8f6c\u53d1\u5230\u540e\u7aef\u670d\u52a1\u5668\u4e0a\uff0c\u5b83\u5747\u8861\u5bf9\u5f85\uff08\u4e00\u89c6\u540c\u4ec1\uff09\u6240\u6709\u670d\u52a1\u5668\uff0c\u800c\u4e0d\u5173\u5fc3\u5f53\u524d\u670d\u52a1\u5668\u5b9e\u9645\u8fde\u63a5\u6570\u53ca\u5f53\u524d\u7cfb\u7edf\u8d1f\u8f7d\u3002 <\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u8f6e\u8be2\u7b97\u6cd5\uff1a \u5c06\u63a5\u6536\u5230\u7684\u8bf7\u6c42\u4f9d\u6b21\u8f6c\u53d1\u5230\u540e\u7aef\u670d\u52a1\u5668\u4e0a\uff0c\u5b83\u5747\u8861\u5bf9\u5f85\uff08\u4e00\u89c6\u540c\u4ec1\uff09\u6240\u6709\u670d\u52a1\u5668\uff0c\u800c\u4e0d\u5173\u5fc3\u5f53\u524d\u670d\u52a1\u5668\u5b9e\u9645\u8fde\u63a5\u6570\u53ca\u5f53\u524d\u7cfb\u7edf\u8d1f\u8f7d\u3002 \u8fd9\u91cc\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u8f6e\u8be2\u7cfb\u7edf\uff1a<\/p>\n

\r\n    public class RoundRobin { \r\n       static Integer  position = 0; \r\n       public static List initServerList() { \r\n           List<\/string> servers = new ArrayList<>(); \r\n           servers.add(\"192.168.10.00\"); \r\n           servers.add(\"192.168.10.01\"); \r\n           servers.add(\"192.168.10.02\"); \r\n           servers.add(\"192.168.10.03\"); \r\n           servers.add(\"192.168.10.04\"); \r\n           servers.add(\"192.168.10.05\"); \r\n           servers.add(\"192.168.10.06\"); \r\n           return servers; \r\n       } \r\n       public static String getServerUrl() { \r\n           \/\/\u65b0\u5efa\u7acb\u4e00\u4e2aList\u8d4b\u503c\uff0c\u907f\u514d\u670d\u52a1\u5668\u4e0a\u4e0b\u7ebf\u5bfc\u81f4\u7684\u5e76\u53d1\u95ee\u9898 \r\n           List<\/string> serverList = new ArrayList<>(); \r\n           serverList.addAll(initServerList()); \r\n           String server = null; \r\n           synchronized (position){ \r\n               if(position >= serverList.size()) { \r\n                   position = 0; \r\n               } \r\n               server = serverList.get(position); \r\n               position ++; \r\n           } \r\n           return server; \r\n       } \r\n       public static void main(String[] args) { \r\n           while (true){ \r\n               System.out.println(getServerUrl()); \r\n           } \r\n       } \r\n    } \r\n<\/string><\/pre>\n

\u5728\u5b9e\u9645\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u6211\u4eec\u8fd8\u5f97\u8003\u8651\u8bf8\u591a\u56e0\u7d20\uff0c\u6bd4\u5982\uff1a
\n\"\"
\n\u65b0\u589e\u670d\u52a1\u5668ip\u5982\u4f55\u5904\u7406\uff1f \u8fd9\u4e2a\u6bd4\u8f83\u7b80\u5355\uff0c\u76f4\u63a5\u6dfb\u52a0\u5230initServerList()\u5373\u53ef\u3002<\/p>\n

\u51fa\u73b0\u670d\u52a1\u5b95\u673a\u600e\u4e48\u529e\uff1f \u6bd4\u5982192.168.10.01 \u6240\u5728\u670d\u52a1\u5668\u6302\u6389\u4e86\uff0c\u8bf7\u6c42\u88ab\u8f6c\u53d1\u7ed9\u5b83\uff0c\u5c31\u4f1a\u62a5\u9519\u3002\u8fd9\u65f6\uff0c\u9700\u8981\u670d\u52a1\u7684\u6d88\u8d39\u8005\u8003\u8651\u5bb9\u9519\u5904\u7406\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6bd4\u5982\u518d\u53d1\u4e00\u6b21\u8bf7\u6c42\uff0c\u90a3\u5c31\u4f1a\u88ab\u8f6c\u53d1\u5230192.168.10.02 \u673a\u5668\u4e0a\uff0c\u6b63\u5e38\u3002 \u8be5\u65b9\u6cd5\u6700\u5927\u7f3a\u70b9\u662f\u5f15\u7528\u4e86\u60b2\u89c2\u9501 synchronized\uff0c\u5f71\u54cd\u7cfb\u7edf\u7684\u5e76\u53d1\u6027\u80fd\u3002<\/p>\n

\u6bcf\u53f0\u673a\u5668\u7684\u914d\u7f6e\u4e0d\u4e00\u6837\uff0c\u6709\u5355\u6838CPU\uff0c2G\u5185\u5b58\uff0c\u67098\u6838CPU\uff0c32G\u5185\u5b58\u3002\u8fd9\u79cd\u60c5\u5f62\u4e0b\uff0c\u4f7f\u7528\u4e0a\u8ff0\u8f6e\u8be2\uff0c\u90a3\u5c31\u4e0d\u516c\u5e73\u4e86\uff0c\u5bf9\u5f31\u914d\u7f6e\u673a\u5668\uff0c\u538b\u529b\u5f88\u5927\u3002 \u8fd9\u4e2a\uff0c\u6211\u4eec\u53ef\u4ee5\u5f15\u5165<\/p>\n

\u52a0\u6743\u8f6e\u8be2\uff1a \u6bcf\u53f0\u670d\u52a1\u5668\uff0c\u7ed9\u4e00\u4e2a\u6743\u91cd\u503c\uff0c\u6743\u503c\u9ad8\u7684\uff0c\u591a\u5206\u914d\u70b9\u513f\u8bf7\u6c42\uff0c\u6743\u503c\u5c11\u7684\uff0c\u5c11\u5206\u914d\u70b9\u513f\u8bf7\u6c42\uff0c\u3002 \u5b9e\u73b0\u601d\u8def\u4e5f\u5f88\u7b80\u5355\uff0c\u6839\u636e\u6743\u503c\uff0c\u91cd\u65b0\u6784\u5efa\u670d\u52a1\u5217\u8868\uff0c\u7136\u540e\u518d\u8f6e\u8be2\u3002\u4e0a\u4e2a\u56fe\u793a\uff1a<\/p>\n

\u5982\u4e0b\u662f\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n

\r\n    public class WeightRoundRobin { \r\n       static Integer  position = 0; \r\n       public static Map initServicesMap() { \r\n           Map<\/string> servicesMap = new HashMap<>(); \r\n           servicesMap.put(\"192.168.10.00\", 1); \r\n           servicesMap.put(\"192.168.10.02\", 3); \r\n           servicesMap.put(\"192.168.10.03\", 3); \r\n           servicesMap.put(\"192.168.10.04\", 5); \r\n           servicesMap.put(\"192.168.10.05\", 5); \r\n           servicesMap.put(\"192.168.10.06\", 5); \r\n           return servicesMap; \r\n       } \r\n       public static String getServerUrl() { \r\n           \/\/\u65b0\u5efa\u7acb\u4e00\u4e2aList\u8d4b\u503c\uff0c\u907f\u514d\u670d\u52a1\u5668\u4e0a\u4e0b\u7ebf\u5bfc\u81f4\u7684\u5e76\u53d1\u95ee\u9898 \r\n           Map<\/string> initMap = new HashMap<>(); \r\n           initMap = initServicesMap(); \r\n           Set<\/string> servicesSet = new HashSet<>(); \r\n           servicesSet.addAll(initMap.keySet()); \r\n           Iterator<\/string> servicesIterator = servicesSet.iterator(); \r\n           List<\/string> servicesList = new ArrayList<>(); \r\n           while (servicesIterator.hasNext()) { \r\n               String server = servicesIterator.next(); \r\n               Integer weight = initMap.get(server); \r\n               if(weight > 0) { \r\n                   for(int i=0; i= servicesList.size()) { \r\n                   position = 0; \r\n               } \r\n               server = servicesList.get(position); \r\n               position ++; \r\n           } \r\n           return server; \r\n       } \r\n       public static void main(String[] args) { \r\n           while (true){ \r\n               System.out.println(getServerUrl()); \r\n           } \r\n       } \r\n    } \r\n<\/weight><\/string><\/pre>\n
\u968f\u673a\u7b97\u6cd5:<\/strong><\/div>\n

\u987e\u540d\u601d\u4e49\uff1a\u73b0\u6709N\u4e2a\u670d\u52a1\u5668ip\u5730\u5740\uff0c\u8bf7\u6c42\u6765\u4e86\u540e\uff0c\u968f\u673a\u8f6c\u53d1\u5230\u67d0\u4e2a\u670d\u52a1\u5668\u4e0a\u3002\u4ece\u6982\u7387\u7684\u89d2\u5ea6\u6765\u8bf4\uff0c\u968f\u7740\u8bf7\u6c42\u6570\u7684\u589e\u591a\uff0c\u6700\u7ec8\u6bcf\u53f0\u670d\u52a1\u5668\u5206\u914d\u5230\u7684\u8bf7\u6c42\uff0c\u8fd1\u4f3c\u4e8e\u5747\u7b49\u3002\u8fd9\u5c31\u6bd4\u8f6e\u8be2\u7b97\u6cd5\u5c11\u4e86\u4e2a\u60b2\u89c2\u9501\uff0c\u5e76\u53d1\u6027\u80fd\u4e0a\uff0c\u6709\u4e86\u6781\u5927\u7684\u63d0\u5347\u3002<\/p>\n

\u5b9e\u73b0\u4e5f\u5f88\u7b80\u5355\uff1a<\/p>\n

\u5982\u4e0b\uff1a<\/p>\n

\r\n    public class RandomDemo { \r\n       public static List initServerList() { \r\n           List<\/string> servers = new ArrayList<>(); \r\n           servers.add(\"192.168.10.00\"); \r\n           servers.add(\"192.168.10.01\"); \r\n           servers.add(\"192.168.10.02\"); \r\n           servers.add(\"192.168.10.03\"); \r\n           servers.add(\"192.168.10.04\"); \r\n           servers.add(\"192.168.10.05\"); \r\n           servers.add(\"192.168.10.06\"); \r\n           return servers; \r\n       } \r\n       public static String getServerUrl() { \r\n           \/\/\u65b0\u5efa\u7acb\u4e00\u4e2aList\u8d4b\u503c\uff0c\u907f\u514d\u670d\u52a1\u5668\u4e0a\u4e0b\u7ebf\u5bfc\u81f4\u7684\u5e76\u53d1\u95ee\u9898 \r\n           List<\/string> serverList = new ArrayList<>(); \r\n           serverList.addAll(initServerList()); \r\n           int position = new Random().nextInt(serverList.size()); \r\n           return serverList.get(position); \r\n       } \r\n       public static void main(String[] args) { \r\n           while (true) { \r\n               System.out.println(getServerUrl()); \r\n           } \r\n       } \r\n    } \r\n<\/string><\/pre>\n

\u4f46\u4ed6\u4e5f\u6709\u4e0e\u7b80\u5355\u8f6e\u8be2\u7b97\u6cd5\u4e00\u6837\u7684\u95ee\u9898\uff1a<\/p>\n

\u5bf9\u4e8e\u4e0d\u540c\u6027\u80fd\u7684\u670d\u52a1\u5668\uff0c\u4f9d\u65e7\u4e00\u89c6\u540c\u4ec1\uff0c\u90a3\u5176\u5b9e\u662f\u4e0d\u516c\u5e73\u7684\u3002\u4f4e\u914d\u7f6e\uff0c\u5e94\u8be5\u5c11\u5206\u70b9\u8bf7\u6c42\u561b\u3002<\/p>\n

\u8fd9\u5c31\u6709\u4e86<\/p>\n

\u52a0\u6743\u968f\u673a\u7b97\u6cd5\uff0c\u5176\u5b9e\u73b0\u601d\u60f3\u540c \u52a0\u6743\u8f6e\u8be2\u7b97\u6cd5\u4e00\u6837\uff0c\u7ed9\u4e0d\u540c\u914d\u7f6e\u7684\u670d\u52a1\u5668\uff0c\u914d\u7f6e\u4e0d\u540c\u7684\u6743\u91cd\u503c\u3002\u4ee3\u7801\u5b9e\u73b0\u4e5f\u540c\u52a0\u6743\u8f6e\u8be2\u601d\u8def\u4e00\u6837\uff0c\u6784\u5efa\u51fa\u7b26\u5408\u6743\u91cd\u503c\u7684\u670d\u52a1\u96c6\u5408\u540e\uff0c\u518d\u8fdb\u884c\u968f\u673a\u9009\u53d6\uff0c\u8fd9\u91cc\u5c31\u4e0d\u5199\u4e86\uff0c\u7559\u7ed9\u5927\u5bb6\u81ea\u5df1\u53bb\u5199\u5427\u3002<\/p>\n

\u6e90\u5730\u5740\u54c8\u5e0c\uff08hashCode\uff09\u6cd5 \uff1a \u6839\u636e\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42ip\uff0c\u901a\u8fc7\u54c8\u5e0c\u8ba1\u7b97\uff0c\u5f97\u5230\u4e00\u4e2a\u6570\u503c\uff0c\u968f\u540e\u4e0e\u670d\u52a1\u5668\u5217\u8868\u4e2a\u6570\uff0c\u8fdb\u884c\u53d6\u6a21\u8ba1\u7b97\uff0c\u5f97\u5230\u8be5\u8bf7\u6c42 \u8bbf\u95ee\u670d\u52a1\u5668\u5217\u8868\u7684\u5e8f\u53f7\u3002\u8be5\u65b9\u6cd5\uff0c\u6709\u4e2a\u597d\u5904\u662f\uff0c\u5f53\u670d\u52a1\u5668\u5217\u8868\u4e0d\u53d8\u65f6\uff0c\u67d0\u4e2a\u5ba2\u6237\u7aef\uff0c\u4f1a\u59cb\u7ec8\u8bbf\u95ee\u67d0\u4e00\u4e2a\u56fa\u5b9a\u7684\u670d\u52a1\u5668\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u5ba2\u6237\u7aef--\u670d\u52a1\u5668\u4e4b\u95f4\uff0c\u6709\u72b6\u6001\u7684session\u3002<\/p>\n

\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n

\r\n    public class HashDemo {  \r\n       public static List initServerList() {  \r\n           List<\/string> servers = new ArrayList<>();  \r\n           servers.add(\"192.168.10.00\");  \r\n           servers.add(\"192.168.10.01\");  \r\n           servers.add(\"192.168.10.02\");  \r\n           servers.add(\"192.168.10.03\");  \r\n           servers.add(\"192.168.10.04\");  \r\n           servers.add(\"192.168.10.05\");  \r\n           servers.add(\"192.168.10.06\");  \r\n           return servers;  \r\n       }  \r\n       public static String getServerUrl() {  \r\n           \/\/\u65b0\u5efa\u7acb\u4e00\u4e2aList\u8d4b\u503c\uff0c\u907f\u514d\u670d\u52a1\u5668\u4e0a\u4e0b\u7ebf\u5bfc\u81f4\u7684\u5e76\u53d1\u95ee\u9898  \r\n           List<\/string> serverList = new ArrayList<>();  \r\n           serverList.addAll(initServerList());  \r\n           int requestIpHashCode = \"192.168.10.06.109\".hashCode();  \r\n           int position = requestIpHashCode % serverList.size();  \r\n           return serverList.get(position);  \r\n       }  \r\n       public static void main(String[] args) {  \r\n           while (true) {  \r\n               System.out.println(getServerUrl());  \r\n           }  \r\n       }  \r\n    }  \r\n<\/string><\/pre>\n
\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/server.51cto.com\/ManageDC-579822.htm<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttps:\/\/lrxjmw.cn\/loadbalancer-realize-theory.html<\/a>\u7f16\u8f91\uff1a\u70e8\u5b50\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u8f6e\u8be2\u7b97\u6cd5\uff1a \u5c06\u63a5\u6536\u5230\u7684\u8bf7\u6c42\u4f9d\u6b21\u8f6c\u53d1\u5230\u540e\u7aef\u670d\u52a1\u5668\u4e0a\uff0c\u5b83\u5747\u8861\u5bf9\u5f85\uff08\u4e00\u89c6\u540c\u4ec1\uff09\u6240\u6709\u670d\u52a1\u5668\uff0c\u800c\u4e0d\u5173\u5fc3\u5f53\u524d\u670d\u52a1\u5668\u5b9e […]<\/p>\n","protected":false},"author":63,"featured_media":113534,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-113531","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/113531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=113531"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/113531\/revisions"}],"predecessor-version":[{"id":113574,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/113531\/revisions\/113574"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/113534"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=113531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=113531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=113531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}