{"id":206036,"date":"2025-06-12T12:08:48","date_gmt":"2025-06-12T04:08:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206036\/"},"modified":"2025-06-12T12:08:48","modified_gmt":"2025-06-12T04:08:48","slug":"rpc-%e5%ae%a2%e6%88%b7%e7%ab%af%e4%bb%a3%e7%a0%81%e4%b8%ad%ef%bc%8cgoroutine-%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%e5%a6%82%e4%bd%95%e4%b8%8e%e4%b8%bb%e7%ba%bf%e7%a8%8b%e7%9a%84%e7%94%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206036\/","title":{"rendered":"RPC \u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\uff0cgoroutine \u7684\u751f\u547d\u5468\u671f\u5982\u4f55\u4e0e\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u4ea4\u4e92\uff1f"},"content":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aRPC \u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\uff0cgoroutine \u7684\u751f\u547d\u5468\u671f\u5982\u4f55\u4e0e\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u4ea4\u4e92\uff1f\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241101\/17304494956724905741a69.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u8fd9\u6bb5\u4ee3\u7801\u4e2d goroutine \u7684\u751f\u547d\u5468\u671f<\/strong>,\u5728\u7ed9\u5b9a\u7684 RPC \u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\uff0cnewClientCodec() \u51fd\u6570\u8fd4\u56de\u540e\u542f\u52a8\u4e86\u4e00\u4e2a\u540d\u4e3a receive \u7684 goroutine\u3002\u8fd9\u4e2a\u534f\u7a0b\u8d1f\u8d23\u63a5\u6536\u6765\u81ea\u670d\u52a1\u5668\u7684\u54cd\u5e94\u3002,<br \/>\n<strong>\u534f\u7a0b\u751f\u547d\u5468\u671f\u4fdd\u8bc1<\/strong>,\u4e3b\u7ebf\u7a0b\u8fd0\u884c\u5b8c\u540e\uff0creceive \u534f\u7a0b\u662f\u5426\u7ee7\u7eed\u8fd0\u884c\u53d6\u51b3\u4e8e\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u548c\u8be5\u534f\u7a0b\u7684\u5904\u7406\u65b9\u5f0f\u3002,<br \/>\n<strong>\u4f18\u96c5\u5173\u95ed<\/strong>,\u4e3a\u4e86\u6ee1\u8db3\u4f18\u96c5\u5173\u95ed\u7684\u9700\u6c42\uff0c\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a,\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aRPC \u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\uff0cgoroutine \u7684\u751f\u547d\u5468\u671f\u5982\u4f55\u4e0e\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u4ea4\u4e92\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGOLANG\u77e5\u8bc6\uff01,\u76d1\u542c SIGINT \u6216 SIGTERM \u4fe1\u53f7\u5e76\u4f7f\u7528 context \u6216 sync.WaitGroup \u6765\u8ba9 receive \u534f\u7a0b\u5728\u6536\u5230\u4fe1\u53f7\u65f6\u4f18\u96c5\u5730\u9000\u51fa\u3002,\u4f7f\u7528 sync.WaitGroup \u6216 context \u6765\u7b49\u5f85 receive \u534f\u7a0b\u5b8c\u6210\u5904\u7406\u3002,<\/p>\n<p><strong>\u4e3b\u7ebf\u7a0b\u975e\u963b\u585e\u7684\u60c5\u51b5\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u4e3b\u7ebf\u7a0b\u6ca1\u6709\u88ab\u963b\u585e\u8c03\u7528\u963b\u585e\uff0c\u90a3\u4e48\u5b83\u5c06\u6267\u884c\u5b8c\u6bd5\u5e76\u9000\u51fa\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u4efb\u4f55\u5176\u4ed6\u673a\u5236\u6765\u4fdd\u6301 receive \u534f\u7a0b\u7684\u751f\u547d\u5468\u671f\uff0c\u90a3\u4e48\u5f53\u4e3b\u7ebf\u7a0b\u9000\u51fa\u65f6\uff0c\u8be5\u534f\u7a0b\u4e5f\u4f1a\u88ab\u7ec8\u6b62\u3002<\/p>\n<p>,<\/p>\n<p><strong>\u4e3b\u7ebf\u7a0b\u963b\u585e\u7684\u60c5\u51b5\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u4e3b\u7ebf\u7a0b\u88ab\u7f51\u7edc API\uff08\u5982 listen\uff09\u7b49\u963b\u585e\u8c03\u7528\u963b\u585e\uff0c\u90a3\u4e48\u5b83\u5c06\u4e00\u76f4\u8fd0\u884c\uff0c\u76f4\u5230\u963b\u585e\u89e3\u9664\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0creceive \u534f\u7a0b\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u800c\u4e0d\u53d7\u4e3b\u7ebf\u7a0b\u751f\u547d\u5468\u671f\u5f71\u54cd\u3002<\/p>\n<p>,\u8fd9\u6bb5\u4ee3\u7801\u4e2d goroutine \u7684\u751f\u547d\u5468\u671f,\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aRPC \u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\uff0cgoroutine \u7684\u751f\u547d\u5468\u671f\u5982\u4f55\u4e0e\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u4ea4\u4e92\uff1f\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>RPC \u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\uff0cgoroutine \u7684\u751f\u547d\u5468\u671f\u5982\u4f55\u4e0e\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u4ea4\u4e92\uff1f<\/span><\/p>\n<h1>RPC \u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\uff0cgoroutine \u7684\u751f\u547d\u5468\u671f\u5982\u4f55\u4e0e\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u4ea4\u4e92\uff1f<\/h1>\n<p><span>2024-11-01 16:25:10<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aRPC \u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\uff0cgoroutine \u7684\u751f\u547d\u5468\u671f\u5982\u4f55\u4e0e\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u4ea4\u4e92\uff1f\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241101\/17304494956724905741a69.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u8fd9\u6bb5\u4ee3\u7801\u4e2d goroutine \u7684\u751f\u547d\u5468\u671f<\/strong><\/p>\n<p>\u5728\u7ed9\u5b9a\u7684 RPC \u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\uff0cnewClientCodec() \u51fd\u6570\u8fd4\u56de\u540e\u542f\u52a8\u4e86\u4e00\u4e2a\u540d\u4e3a receive \u7684 goroutine\u3002\u8fd9\u4e2a\u534f\u7a0b\u8d1f\u8d23\u63a5\u6536\u6765\u81ea\u670d\u52a1\u5668\u7684\u54cd\u5e94\u3002<\/p>\n<p><strong>\u534f\u7a0b\u751f\u547d\u5468\u671f\u4fdd\u8bc1<\/strong><\/p>\n<p>\u4e3b\u7ebf\u7a0b\u8fd0\u884c\u5b8c\u540e\uff0creceive \u534f\u7a0b\u662f\u5426\u7ee7\u7eed\u8fd0\u884c\u53d6\u51b3\u4e8e\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u548c\u8be5\u534f\u7a0b\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u4e3b\u7ebf\u7a0b\u963b\u585e\u7684\u60c5\u51b5\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u4e3b\u7ebf\u7a0b\u88ab\u7f51\u7edc API\uff08\u5982 listen\uff09\u7b49\u963b\u585e\u8c03\u7528\u963b\u585e\uff0c\u90a3\u4e48\u5b83\u5c06\u4e00\u76f4\u8fd0\u884c\uff0c\u76f4\u5230\u963b\u585e\u89e3\u9664\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0creceive \u534f\u7a0b\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u800c\u4e0d\u53d7\u4e3b\u7ebf\u7a0b\u751f\u547d\u5468\u671f\u5f71\u54cd\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4e3b\u7ebf\u7a0b\u975e\u963b\u585e\u7684\u60c5\u51b5\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u4e3b\u7ebf\u7a0b\u6ca1\u6709\u88ab\u963b\u585e\u8c03\u7528\u963b\u585e\uff0c\u90a3\u4e48\u5b83\u5c06\u6267\u884c\u5b8c\u6bd5\u5e76\u9000\u51fa\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u4efb\u4f55\u5176\u4ed6\u673a\u5236\u6765\u4fdd\u6301 receive \u534f\u7a0b\u7684\u751f\u547d\u5468\u671f\uff0c\u90a3\u4e48\u5f53\u4e3b\u7ebf\u7a0b\u9000\u51fa\u65f6\uff0c\u8be5\u534f\u7a0b\u4e5f\u4f1a\u88ab\u7ec8\u6b62\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u4f18\u96c5\u5173\u95ed<\/strong><\/p>\n<p>\u4e3a\u4e86\u6ee1\u8db3\u4f18\u96c5\u5173\u95ed\u7684\u9700\u6c42\uff0c\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 sync.WaitGroup \u6216 context \u6765\u7b49\u5f85 receive \u534f\u7a0b\u5b8c\u6210\u5904\u7406\u3002<\/li>\n<li>\u76d1\u542c SIGINT \u6216 SIGTERM \u4fe1\u53f7\u5e76\u4f7f\u7528 context \u6216 sync.WaitGroup \u6765\u8ba9 receive \u534f\u7a0b\u5728\u6536\u5230\u4fe1\u53f7\u65f6\u4f18\u96c5\u5730\u9000\u51fa\u3002<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aRPC \u5ba2\u6237\u7aef\u4ee3\u7801\u4e2d\uff0cgoroutine \u7684\u751f\u547d\u5468\u671f\u5982\u4f55\u4e0e\u4e3b\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u4ea4\u4e92\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGOLANG\u77e5\u8bc6\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Scss \u4e2d\u5982\u4f55\u6d88\u9664\u5b50\u5143\u7d20\u7ee7\u627f\u7236\u5143\u7d20\u5c5e\u6027\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u4e3a\u4ec0\u4e48 Flex \u5bb9\u5668\u5185\u7684\u56fe\u7247\u6ca1\u6709\u538b\u7f29\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587 \u300a&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-206036","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=206036"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206036\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}