{"id":10007983,"date":"2023-06-25T13:58:43","date_gmt":"2023-06-25T05:58:43","guid":{"rendered":"https:\/\/ityet.com\/?p=10007983"},"modified":"2023-06-25T13:58:43","modified_gmt":"2023-06-25T05:58:43","slug":"%e6%88%91%e8%af%95%e5%9b%be%e7%bb%99%e4%bd%a0%e5%88%86%e4%ba%ab%e4%b8%80%e7%a7%8d%e8%87%aa%e9%80%82%e5%ba%94%e7%9a%84%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e3%80%82%e6%9c%89%e7%82%b9%e6%89%93%e8%84%91","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10007983","title":{"rendered":"\u6211\u8bd5\u56fe\u7ed9\u4f60\u5206\u4eab\u4e00\u79cd\u81ea\u9002\u5e94\u7684\u8d1f\u8f7d\u5747\u8861\u3002\u6709\u70b9\u6253\u8111\u58f3\uff0c\u4f46\u662f\u786e\u5b9e\u4e5f\u6709\u70b9\u5389\u5bb3\u3002"},"content":{"rendered":"<p>\u4f60\u597d\u5440\uff0c\u6211\u662f\u6b6a\u6b6a\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u6765\u76d8\u4e00\u4e2a\u6709\u70b9\u610f\u601d\u7684\u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=p9Zv%2BBunXP9xiqGJT9K%2FvQ%3D%3D.LbV75O6lA9cP28%2Fu1ovtCv8gj0JKl9oeuAnQiePXnU8S%2Fc%2BL4S28DMAljb9umxpbTmm%2BwJa54l23lt%2F5bvc7hIuFulXok3dEP%2FNT15%2BcOQk%3D\" rel=\"nofollow\">https:\/\/cn.dubbo.apache.org\/zh-cn\/overview\/core-features\/load&#8230;<\/a><\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932079\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u81ea\u9002\u5e94\u8d1f\u8f7d\u5747\u8861\uff0c\u867d\u7136\u8fd9\u4e2a\u7b97\u6cd5\u6211\u662f\u5728 Dubbo \u7684\u6e90\u7801\u91cc\u9762\u770b\u5230\u7684\u3002\u4f46\u662f\u8fd9\u5e76\u4e0d\u7b97\u662f Dubbo \u7684\u4e13\u5c5e\uff0c\u800c\u662f\u4e00\u79cd\u7b97\u6cd5\u601d\u60f3\uff0c\u53ea\u4e0d\u8fc7\u4f60\u53ef\u4ee5\u5728 Dubbo \u91cc\u9762\u627e\u5230\u5176\u5bf9\u5e94\u7684 Java \u5b9e\u73b0\u3002<\/p>\n<p>\u540c\u6837\u7684\uff0c\u5728 go-zero \u91cc\u9762\uff0c\u4f60\u4e5f\u53ef\u4ee5\u627e\u5230\u5176\u5bf9\u5e94\u7684 Go \u8bed\u8a00\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u5173\u4e8e\u8fd9\u51e0\u79cd\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\uff0c\u5b98\u65b9\u7ed9\u4e86\u4e24\u4e2a\u793a\u610f\u56fe\u3002<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=t6vel31LNsrUvReEI0dcDw%3D%3D.ZU%2B%2FHGOYIinYU552Ng2adxZDnOA8SP32GyD3A6ECDWFuW8xIlnfvWxaeZGtNPdjBqaRtwlvKdFS2ExuDuw1tLqvkwGC9%2FNk%2BcUFL1oEqPlrR3JQebzss2AQuvyU%2BylpR\" rel=\"nofollow\">https:\/\/cn.dubbo.apache.org\/zh-cn\/overview\/reference\/proposal&#8230;<\/a><\/p><\/blockquote>\n<p>\u5f53\u670d\u52a1\u63d0\u4f9b\u7aef\u7684\u673a\u5668\u914d\u7f6e\u6bd4\u8f83\u5747\u8861\u65f6\uff0c\u65e2\u6bcf\u53f0\u673a\u5668\u7684\u5904\u7406\u80fd\u529b\u90fd\u5dee\u4e0d\u591a\uff0c\u751a\u81f3\u65f6\u4e00\u6837\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u770b\u5230 p2c \u7b97\u6cd5\u7684\u541e\u5410\u91cf\uff0c\u9065\u9065\u9886\u5148\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932080\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u800c\u5f53\u670d\u52a1\u63d0\u4f9b\u7aef\u7684\u673a\u5668\u914d\u7f6e\u53c2\u5dee\u4e0d\u9f50\u7684\u65f6\u5019\uff0c\u4e5f\u5c31\u662f\u6709\u7684\u673a\u5668\u5904\u7406\u80fd\u529b\u725b\u903c\uff0c\u6709\u7684\u53c8\u5f88\u62c9\u8de8\u7684\u65f6\u5019\uff0cadaptive \u7b56\u7565\u5c31\u6bd4\u8f83\u51fa\u8272\u4e86\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932081\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u672c\u6587\u4e3b\u8981\u5c31\u5e26\u5927\u5bb6\u76d8\u4e00\u4e0b adaptive \u7b56\u7565\u3002<\/p>\n<h2>Demo<\/h2>\n<p>\u9996\u5148\uff0c\u8fd8\u662f\u4e0d\u8981\u5077\u61d2\uff0c\u6211\u4eec\u641e\u4e2a Demo \u51fa\u6765\u3002<\/p>\n<p>\u5728 Dubbo \u7684\u5b98\u7f51\u4e0a\uff0c\u4e0d\u77e5\u9053\u4ec0\u4e48\u65f6\u5019\u5192\u51fa\u6765\u4e00\u4e2a Initializer \u6a21\u5757\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932082\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6211\u4f53\u9a8c\u4e86\u4e00\u4e0b\uff0c\u5229\u7528\u8fd9\u4e2a\u642d\u5efa Demo\uff0c\u548c\u4ee5\u524d\u81ea\u5df1\u548c SpringBoot \u641e\u878d\u5408\u7684\u65b9\u5f0f\u6bd4\u8d77\u6765\uff0c\u5c31\u4e00\u4e2a\u5b57\uff1a\u975e\u5e38\u7684\u5feb\uff01<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=h5AgOZMkX%2Bo7hmKHP5Bqyw%3D%3D.9J0DtSAZmYOoTbv0k9Ex%2BNkTlGfaDEaNvTwsvER7qLBjd5t2OgqJN0GXIbO%2Ftd0O\" rel=\"nofollow\">https:\/\/start.dubbo.apache.org\/bootstrap.html<\/a><\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932083\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5173\u4e8e\u8fd9\u4e2a Initializer \u6a21\u5757\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5982\u679c\u611f\u5174\u8da3\u53ef\u4ee5\u81ea\u5df1\u53bb\u73a9\u73a9\uff0c\u6211\u8fd9\u91cc\u5c31\u4e0d\u6269\u5c55\u4e86\u3002<\/p>\n<p>\u6ce8\u610f Dubbo \u7248\u672c\u9009\u62e9 3.2.0 \u5c31\u884c\uff0c\u56e0\u4e3a\u6211\u4eec\u8981\u7814\u7a76\u7684\u81ea\u9002\u5e94\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u662f\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\u624d\u5f00\u59cb\u652f\u6301\u7684\u3002<\/p>\n<p>\u6211\u521b\u5efa\u7684\u662f\u4e00\u4e2a Single Module \u9879\u76ee\uff0c\u4ee3\u7801\u4e0b\u8f7d\u4e0b\u6765\u4e4b\u540e\uff0c\u7ed3\u6784\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932084\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e00\u4e2a\u63d0\u4f9b\u8005\uff0c\u4e00\u4e2a\u6d88\u8d39\u8005\u3002\u6d88\u8d39\u8005\u7ee7\u627f\u4e86 CommandLineRunner \u63a5\u53e3\uff0c\u5728\u670d\u52a1\u542f\u52a8\u5b8c\u6210\u4e4b\u540e\u4f1a\u81ea\u52a8\u53bb\u89e6\u53d1\u4e00\u6b21\u670d\u52a1\u8c03\u7528\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932085\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6240\u4ee5\u5728\u9879\u76ee\u6253\u5f00\uff0c\u4f9d\u8d56\u62c9\u53d6\u5b8c\u6bd5\u4e4b\u540e\uff0c\u5565\u4e5f\u4e0d\u7528\u7ba1\uff0c\u6211\u4eec\u5148\u628a\u9879\u76ee\u542f\u52a8\u8d77\u6765\uff0c\u770b\u770b\u5565\u60c5\u51b5\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932086\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u63a7\u5236\u53f0\u8f93\u51fa\u4e86 Consumer \u7c7b\u4e2d\u7684\u5185\u5bb9\uff0c\u8fd9\u6837\u5c31\u7b97\u662f\u5b8c\u6210\u4e86\u4e00\u6b21 Dubbo \u8c03\u7528\uff0c\u5c31\u8fd9\u4e48\u7b80\u5355\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u4e86\u89e3 Dubbo \u670d\u52a1\u7684\u8c03\u7528\u8fc7\u7a0b\uff0c\u90a3\u4e48\u4f60\u57fa\u672c\u4e0a\u5c31\u53ef\u4ee5\u7528\u8fd9\u4e2a Demo \u53bb\u8fdb\u884c\u8c03\u8bd5\u4e86\u3002<\/p>\n<p>\u5728\u670d\u52a1\u63d0\u4f9b\u8005\u7684\u5b9e\u73b0\u7c7b\u4e2d\u6253\u4e0a\u65ad\u70b9\uff0c\u62ff\u5230\u8c03\u7528\u6808\uff0c\u73a9\u53bb\u5427\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932087\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4f46\u662f\uff0c\u4f60\u6709\u6ca1\u6709\u611f\u89c9\u5230\u4e00\u4e1d\u4e1d\u5947\u602a\uff1f<\/p>\n<p>\u6211\u4eec\u751a\u81f3\u90fd\u6ca1\u6709\u542f\u52a8\u4e00\u4e2a\u6ce8\u518c\u4e2d\u5fc3\uff0c\u5c31\u5b8c\u6210\u4e86\u4e00\u6b21 Dubbo \u8c03\u7528\uff1f<\/p>\n<p>\u56e0\u4e3a\u5728 Demo \u91cc\u9762\u4f7f\u7528\u7684\u662f injvm \u534f\u8bae\uff0c\u4e5f\u5c31\u662f\u672c\u5730\u8c03\u7528\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=HHIabTzG1TKQYNEcm5dW3w%3D%3D.DYQtBhmTRIXFau4SSSOUUtTeSRNwZX%2Bgopt9SFClbvgaSa6XW0Fi3CKYvYW4Vrap8hL1qsWdsvd49jBnTtdekcyJOeEccQxufcMdYWiJTu2thF7Xb%2F%2B6O5v7Z%2FiNFmEXOh7655%2By20vdy9hEOYDF3A%3D%3D\" rel=\"nofollow\">https:\/\/cn.dubbo.apache.org\/zh-cn\/overview\/mannual\/java-sdk\/a&#8230;<\/a><\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932088\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6240\u4ee5\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5bf9\u8fd9\u4e2a Demo \u8fdb\u884c\u4e00\u70b9\u6539\u9020\uff0c\u672c\u6b21\u8981\u8c03\u8bd5\u7684\u90e8\u5206\u662f\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\uff0c\u9700\u8981\u4f7f\u7528\u8fdc\u7a0b\u8c03\u7528\u624d\u884c\u3002<\/p>\n<p>\u56e0\u6b64\u9700\u8981\u5728\u670d\u52a1\u5f15\u7528\u7684\u5730\u65b9\uff0c\u52a0\u4e0a scope=&#8221;remote&#8221; \u914d\u7f6e\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932089\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7136\u540e\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u6ce8\u518c\u4e2d\u5fc3\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932090\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u540c\u65f6\u5728\u81ea\u5df1\u672c\u5730\u542f\u52a8\u4e00\u4e2a zookeeper\u3002<\/p>\n<p>\u901a\u8fc7 ZooInspector \u5de5\u5177\uff0c\u53ef\u4ee5\u770b\u5230\u5728 20880 \u7aef\u53e3\u6709\u4e00\u4e2a\u670d\u52a1\u63d0\u4f9b\u8005\u4e86\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932091\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7c7b\u4f3c\u7684\uff0c\u6211\u4eec\u5728 20881 \u548c 20882 \u518d\u641e\u4e00\u4e2a\u670d\u52a1\u63d0\u4f9b\u8005\uff0c\u4e00\u5171\u4e09\u4e2a\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932092\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6709\u540c\u5b66\u53ef\u80fd\u5c31\u8981\u95ee\u4e86\uff1a\u4e3a\u4ec0\u4e48\u81f3\u5c11\u8981\u4e09\u4e2a\u5462\uff1f<\/p>\n<p>\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u670d\u52a1\u63d0\u4f9b\u8005\uff0c\u4e5f\u4e0d\u9700\u8981\u8fdb\u884c\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<p>\u5982\u679c\u53ea\u6709\u4e24\u4e2a\u670d\u52a1\u63d0\u4f9b\u8005\uff0c\u4e5f\u7528\u4e0d\u4e0a\u81ea\u9002\u5e94\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\uff1f<\/p>\n<p>\u6211\u4eec\u518d\u770b\u4e00\u4e0b\u5173\u4e8e\u5b83\u7684\u63cf\u8ff0\u3002<\/p>\n<blockquote><p>\u81ea\u9002\u5e94\u8d1f\u8f7d\u5747\u8861\uff1a\u5728 P2C \u7b97\u6cd5\u57fa\u7840\u4e0a\uff0c\u9009\u62e9\u4e8c\u8005\u4e2d load \u6700\u5c0f\u7684\u90a3\u4e2a\u8282\u70b9<\/p><\/blockquote>\n<p>P2C \u7b97\u6cd5\uff0c\u662f\u8981\u968f\u673a\u9009\u62e9\u4e24\u4e2a\u8282\u70b9\u3002<\/p>\n<p>\u5982\u679c\u4f60\u53ea\u6709\u4e24\u4e2a\u8282\u70b9\uff0c\u8fd8\u9009\u4e2a\u5565\u554a\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5982\u679c\u6211\u4eec\u8981\u76d8\u4e00\u4e0b\u81ea\u9002\u5e94\u8d1f\u8f7d\u5747\u8861\uff0c\u670d\u52a1\u63d0\u4f9b\u65b9\u8282\u70b9\u5f53\u7136\u662f\u8d8a\u591a\u8d8a\u597d\uff0c\u4f46\u662f\u81f3\u5c11\u4e5f\u9700\u8981 3 \u4e2a\u8282\u70b9\u3002<\/p>\n<h2>\u6e90\u7801<\/h2>\n<p>\u5982\u679c\u8981\u542f\u7528\u81ea\u9002\u5e94\u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5\uff0c\u9700\u8981\u5728\u670d\u52a1\u5f15\u7528\u7684\u5730\u65b9\u8fdb\u884c\u6307\u5b9a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932093\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7136\u540e\u5728\u5bf9\u5e94\u7684\u4f4d\u7f6e\u6253\u4e0a\u65ad\u70b9\uff1a<\/p>\n<blockquote><p>org.apache.dubbo.rpc.cluster.loadbalance.AdaptiveLoadBalance#doSelect<\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932094\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fdb\u5165\u65ad\u70b9\u5904\uff0c\u65e2 doSelect \u65b9\u6cd5\u7684\u7b2c\u4e00\u884c\u7684\u65b9\u6cd5\uff0c\u5c31\u662f\u53eb\u505a selectByP2C\uff0c\u4e3b\u6253\u7684\u5c31\u662f\u4e00\u4e2a\u5f00\u95e8\u89c1\u5c71\u3002<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u65b9\u6cd5\u7684\u5165\u53c2 invokers \u7684\u5927\u5c0f\u5c31\u662f 3\uff0c\u5b83\u5c31\u662f\u4ee3\u8868\u6211\u4eec\u5728 20880\u300120881\u300120882 \u7aef\u53e3\u542f\u52a8\u7684\u4e09\u4e2a\u670d\u52a1\u63d0\u4f9b\u65b9\u3002<\/p>\n<p>\u8fdb\u5165 doSelect \u65b9\u6cd5\uff0c\u6838\u5fc3\u903b\u8f91\u5c31\u4e24\u90e8\u5206\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932095\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7b2c\u4e00\u90e8\u5206\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code>int pos1 = ThreadLocalRandom.current().nextInt(length);\nint pos2 = ThreadLocalRandom.current().nextInt(length - 1);\nif (pos2 &gt;= pos1) {\n    pos2 = pos2 + 1;\n}<\/code><\/pre>\n<p>\u4f60\u8bf4\u8fd9\u662f\u5728\u5e72\u5565\uff1f<\/p>\n<p>\u867d\u7136\u53ea\u6709\u7b80\u5355\u7684\u56db\u884c\u4ee3\u7801\uff0c\u4f46\u662f\u6211\u8fd8\u662f\u7ed9\u4f60\u7f15\u4e00\u7f15\uff0c\u56e0\u4e3a\u6211\u603b\u611f\u89c9\u8fd9\u4e2a\u5730\u65b9\u6709 BUG\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u6211\u4eec\u7684 Demo \u4e2d length \u5c31\u662f invokers \u7684\u5927\u5c0f\uff0c\u65e2\u4e3a 3\u3002<\/p>\n<p>\u7136\u540e pos1\u3001pos2 \u4ee3\u8868\u7684\u662f invokers \u8fd9\u4e2a List \u7684\u4e0b\u6807\u3002<\/p>\n<p>\u6240\u4ee5\uff0c<\/p>\n<p>\u7b2c\u4e00\u6b21\u968f\u673a\uff0cpos1 \u5c31\u662f\u4ece [0,3) \u4e4b\u95f4\u7684\u6b63\u6570\u3002<\/p>\n<p>\u7b2c\u4e8c\u6b21\u968f\u673a\uff0cpos2 \u5c31\u662f\u4ece [0,2) \u4e4b\u95f4\u7684\u6b63\u6570\u3002<\/p>\n<p>\u6ca1\u95ee\u9898\u5bf9\u5427\uff1f<\/p>\n<p>\u90a3\u4e48\u95ee\u9898\u5c31\u6765\u4e86\uff1a\u5982\u679c\u7b2c\u4e00\u6b21\u6ca1\u6709\u968f\u673a\u51fa 2\uff0c\u5373\u6700\u540e\u4e00\u4e2a\u4e0b\u6807\u3002\u90a3\u4e48\u7b2c\u4e8c\u6b21\u968f\u673a\u7684\u65f6\u5019\u7531\u4e8e\u6267\u884c\u4e86\u51cf\u4e00\u64cd\u4f5c\uff0c\u6240\u4ee5\u6700\u540e\u4e00\u4e2a\u4e0b\u6807\u6839\u672c\u5c31\u4e0d\u53ef\u80fd\u88ab\u968f\u673a\u5230\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u6211\u8ba4\u4e3a\u8fd9\u4e2a\u968f\u673a\u7b97\u6cd5\u5bf9\u4e8e invokers \u96c6\u5408\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f\u4e0d\u516c\u5e73\u7684\uff0c\u56e0\u4e3a\u5b83\u5c11\u4e86\u4e00\u6b21\u53c2\u4e0e\u968f\u673a\u7684\u673a\u4f1a\u3002<\/p>\n<p>\u7136\u540e\uff0c\u5b83\u8fd8\u6709\u8fd9\u6837\u7684\u4e24\u884c\u4ee3\u7801\uff1a<\/p>\n<pre><code>if (pos2 &gt;= pos1) {\n    pos2 = pos2 + 1;\n}<\/code><\/pre>\n<p>\u76ee\u7684\u662f\u4e3a\u4e86\u89e3\u51b3\u5f53 pos2 \u548c pos1 \u968f\u673a\u51fa\u4e00\u6837\u7684\u503c\u7684\u65f6\u5019\uff0c\u628a pos2 \u8fdb\u884c\u52a0\u4e00\u5904\u7406\u3002<\/p>\n<p>\u9996\u5148\uff0c pos2 \u548c pos1 \u968f\u673a\u51fa\u4e00\u6837\u7684\u503c\uff0c\u8fd9\u4e2a\u662f\u5b8c\u5168\u6709\u53ef\u80fd\u7684\u3002<\/p>\n<p>\u5176\u6b21\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u662f pos1 + 1 \u5462\uff1f\u6216\u8005\u8bf4\u80fd\u4e0d\u80fd\u662f pos1 + 1 \u5462\uff1f<\/p>\n<p>\u4e0d\u80fd\u3002<\/p>\n<p>\u56e0\u4e3a\uff0c\u5047\u8bbe pos1 \u662f\u6700\u540e\u4e00\u4e2a\u4e0b\u6807\uff0c\u518d\u52a0\u4e00\u7684\u8bdd\u90a3\u4e48\u5c31\u8d8a\u754c\u4e86\u5440\u3002<\/p>\n<p>\u53ea\u80fd\u662f pos2 \u8fdb\u884c\u52a0\u4e00\uff0c\u56e0\u4e3a pos2 \u7684\u6700\u5927\u503c\u4e5f\u53ea\u80fd\u662f\u5012\u6570\u7b2c\u4e8c\u4e2a\u5143\u7d20\u3002<\/p>\n<p>\u6574\u4e2a\u7b97\u6cd5\u4ece\u903b\u8f91\u4e0a\u6765\u8bf4\uff0c\u5b8c\u5168\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u53ef\u4ee5\u5b9e\u73b0\u968f\u673a\u9009\u62e9\u4e24\u4e2a\u5143\u7d20\u51fa\u6765\u7684\u903b\u8f91\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u6211\u603b\u611f\u89c9\u5bf9\u4e8e\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5373 List \u4e2d\u6700\u540e\u4e00\u4e2a\u670d\u52a1\u63d0\u4f9b\u8005\u6765\u8bf4\uff0c\u786e\u5b9e\u662f\u4e0d\u592a\u53cb\u597d\u3002\u5b83\u88ab\u9009\u4e2d\u7684\u6982\u7387\u6bd4\u5176\u4ed6\u7684\u5143\u7d20\u5c11\u4e86\u4e00\u534a\u3002<\/p>\n<p>\u4e07\u4e00\u6700\u540e\u4e00\u4e2a\u670d\u52a1\u63d0\u4f9b\u8005\uff0c\u53c8\u6070\u597d\u662f\u4e00\u4e2a\u6027\u80fd\u725b\u903c\u7684\u670d\u52a1\u5668\u5462\uff1f<\/p>\n<p>\u8fd9\u4e2a\u5730\u65b9\u4e0d\u77e5\u9053\u662f\u4e0d\u662f\u6211\u60f3\u9519\u4e86\uff0c\u53cd\u6b63\u6211\u4e4b\u524d\u5199 P2C \u7684\u65f6\u5019\uff0c\u662f\u8fd9\u6837\u5199\u7684\uff1a<\/p>\n<pre><code>Object invoker1 = invokerList.remove(ThreadLocalRandom.current().nextInt(invokerList.size()-1));\nObject invoker2 = invokerList.remove(ThreadLocalRandom.current().nextInt(invokerList.size()-1));<\/code><\/pre>\n<p>\u5173\u4e8e Dubbo \u6e90\u7801\u8fd9\u91cc\u4e3a\u4ec0\u4e48\u662f\u8fd9\u6837\u7684\u5199\u6cd5\uff0c\u6211\u4e5f\u4e0d\u592a\u660e\u767d\u3002\u6211\u770b\u4e86 go-zero \u5bf9\u5e94\u90e8\u5206\u7684\u6e90\u7801\uff0c\u4e5f\u662f\u548c Dubbo \u4e00\u6837\u7684\u5199\u6cd5\u3002\u4e0d\u77e5\u9053\u662f\u4e0d\u662f\u6211\u628a\u81ea\u5df1\u7ed9\u7ed5\u8fdb\u53bb\u4e86\u3002<\/p>\n<p>\u7b97\u4e86\uff0c\u95ee\u9898\u4e0d\u5927\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932096\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u524d\u9762\u8bb2\u7684\u4ee3\u7801\uff0c\u5c31\u662f P2C \u601d\u60f3\u7684\u5b9e\u73b0\uff0c\u662f\u4e0d\u662f\u975e\u5e38\u7b80\u5355\uff1f<\/p>\n<p>\u5728\u6211\u4eec\u7684 Demo \u4e2d\uff0c\u9009\u51fa\u6765\u7684\u5c31\u662f 2\uff0c1 \u8fd9\u4e24\u4e2a invokers\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932097\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u968f\u673a\u9009\u62e9\u51fa\u4e86\u4e24\u4e2a invoker \u4e86\uff0c\u90a3\u4e48\u5e94\u8be5\u7531\u54ea\u4e2a invoker \u6765\u6267\u884c\u8fd9\u6b21\u7684\u8bf7\u6c42\u5462\uff1f<\/p>\n<p>\u903b\u8f91\u5c31\u6765\u5230\u4e86 chooseLowLoadInvoker \u65b9\u6cd5\u91cc\u9762\u3002\u4ece\u65b9\u6cd5\u540d\u53ef\u4ee5\u77e5\u9053\uff0c\u662f\u8981\u9009\u62e9\u8d1f\u8f7d\u6bd4\u8f83\u4f4e\u7684\u90a3\u4e2a\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u91cc\u9762\uff0c\u6709\u4e24\u4e2a\u5173\u952e\u53d8\u91cf\uff0cload1 \u548c load2\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932098\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5b83\u4eec\u662f\u7ecf\u8fc7\u67d0\u4e2a\u516c\u5f0f\u8ba1\u7b97\u51fa\u6765\u7684\u3002<\/p>\n<p>\u6211\u4eec\u5148\u770b\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u5f53 load1 \u548c load2 \u4e00\u6837\u7684\u65f6\u5019\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e24\u4e2a invoker \u90fd\u53ef\u4ee5\u4f7f\u7528\uff0c\u5219\u6309\u7167\u6743\u91cd\u9009\u62e9\u4e00\u4e2a\u3002<\/p>\n<p>\u5f53 load1 \u548c load2 \u4e0d\u4e00\u6837\u7684\u65f6\u5019\uff0c\u5219\u9009 load \u503c\u6bd4\u8f83\u5c0f\u7684\u3002<\/p>\n<p>\u6240\u4ee5\u63a5\u4e0b\u6765\u7684\u95ee\u9898\u5c31\u53d8\u6210\u4e86\uff1a\u5982\u4f55\u8ba1\u7b97 load \u7684\u503c\uff1f<\/p>\n<p>\u6e90\u7801\u90fd\u5728\u8fd9\u4e2a\u65b9\u6cd5\u91cc\u9762\uff1a<\/p>\n<blockquote><p>org.apache.dubbo.rpc.AdaptiveMetrics#getLoad<\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932099\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u9996\u5148 getStatus \u65b9\u6cd5\u662f\u83b7\u53d6\u4e00\u4e2a AdaptiveMetrics \u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u91cc\u9762\u6709\u4e00\u4e2a ConcurrentHashMap\uff0c\u5185\u5bb9\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932100\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4ee5\u201cip:\u7aef\u53e3:\u65b9\u6cd5\u201d\u4e3a key\uff0c value \u91cc\u9762\u653e\u4e86\u5f88\u591a\u8ba1\u7b97\u8d1f\u8f7d\u76f8\u5173\u7684\u5b57\u6bb5\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932101\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6bd4\u5982\u5176\u4e2d\u7684 pickTime \u5b57\u6bb5\uff0c\u5728\u8ba1\u7b97\u8d1f\u8f7d\u7684\u65f6\u5019\uff0c\u7b2c\u4e00\u4e2a\u7528\u5230\u7684\u5c31\u662f\u5b83\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932102\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5f53\u524d\u65f6\u95f4\u51cf\u53bb pickTime \u65f6\u95f4\uff0c\u5982\u679c\u5dee\u503c\u8d85\u8fc7\u8d85\u65f6\u65f6\u95f4\u7684\u4e24\u500d\uff0c\u5219\u76f4\u63a5\u9009\u4e2d\u5b83\u3002<\/p>\n<p>\u5047\u8bbe\u8d85\u65f6\u65f6\u95f4\u662f 5s\uff0c\u90a3\u4e48\u5f53\u8fd9\u4e2a\u670d\u52a1\u7aef\u8ddd\u79bb\u4e0a\u6b21\u88ab\u9009\u4e2d\u7684\u65f6\u95f4\u8d85\u8fc7 10s\uff0c\u5219\u8fd4\u56de 0\uff0c\u65e2\u8868\u793a\u65e0\u8d1f\u8f7d\u3002<\/p>\n<p>\u90a3\u4e48\u8fd9\u4e2a pickTime \u662f\u4ec0\u4e48\u65f6\u5019\u8bbe\u7f6e\u7684\u5462\uff1f<\/p>\n<p>\u5c31\u662f\u5728 doSelect \u65b9\u6cd5\u91cc\u9762\u7ecf\u8fc7 selectByP2C \u65b9\u6cd5\u9009\u62e9\u51fa\u4e00\u4e2a\u670d\u52a1\u7aef\u4e4b\u540e\uff0c\u5728\u524d\u9762\u63d0\u5230\u7684 ConcurrentHashMap \u4e2d\u7ef4\u62a4\u4e86 pickTime\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932103\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u540c\u65f6\uff0c\u5728\u8fd9\u91cc\u8fd8\u5728\u4e0a\u4e0b\u6587\u4e2d\u7ef4\u62a4\u4e86\u4e00\u4e2a startTime\uff0c\u8868\u793a\u8fd9\u4e2a\u8bf7\u6c42\u5f00\u59cb\u6267\u884c\u7684\u65f6\u95f4\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932104\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5b83\u662f\u5728\u4ec0\u4e48\u65f6\u5019\u7528\u7684\u5462\uff1f<\/p>\n<p>\u8fd9\u4e2a\u65f6\u5019\u5c31\u8981\u628a\u76ee\u5149\u653e\u5230 AdaptiveLoadBalanceFilter \u8fd9\u4e2a\u7c7b\u4e0a\u4e86\uff1a<\/p>\n<blockquote><p>org.apache.dubbo.rpc.filter.AdaptiveLoadBalanceFilter#onResponse<\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932105\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5728 AdaptiveLoadBalanceFilter \u91cc\u9762\u7684 onResponse \u65b9\u6cd5\u91cc\u9762\uff0c\u5f53\u6536\u5230\u670d\u52a1\u7aef\u7684\u54cd\u5e94\u4e4b\u540e\uff0c\u5728\u8fd9\u91cc\u53d6\u51fa\u4e86 startTime \u7528\u6765\u8ba1\u7b97 rt \u503c\u3002<\/p>\n<p>\u540c\u65f6\u5728\u8fd9\u91cc\u8fd8\u7ef4\u62a4\u4e86 AdaptiveMetrics \u7684 consumerSuccess\uff08\u8bf7\u6c42\u6210\u529f\uff09\u3001errorReq\uff08\u8bf7\u6c42\u5931\u8d25\uff09\u8fd9\u4e24\u4e2a\u5c5e\u6027\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932106\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u770b\u5230\u6709\u8fd9\u6837\u7684\u4e00\u90e8\u5206\u4ee3\u7801\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932107\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u90e8\u5206\u4ee3\u7801\u662f\u5728\u5bf9 ADAPTIVE_LOADBALANCE_ATTACHMENT_KEY \u8fd9\u4e2a\u5b57\u6bb5\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u90a3\u4e48\u8fd9\u662f\u4e2a\u5565\u73a9\u610f\u5462\uff1f<\/p>\n<p>\u6211\u4e5f\u4e0d\u77e5\u9053\uff0c\u4f46\u662f\u6211\u77e5\u9053\u5b83\u4e5f\u662f\u5728 AdaptiveLoadBalance \u7684 doSelect \u65b9\u6cd5\u91cc\u9762\u8fdb\u884c\u4e86\u4e00\u6b21\u7ef4\u62a4\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932108\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u585e\u8fdb\u53bb\u7684\u503c\u662f\uff1a<\/p>\n<blockquote><p>private String attachmentKey = &#8220;mem,load&#8221;;<\/p><\/blockquote>\n<p>\u770b\u6837\u5b50\u662f\u8981\u5bf9\u5185\u5b58\u548c\u8d1f\u8f7d\u8fd9\u4e24\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u7edf\u8ba1\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u95ee\u4f60\uff0c\u7edf\u8ba1\u662f\u7ad9\u5728\u8c01\u7684\u7ef4\u5ea6\u7edf\u8ba1\uff1f<\/p>\n<p>\u662f\u4e0d\u662f\u8981\u7edf\u8ba1\u670d\u52a1\u7aef\u7684 mem \u548c load\uff1f<\/p>\n<p>\u6240\u4ee5\uff0c\u8fd9\u91cc\u7684\u542b\u4e49\u662f\u5ba2\u6237\u7aef\u544a\u8bc9\u670d\u52a1\u7aef\uff1a\u6211\u8fd9\u8fb9\u9700\u8981 mem,load \u8fd9\u4e24\u4e2a\u7ef4\u5ea6\uff0c\u4f60\u4e00\u4f1a\u7ed9\u6211\u9001\u56de\u6765\u3002<\/p>\n<p>\u90a3\u4e48\u670d\u52a1\u7aef\u662f\u600e\u4e48\u611f\u77e5\u5230\u7684\u5462\uff1f<\/p>\n<p>\u90a3\u6211\u4eec\u5c31\u8981\u628a\u76ee\u5149\u5207\u6362\u5230 ProfilerServerFilter \u8fd9\u4e2a Filter \u4e86\uff1a<\/p>\n<blockquote><p>org.apache.dubbo.rpc.filter.ProfilerServerFilter#onResponse<\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932109\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5728\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5b83\u4ece\u4e0a\u4e0b\u6587\u4e2d\u53d6\u51fa\u4e86 ADAPTIVE_LOADBALANCE_ATTACHMENT_KEY \u53d8\u91cf\uff0c\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u4e3a\u7a7a\u5219\u641e\u70b9\u4e8b\u60c5\uff0c\u7136\u540e\u518d\u91cd\u65b0\u7ed9\u8fd9\u4e2a\u53d8\u91cf\u8d4b\u503c\u3002<\/p>\n<p>\u90a3\u4e48\u95ee\u9898\u5c31\u6765\u4e86\uff1a\u8fd9\u4e2a\u5730\u65b9\u5e76\u6ca1\u6709\u5bf9\u6240\u8c13\u7684 mem,load \u8fdb\u884c\u4efb\u4f55\u5904\u7406\uff0c\u53ea\u662f\u8fdb\u884c\u4e86\u975e\u7a7a\u5224\u65ad\uff0c\u7136\u540e\u5c31\u81ea\u5df1 new \u4e86\u4e00\u4e2a StringBuilder\uff0c\u62fc\u63a5\u4e86 curTime \u548c load \u5c5e\u6027\u3002<\/p>\n<p>\u548c mem \u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\uff1f<\/p>\n<p>\u662f\u7684\uff0c\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002<\/p>\n<p>\u751a\u81f3\u548c load \u90fd\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\uff0c\u53ea\u8981 ADAPTIVE_LOADBALANCE_ATTACHMENT_KEY \u4e0d\u662f\u7a7a\u5c31\u884c\u3002<\/p>\n<p>\u4f46\u662f\u4f60\u4e0d\u80fd\u8bf4\u8fd9\u662f BUG\uff0c\u8fd9\u7b97\u662f\u4e2a features \u5427\u3002<\/p>\n<p>\u597d\uff0c\u7ecf\u8fc7\u524d\u9762\u7684\u5206\u6790\uff0c\u6211\u4eec\u56de\u5230\u8fd9\u4e2a\u5730\u65b9\uff1a<\/p>\n<blockquote><p>org.apache.dubbo.rpc.filter.AdaptiveLoadBalanceFilter#onResponse<\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932110\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6765\uff0c\u4f60\u544a\u8bc9\u6211\uff0c\u8fd9\u4e2a metricsMap \u91cc\u9762\u88c5\u5f97\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u662f\u4e0d\u662f\u53ea\u6709 curTime\u3001load\u3001rt \u8fd9\u4e09\u4e2a\u5c5e\u6027\u3002<\/p>\n<p>\u5de7\u4e86\uff0c\u5728 AdaptiveMetrics \u7684 setProviderMetrics \u65b9\u6cd5\u4e2d\uff0c\u4e5f\u53ea\u662f\u7528\uff08\u5199\uff09\u5230\uff08\u6b7b\uff09\u4e86\u8fd9\u4e09\u4e2a\u503c\uff0c\u7528\u4e8e\u7ed9\u5176\u4ed6\u5b57\u6bb5\u8d4b\u503c\uff1a<\/p>\n<blockquote><p>org.apache.dubbo.rpc.AdaptiveMetrics#setProviderMetrics<\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932111\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7136\u540e\u4f60\u6ce8\u610f\u770b\u6700\u540e\u4e24\u884c\uff1a<\/p>\n<blockquote><p>Vt =  \u03b2 <em> Vt-1 + (1 &#8211;  \u03b2 ) <\/em> \u03b8t<\/p><\/blockquote>\n<p>\u8fd9\u662f\u4ec0\u4e48\u4e1c\u897f\uff1f<\/p>\n<p>\u516c\u5f0f\uff1f\u6211\u5f53\u7136\u77e5\u9053\u8fd9\u662f\u4e00\u4e2a\u516c\u5f0f\u4e86\u3002<\/p>\n<p>\u6211\u662f\u95ee\u4f60\u8fd9\u662f\u4e00\u4e2a\u4ec0\u4e48\u516c\u5f0f\uff1f<\/p>\n<p>\u7b2c\u4e00\u6b21\u770b\u5230\u7684\u65f6\u5019\u6211\u4e5f\u662f\u61f5\u7684\uff0c\u6211\u4e5f\u4e0d\u77e5\u9053\uff0c\u6240\u4ee5\u6211\u67e5\u4e86\u4e00\u4e0b\uff0c\u8fd9\u662f\u6307\u6570\u52a0\u6743\u5e73\u5747(exponentially weighted moving average)\uff0c\u7b80\u79f0 EWMA\uff0c\u53ef\u4ee5\u7528\u6765\u4f30\u8ba1\u53d8\u91cf\u7684\u5c40\u90e8\u5747\u503c\uff0c\u4f7f\u5f97\u53d8\u91cf\u7684\u66f4\u65b0\u4e0e\u4e00\u6bb5\u65f6\u95f4\u5185\u7684\u5386\u53f2\u53d6\u503c\u6709\u5173\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932112\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6211\u8bd5\u56fe\u53bb\u7406\u89e3\u5b83\uff0c\u6211\u4e5f\u5927\u6982\u77e5\u9053\u5b83\u662f\u4ec0\u4e48\u4e1c\u897f\uff0c\u4f46\u662f\u4f60\u8ba9\u6211\u7ed9\u4f60\u8bf4\u51fa\u6765\uff0c\u62b1\u6b49\uff0c\u8d85\u7eb2\u4e86\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932113\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u597d\uff0c\u6700\u540e\u56de\u5230\u8ba1\u7b97 load \u503c\u7684\u8fd9\u4e2a\u5730\u65b9\uff1a<\/p>\n<blockquote><p>org.apache.dubbo.rpc.AdaptiveMetrics#getLoad<\/p><\/blockquote>\n<p>\u6700\u540e\u4e00\u884c\u4ee3\u7801\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<blockquote><p>return metrics.providerCPULoad <em> (Math.sqrt(metrics.ewma) + 1) <\/em> (inflight + 1) \/ ((((double) metrics.consumerSuccess.get() \/ (double) (metrics.consumerReq.get() + 1)) * weight) + 1);<\/p><\/blockquote>\n<p>\u867d\u7136\u4e00\u773c\u671b\u53bb\u773c\u775b\u75bc\uff0c\u4f46\u662f\u6211\u8fd8\u662f\u7ed9\u4f60\u89e3\u91ca\u4e00\u4e0b\u6bcf\u4e2a\u53d8\u91cf\u7684\u542b\u4e49\uff1a<\/p>\n<ul>\n<li>providerCPULoad\uff1a\u662f\u5728 ProfilerServerFilter \u7684 onResponse \u65b9\u6cd5\u4e2d\u7ecf\u8fc7\u8ba1\u7b97\u5f97\u5230\u7684 cpu load\u3002<\/li>\n<li>ewma\uff1a\u662f\u5728 setProviderMetrics \u65b9\u6cd5\u91cc\u9762\u7ef4\u62a4\u7684\uff0c\u5176\u4e2d lastLatency \u662f\u5728 ProfilerServerFilter \u7684 onResponse \u65b9\u6cd5\u4e2d\u7ecf\u8fc7\u8ba1\u7b97\u5f97\u5230\u7684 rt \u503c\u3002<\/li>\n<li>inflight\uff1a\u662f\u5f53\u524d\u670d\u52a1\u63d0\u4f9b\u65b9\u6b63\u5728\u5904\u7406\u4e2d\u7684\u8bf7\u6c42\u4e2a\u6570\u3002<\/li>\n<li>consumerSuccess\uff1a\u662f\u5728\u6bcf\u6b21\u8c03\u7528\u6210\u529f\u540e\u5728 AdaptiveLoadBalanceFilter \u7684 onResponse \u65b9\u6cd5\u4e2d\u7ef4\u62a4\u7684\u503c\u3002<\/li>\n<li>consumerReq\uff1a\u662f\u603b\u7684\u8c03\u7528\u6b21\u6570\u3002<\/li>\n<li>weight\uff1a\u662f\u670d\u52a1\u63d0\u4f9b\u65b9\u914d\u7f6e\u7684\u6743\u91cd\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u8fd9\u4e9b\u53d8\u91cf\u5e26\u5165\u5230\u4e0a\u9762\u7684\u516c\u5f0f\u4e2d\uff0c\u5c31\u80fd\u83b7\u53d6\u5230\u4e00\u4e2a load \u503c\u3002<\/p>\n<p>\u6bcf\u4e2a\u670d\u52a1\u63d0\u4f9b\u65b9\u7ecf\u8fc7\u4e0a\u9762\u7684\u8ba1\u7b97\u90fd\u4f1a\u5f97\u5230\u4e00\u4e2a load \u503c\uff0c\u5176\u503c\u8d8a\u4f4e\u4ee3\u8868\u8d8a\u5176\u8d1f\u8f7d\u8d8a\u4f4e\u3002\u8bf7\u6c42\u5c31\u5e94\u8be5\u53d1\u5230\u8d1f\u8f7d\u4f4e\u7684\u673a\u5668\u4e0a\u53bb\u3002<\/p>\n<p>\u56e0\u4e3a\u8fd9\u4e2a load \u503c\uff0c\u662f\u5b9e\u65f6\u8ba1\u7b97\u51fa\u6765\u7684\uff0c\u53cd\u5e94\u7684\u662f\u5f53\u524d\u670d\u52a1\u5668\u7684\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>\u800c\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u5728\u9009\u62e9\u7684\u65f6\u5019\uff0c\u901a\u8fc7 load \u503c\u6765\u51b3\u7b56\u662f\u5426\u5e94\u8be5\u9009\u4e2d\u8fd9\u4e2a\u670d\u52a1\u63d0\u4f9b\u65b9\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u8fd9\u5c31\u662f\u81ea\u9002\u5e94\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<p>\u6709\u7684\u540c\u5b66\u5c31\u4f1a\u95ee\u4e86\uff1a\u65e2\u7136\u53ef\u4ee5\u5b9e\u65f6\u8ba1\u7b97 load \u503c\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e0d\u628a\u6240\u6709\u7684\u670d\u52a1\u63d0\u4f9b\u8005\u7684 load \u90fd\u8ba1\u7b97\u51fa\u6765\uff0c\u7136\u540e\u9009\u62e9\u6700\u5c0f\u7684\u5462\uff1f<\/p>\n<p>\u5f88\u7b80\u5355\uff0c\u56e0\u4e3a\u968f\u673a\u9009\u62e9\u4e24\u4e2a\u51fa\u6765\u6bd4\u8f83\u5bf9\u5e94\u7684\u65f6\u95f4\u662f\u53ef\u63a7\u7684\uff0c\u5728\u5e38\u6570\u65f6\u95f4\u5185\u3002\u4f46\u662f\u5982\u679c\u4f60\u8981\u628a\u6240\u6709\u7684\u670d\u52a1\u63d0\u4f9b\u8005\u90fd\u8ba1\u7b97\u4e00\u904d\uff0c\u90a3\u4e48\u8017\u65f6\u5c31\u548c\u670d\u52a1\u63d0\u4f9b\u8005\u7684\u6570\u91cf\u6210\u6b63\u6bd4\u4e86\u3002<\/p>\n<p>P2C\uff0c\u7a33\u5f53\u7684\uff0c\u653e\u5fc3\u3002<\/p>\n<h2>\u81ea\u9002\u5e94\u9650\u6d41<\/h2>\n<p>\u524d\u9762\u804a\u4e86\u81ea\u9002\u5e94\u8d1f\u8f7d\u5747\u8861\uff0c\u4f46\u662f\u8fd8\u5728\u7ad9\u5728\u670d\u52a1\u8c03\u7528\u65b9\u7684\u89d2\u5ea6\u6765\u8bf4\u7684\u3002<\/p>\n<p>\u670d\u52a1\u8c03\u7528\u65b9\u6765\u51b3\u5b9a\u672c\u6b21\u7531\u54ea\u4e2a\u670d\u52a1\u63d0\u4f9b\u65b9\u6765\u6267\u884c\u8fd9\u6b21\u8bf7\u6c42\u3002<\/p>\n<p>\u90a3\u4e48\u95ee\u9898\u5c31\u6765\u4e86\uff1a\u4f60\u670d\u52a1\u8c03\u7528\u65b9\u51ed\u4ec0\u4e48\u8bf4\u5565\u5c31\u662f\u5565\uff1f\u6211\u670d\u52a1\u63d0\u4f9b\u65b9\u4e0d\u670d\u6c14\u3002<\/p>\n<p>\u5047\u8bbe\u73b0\u5728\u6240\u6709\u7684\u670d\u52a1\u63d0\u4f9b\u65b9\u7684 load \u503c\u90fd\u5f88\u9ad8\u4e86\u3002\u6211 P2C \u51fa\u6765\u4e24\u4e2a\uff0c\u4e00\u4e2a\u8d1f\u8f7d\u662f 100\uff0c\u4e00\u4e2a\u8d1f\u8f7d\u662f 99\u3002<\/p>\n<p>\u6309\u7406\u6765\u8bf4\uff0c\u8fd9\u4e2a\u65f6\u5019\u4e0d\u5e94\u8be5\u628a\u8bf7\u6c42\u518d\u7ed9\u5230\u670d\u52a1\u65b9\u4e86\u3002\u4f46\u662f\u8c03\u7528\u65b9\u53ef\u4e0d\u7ba1\u8fd9\u4e9b\u4e8b\u60c5\uff0c\u4e00\u4e2a\u52b2\u7684\u7ed9\u5c31\u884c\u4e86\uff0c\u8fb9\u7ed9\u8fb9\u8bf4\uff1a\u6210\u957f\uff0c\u4e00\u5b9a\u662f\u4f34\u968f\u7740\u75db\u82e6\u7684&#8230;<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932114\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6240\u4ee5\uff0c\u51fa\u4e8e\u4fdd\u62a4\u81ea\u5df1\u7684\u539f\u5219\uff0c\u670d\u52a1\u7aef\u5e94\u8be5\u6709\u6743\u5728\u81ea\u5df1\u5feb hold \u4e0d\u4f4f\u7684\u65f6\u5019\uff0c\u62d2\u7edd\u8c03\u7528\u7aef\u53d1\u6765\u7684\u8bf7\u6c42\u3002<\/p>\n<p>\u4f46\u662f\u5230\u5e95\u5e94\u8be5\u5728\u4ec0\u4e48\u65f6\u5019\u53bb\u62d2\u7edd\u8bf7\u6c42\u5462\uff1f<\/p>\n<p>\u8fd9\u4e2a\u4e0d\u597d\u8bf4\uff0c\u5e94\u8be5\u662f\u4e00\u4e2a\u968f\u7740\u670d\u52a1\u80fd\u529b\u53d8\u5316\u800c\u53d8\u5316\u7684\u4e00\u4e2a\u4e1c\u897f\u3002<\/p>\n<p>\u57fa\u4e8e\u6b64\uff0cDubbo \u4e5f\u63d0\u51fa\u4e86\u81ea\u9002\u5e94\u9650\u6d41\u7684\u63aa\u65bd\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=ZoGEHpkukb%2BWu4gustTV%2Fw%3D%3D.eutykfrc8TKyw0fWn3Gb8BfFLOpQc15u0kJZbtzt89FoW%2FbGfpmoDUE6jKObb4orp2vip9RKi6c1NuCAyo%2BQ2Y7D7QLA1haCX6G83W5WI9fBciIPJbXDa7KzlJbiGxX%2FZCCKOulLBsGqUMIAKkoBRy3VXkZKovFSXp9k5gXx0MTkCGPEVocdiy%2BWiHOnuQoj\" rel=\"nofollow\">https:\/\/cn.dubbo.apache.org\/zh-cn\/overview\/reference\/proposal&#8230;<\/a><\/p><\/blockquote>\n<p>\u4ece\u7406\u8bba\u4e0a\u8bb2\uff0c\u670d\u52a1\u7aef\u673a\u5668\u7684\u5904\u7406\u80fd\u529b\u662f\u5b58\u5728\u4e0a\u9650\u7684\uff0c\u5bf9\u4e8e\u4e00\u53f0\u670d\u52a1\u7aef\u673a\u5668\uff0c\u5f53\u77ed\u65f6\u95f4\u5185\u51fa\u73b0\u5927\u91cf\u7684\u8bf7\u6c42\u8c03\u7528\u65f6\uff0c\u4f1a\u5bfc\u81f4\u5904\u7406\u4e0d\u53ca\u65f6\u7684\u8bf7\u6c42\u79ef\u538b\uff0c\u4f7f\u673a\u5668\u8fc7\u8f7d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u80fd\u5bfc\u81f4\u4e24\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u7531\u4e8e\u8bf7\u6c42\u79ef\u538b\uff0c\u6700\u7ec8\u6240\u6709\u7684\u8bf7\u6c42\u90fd\u5fc5\u987b\u7b49\u5f85\u8f83\u957f\u65f6\u95f4\u624d\u80fd\u88ab\u5904\u7406\uff0c\u4ece\u800c\u4f7f\u6574\u4e2a\u670d\u52a1\u762b\u75ea\u3002<\/li>\n<li>\u670d\u52a1\u7aef\u673a\u5668\u957f\u65f6\u95f4\u7684\u8fc7\u8f7d\u53ef\u80fd\u6709\u5b95\u673a\u7684\u98ce\u9669\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932115\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932116\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u73a9\u610f\u5c31\u66f4\u590d\u6742\u4e86\uff0c\u6211\u770b\u4e86\u4e00\u4e0b\u5bf9\u5e94\u7684 pr\uff0c\u76ee\u524d\u8fd8\u5904\u4e8e open \u72b6\u6001\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=pwJm1zdikzKHlBG%2BNEsB0A%3D%3D.%2F%2FK0NlyhCc1FDkw2qfGUHVpNSie6EUvJ4Ue5e%2BMmST1TIbPMrcwlLFps2qHBAG1c\" rel=\"nofollow\">https:\/\/github.com\/apache\/dubbo\/pull\/10642<\/a><\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932117\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u770b\u5f97\u6211\u8111\u58f3\u75bc\uff0c\u5728\u8fd9\u91cc\u6307\u4e2a\u8def\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u81ea\u5df1\u53bb\u7814\u7a76\u4e00\u4e0b\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u53ef\u4ee5\u7ed3\u5408\u7740\u8fd9\u4e2a\u8d5b\u9898\u770b\u3002<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=c7pdH421nUh6MiOH8Qy61A%3D%3D.z1pbgJkhzwlTohYb%2FXIQu67wRY2ZF3899cz9J8ZlWEMrJmyFTrr5lE7nlIw8B%2F3pkKPxX8bqLH4OaNXinF4FhjnMvc0s7CdrFCXoun9wqrI%3D\" rel=\"nofollow\">https:\/\/tianchi.aliyun.com\/competition\/entrance\/531923\/introd&#8230;<\/a><\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043932118\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u80fd\u770b\u660e\u767d\u66f4\u597d\uff0c\u770b\u4e0d\u660e\u767d\u7684\u8bdd\uff0c\u5b66\u5230\u4e00\u4e2a\u770b\u8d77\u6765\u5f88\u725b\u903c\u7684\u8bcd\u4e5f\u4e0d\u9519\u7684\uff1a\u67d4\u6027\u96c6\u7fa4\u8c03\u5ea6\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043932077\">https:\/\/segmentfault.com\/a\/1190000043932077<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f60\u597d\u5440\uff0c\u6211\u662f\u6b6a\u6b6a\u3002 \u8fd9\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u6765\u76d8\u4e00\u4e2a\u6709\u70b9\u610f\u601d\u7684\u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5\uff1a https:\/\/cn.dubbo.apac [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10007983","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10007983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10007983"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10007983\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10007983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10007983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10007983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}