{"id":17006,"date":"2025-03-31T15:03:21","date_gmt":"2025-03-31T07:03:21","guid":{"rendered":"https:\/\/92it.top\/?p=17006"},"modified":"2025-03-31T15:03:21","modified_gmt":"2025-03-31T07:03:21","slug":"http-%e4%b8%8e-rpc%e7%9a%84%e5%8c%ba%e5%88%ab%ef%bc%81","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=17006","title":{"rendered":"HTTP \u4e0e RPC\u7684\u533a\u522b\uff01"},"content":{"rendered":"\n<p><a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247503359&amp;idx=1&amp;sn=54529d36592977be332a03e9fa4e9c19&amp;chksm=96c4e1c3a1b368d5d1ff0b7b8b0115a0d1a93765c61a49028d5efdab18b0dc7f60fe714735dd&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">\u8f6c\u8f7d\uff1aHTTP \u4e0e RPC\u7684\u533a\u522b\uff01<\/a><\/p>\n\n\n\n<p>\u4eca\u5929\u6211\u4eec\u4e00\u8d77\u6765\u804a\u804a\u6dd8\u5b9d1\u9762\u7684\u4e00\u4e2a\u95ee\u9898\uff1aHTTP \u4e0e RPC\u7684\u533a\u522b\u3002HTTP \u4e0e RPC\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u90a3\u4e48\uff0c\u5b83\u4eec\u5230\u5e95\u6709\u4ec0\u4e48\u533a\u522b\uff1f\u6211\u4eec\u8be5\u5982\u4f55\u9009\u62e9\uff1f\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u6765\u63ed\u6653\u7b54\u6848\u3002<\/p>\n\n\n\n<p><strong>1. HTTP<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>1.1 \u5b9a\u4e49<\/strong><\/p>\n\n\n\n<p>HTTP\uff0c\u5168\u79f0\u662f&nbsp;<strong>HyperText Transfer Protocol<\/strong>\uff0c\u662f\u7528\u4e8e\u5206\u5e03\u5f0f\u3001\u534f\u4f5c\u5f0f\u548c\u8d85\u5a92\u4f53\u4fe1\u606f\u7cfb\u7edf\u7684\u5e94\u7528\u5c42\u534f\u8bae\u3002\u7b80\u5355\u6765\u8bf4\uff0cHTTP \u5c31\u662f\u6211\u4eec\u5e73\u65f6\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee\u7f51\u9875\u65f6\u7528\u7684\u534f\u8bae\u3002\u5b83\u57fa\u4e8e\u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f\uff0c\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u8fd4\u56de\u54cd\u5e94\u3002<\/p>\n\n\n\n<p><strong>1.2 \u5de5\u4f5c\u539f\u7406<\/strong><\/p>\n\n\n\n<p>HTTP \u662f\u4e00\u79cd\u65e0\u72b6\u6001\u7684\u534f\u8bae\uff0c\u6bcf\u6b21\u8bf7\u6c42\u90fd\u662f\u72ec\u7acb\u7684\u3002\u5ba2\u6237\u7aef\u53d1\u9001\u4e00\u4e2a HTTP \u8bf7\u6c42\uff08\u5305\u62ec\u65b9\u6cd5\u3001URL\u3001\u5934\u90e8\u4fe1\u606f\u548c\u53ef\u9009\u7684\u4e3b\u4f53\uff09\uff0c\u670d\u52a1\u5668\u5904\u7406\u540e\u8fd4\u56de\u4e00\u4e2a HTTP \u54cd\u5e94\uff08\u72b6\u6001\u7801\u3001\u5934\u90e8\u4fe1\u606f\u548c\u4e3b\u4f53\uff09\u3002<\/p>\n\n\n\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5f53\u4f60\u5728\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165&nbsp;<code>https:\/\/www.yuanjava.com<\/code>\uff0c\u6d4f\u89c8\u5668\u4f1a\u53d1\u9001\u4e00\u4e2a HTTP GET \u8bf7\u6c42\u5230\u670d\u52a1\u5668\uff0c\u670d\u52a1\u5668\u5904\u7406\u540e\u8fd4\u56de\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n\n\n\n<p><strong>2. RPC<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>2.1 \u5b9a\u4e49<\/strong><\/p>\n\n\n\n<p>RPC\uff0c\u5168\u79f0\u662f&nbsp;<strong>Remote Procedure Call<\/strong>\uff08\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff09\uff0c\u662f\u4e00\u79cd\u901a\u8fc7\u7f51\u7edc\u6267\u884c\u8fdc\u7a0b\u8ba1\u7b97\u673a\u4e0a\u7684\u8fc7\u7a0b\uff08\u51fd\u6570\uff09\u7684\u534f\u8bae\u3002\u5b83\u7684\u76ee\u6807\u662f\u8ba9\u5f00\u53d1\u8005\u611f\u89c9\u50cf\u662f\u5728\u672c\u5730\u8c03\u7528\u51fd\u6570\u4e00\u6837\uff0c\u65e0\u9700\u5173\u5fc3\u5e95\u5c42\u7684\u7f51\u7edc\u901a\u4fe1\u7ec6\u8282\u3002\u5e38\u89c1\u7684 RPC \u6846\u67b6\u6709 gRPC\u3001Thrift \u7b49\u3002<\/p>\n\n\n\n<p><strong>2.2 \u5de5\u4f5c\u539f\u7406<\/strong><\/p>\n\n\n\n<p>RPC \u6a21\u578b\u5219\u66f4\u50cf\u662f\u51fd\u6570\u8c03\u7528\u3002\u5ba2\u6237\u7aef\u8c03\u7528\u4e00\u4e2a\u8fdc\u7a0b\u7684\u51fd\u6570\uff0c\u4f20\u9012\u53c2\u6570\uff0c\u7b49\u5f85\u7ed3\u679c\u8fd4\u56de\u3002RPC \u6846\u67b6\u4f1a\u8d1f\u8d23\u5c06\u8fd9\u4e2a\u8c03\u7528\u8f6c\u6362\u4e3a\u7f51\u7edc\u8bf7\u6c42\uff0c\u4f20\u8f93\u53c2\u6570\uff0c\u63a5\u6536\u54cd\u5e94\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u8fdc\u7a0b\u7684&nbsp;<code>getUserInfo(userId)<\/code>&nbsp;\u51fd\u6570\uff0c\u5ba2\u6237\u7aef\u53ea\u9700\u8981\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0cRPC \u6846\u67b6\u4f1a\u5904\u7406\u7f51\u7edc\u901a\u4fe1\uff0c\u8fd4\u56de\u7528\u6237\u4fe1\u606f\u3002<\/p>\n\n\n\n<p><strong>3. \u6838\u5fc3\u533a\u522b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>HTTP \u548c RPC \u7684\u533a\u522b\u5728\u4e8e\u5b83\u4eec\u7684\u901a\u4fe1\u6a21\u578b\u548c\u8bed\u4e49\u3002<\/p>\n\n\n\n<p><strong>1. \u901a\u4fe1\u6a21\u578b<\/strong>\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>HTTP<\/strong>\uff1a\u57fa\u4e8e\u8bf7\u6c42-\u54cd\u5e94\uff0c\u901a\u5e38\u7528\u4e8e\u8d44\u6e90\u7684\u83b7\u53d6\u548c\u64cd\u4f5c\uff08\u5982 RESTful \u98ce\u683c\uff09\u3002<\/li>\n\n\n\n<li><strong>RPC<\/strong>\uff1a\u57fa\u4e8e\u65b9\u6cd5\u8c03\u7528\uff0c\u66f4\u50cf\u662f\u8c03\u7528\u8fdc\u7a0b\u7684\u51fd\u6570\u6216\u670d\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>2. \u8bed\u4e49<\/strong>\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>HTTP<\/strong>\uff1a\u5f3a\u8c03\u8d44\u6e90\u7684\u8868\u73b0\u5f62\u5f0f\u548c\u72b6\u6001\uff0c\u5982 GET\u3001POST\u3001PUT\u3001DELETE \u7b49\u52a8\u8bcd\u3002<\/li>\n\n\n\n<li><strong>RPC<\/strong>\uff1a\u5f3a\u8c03\u529f\u80fd\u548c\u64cd\u4f5c\uff0c\u8c03\u7528\u7684\u662f\u5177\u4f53\u7684\u65b9\u6cd5\u6216\u670d\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>4. \u793a\u4f8b\u89e3\u6790<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u7406\u89e3\uff0c\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u770b\u770b\u5ef6\u65f6\uff08Latency\uff09\u5982\u4f55\u5728 HTTP \u548c RPC \u4e2d\u8868\u73b0\u3002<\/p>\n\n\n\n<p><strong>4.1 \u573a\u666f\u63cf\u8ff0<\/strong><\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7528\u6237\u670d\u52a1\uff0c\u9700\u8981\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u3002\u6211\u4eec\u5206\u522b\u901a\u8fc7 HTTP \u548c RPC \u4e24\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p><strong>4.2 HTTP \u5b9e\u73b0<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u4f7f\u7528 Spring Boot \u7684 HTTP \u5ba2\u6237\u7aef\nRestTemplate restTemplate = new RestTemplate();\nString url = \"http:\/\/localhost:8080\/api\/user\/\" + userId;\nlong startTime = System.currentTimeMillis();\nResponseEntity&lt;User> response = restTemplate.getForEntity(url, User.class);\nlong endTime = System.currentTimeMillis();\nSystem.out.println(\"HTTP \u8bf7\u6c42\u8017\u65f6: \" + (endTime - startTime) + \" ms\");\n<\/pre>\n\n\n\n<p><strong>4.3 RPC \u5b9e\u73b0<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u4f7f\u7528 gRPC \u5ba2\u6237\u7aef\nUserServiceGrpc.UserServiceBlockingStub stub = UserServiceGrpc.newBlockingStub(channel);\nGetUserRequest request = GetUserRequest.newBuilder().setUserId(userId).build();\nlong startTime = System.currentTimeMillis();\nUser response = stub.getUser(request);\nlong endTime = System.currentTimeMillis();\nSystem.out.println(\"RPC \u8bf7\u6c42\u8017\u65f6: \" + (endTime - startTime) + \" ms\");\n<\/pre>\n\n\n\n<p><strong>4.4 \u5ef6\u65f6\u6bd4\u8f83<\/strong><\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u7684\u7f51\u7edc\u5ef6\u65f6\u662f\u56fa\u5b9a\u7684\uff0c\u6bd4\u5982 50ms\u3002\u7531\u4e8e RPC \u7684\u901a\u4fe1\u534f\u8bae\u66f4\u8f7b\u91cf\uff0c\u800c\u4e14\u901a\u5e38\u4f7f\u7528\u4e8c\u8fdb\u5236\u4f20\u8f93\uff0c\u7406\u8bba\u4e0a RPC \u7684\u5ef6\u65f6\u4f1a\u7565\u4f4e\u4e8e HTTP\u3002\u7136\u800c\uff0c\u5b9e\u9645\u60c5\u51b5\u8fd8\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5b9e\u73b0\u548c\u4f18\u5316\u3002<\/p>\n\n\n\n<p><strong>\u4e92\u52a8\u65f6\u95f4<\/strong>\uff1a\u4f60\u89c9\u5f97\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5ef6\u65f6\u5dee\u5f02\u4f1a\u663e\u8457\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\u5417\uff1f\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u5206\u4eab\u4f60\u7684\u770b\u6cd5\uff01<\/p>\n\n\n\n<p><strong>5. \u5982\u4f55\u9009\u62e9\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>5.1 \u9002\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<p><strong>1. HTTP<\/strong>\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u516c\u5f00 API<\/strong>\uff1a\u5982\u9762\u5411\u7b2c\u4e09\u65b9\u5f00\u53d1\u8005\u7684 RESTful API\u3002<\/li>\n\n\n\n<li><strong>\u6d4f\u89c8\u5668\u901a\u4fe1<\/strong>\uff1a\u524d\u7aef\u4e0e\u540e\u7aef\u7684\u901a\u4fe1\uff0c\u7279\u522b\u662f\u7f51\u9875\u5e94\u7528\u3002<\/li>\n\n\n\n<li><strong>\u7b80\u5355\u7684 CRUD \u64cd\u4f5c<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>2. RPC<\/strong>\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u5185\u90e8\u670d\u52a1\u901a\u4fe1<\/strong>\uff1a\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u670d\u52a1\u4e4b\u95f4\u7684\u9ad8\u6548\u901a\u4fe1\u3002<\/li>\n\n\n\n<li><strong>\u9700\u8981\u9ad8\u6027\u80fd<\/strong>\uff1a\u5bf9\u5ef6\u65f6\u8981\u6c42\u9ad8\u7684\u573a\u666f\uff0c\u5982\u5b9e\u65f6\u6570\u636e\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>\u590d\u6742\u4e1a\u52a1\u903b\u8f91<\/strong>\uff1a\u9700\u8981\u8c03\u7528\u591a\u4e2a\u8fdc\u7a0b\u65b9\u6cd5\uff0cRPC \u66f4\u5177\u6709\u7075\u6d3b\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>5.2 \u4e92\u8865\u4f7f\u7528<\/strong><\/p>\n\n\n\n<p>\u5176\u5b9e\uff0cHTTP \u548c RPC \u5e76\u4e0d\u4e00\u5b9a\u662f\u975e\u6b64\u5373\u5f7c\u7684\u9009\u62e9\u3002\u5f88\u591a\u7cfb\u7edf\u4e2d\u4f1a\u540c\u65f6\u4f7f\u7528\u4e24\u8005\uff0c\u9488\u5bf9\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u901a\u4fe1\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p><strong>\u4e92\u52a8\u65f6\u95f4<\/strong>\uff1a\u4f60\u4eec\u5728\u9879\u76ee\u4e2d\u662f\u5982\u4f55\u9009\u62e9\u4f7f\u7528 HTTP \u8fd8\u662f RPC \u7684\u5462\uff1f\u9047\u5230\u8fc7\u54ea\u4e9b\u56f0\u96be\u6216\u6709\u8da3\u7684\u60c5\u51b5\uff1f\u6b22\u8fce\u5206\u4eab\uff01<\/p>\n\n\n\n<p><strong>6. \u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u672c\u6587\uff0c\u6211\u4eec\u4ece\u5404\u4e2a\u7ef4\u5ea6\u5206\u6790\u548c\u5bf9\u6bd4\u4e86 HTTP \u548c RPC\uff0c\u901a\u8fc7\u4e0a\u9762\u7684\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>HTTP<\/strong>\u00a0\u66f4\u9002\u5408\u8d44\u6e90\u5bfc\u5411\u7684\u901a\u4fe1\uff0c\u5177\u6709\u5e7f\u6cdb\u7684\u517c\u5bb9\u6027\u548c\u6613\u7528\u6027\u3002<\/li>\n\n\n\n<li><strong>RPC<\/strong>\u00a0\u66f4\u9002\u5408\u670d\u52a1\u5bfc\u5411\u7684\u901a\u4fe1\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u66f4\u81ea\u7136\u7684\u8c03\u7528\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1aHTTP \u4e0e RPC\u7684\u533a\u522b\uff01 \u4eca\u5929\u6211\u4eec\u4e00\u8d77\u6765\u804a\u804a\u6dd8\u5b9d1\u9762\u7684\u4e00\u4e2a\u95ee\u9898\uff1aHTTP \u4e0e RPC\u7684\u533a\u522b\u3002HTTP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,10],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17006"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17006"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17006\/revisions"}],"predecessor-version":[{"id":17007,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17006\/revisions\/17007"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}