{"id":1040,"date":"2019-10-25T13:34:17","date_gmt":"2019-10-25T05:34:17","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=1040"},"modified":"2023-12-04T16:31:28","modified_gmt":"2023-12-04T08:31:28","slug":"rpc%e8%bf%9c%e7%a8%8b%e8%b0%83%e7%94%a8%e5%8e%9f%e7%90%86%e5%8f%8aphp%e5%ba%94%e7%94%a8%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=1040","title":{"rendered":"RPC(\u8fdc\u7a0b\u8c03\u7528)\u539f\u7406\u53caPHP\u5e94\u7528\u793a\u4f8b"},"content":{"rendered":"<h3>1. rpc\u76f8\u5173\u77e5\u8bc6\u51c6\u5907<\/h3>\n<p>RPC\uff08Remote Procedure Call Protocol\uff09\u2014\u2014\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u534f\u8bae\uff0c\u5b83\u662f\u4e00\u79cd\u901a\u8fc7\u7f51\u7edc\u4ece\u8fdc\u7a0b\u8ba1\u7b97\u673a\u7a0b\u5e8f\u4e0a\u8bf7\u6c42\u670d\u52a1\uff0c\u800c\u4e0d\u9700\u8981\u4e86\u89e3\u5e95\u5c42\u7f51\u7edc\u6280\u672f\u7684\u534f\u8bae\u3002RPC\u534f\u8bae\u5047\u5b9a\u67d0\u4e9b\u4f20\u8f93\u534f\u8bae\u7684\u5b58\u5728\uff0c\u5982TCP\u6216UDP\uff0c\u4e3a\u901a\u4fe1\u7a0b\u5e8f\u4e4b\u95f4\u643a\u5e26\u4fe1\u606f\u6570\u636e\u3002\u5728OSI\u7f51\u7edc\u901a\u4fe1\u6a21\u578b\u4e2d\uff0cRPC\u8de8\u8d8a\u4e86\u4f20\u8f93\u5c42\u548c\u5e94\u7528\u5c42\u3002RPC\u4f7f\u5f97\u5f00\u53d1\u5305\u62ec\u7f51\u7edc\u5206\u5e03\u5f0f\u591a\u7a0b\u5e8f\u5728\u5185\u7684\u5e94\u7528\u7a0b\u5e8f\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<p>RPC\u91c7\u7528\u5ba2\u6237\u673a\/\u670d\u52a1\u5668\u6a21\u5f0f\u3002\u8bf7\u6c42\u7a0b\u5e8f\u5c31\u662f\u4e00\u4e2a\u5ba2\u6237\u673a\uff0c\u800c\u670d\u52a1\u63d0\u4f9b\u7a0b\u5e8f\u5c31\u662f\u4e00\u4e2a\u670d\u52a1\u5668\u3002\u9996\u5148\uff0c\u5ba2\u6237\u673a\u8c03\u7528\u8fdb\u7a0b\u53d1\u9001\u4e00\u4e2a\u6709\u8fdb\u7a0b\u53c2\u6570\u7684\u8c03\u7528\u4fe1\u606f\u5230\u670d\u52a1\u8fdb\u7a0b\uff0c\u7136\u540e\u7b49\u5f85\u5e94\u7b54\u4fe1\u606f\u3002\u5728\u670d\u52a1\u5668\u7aef\uff0c\u8fdb\u7a0b\u4fdd\u6301\u7761\u7720\u72b6\u6001\u76f4\u5230\u8c03\u7528\u4fe1\u606f\u5230\u8fbe\u4e3a\u6b62\u3002\u5f53\u4e00\u4e2a\u8c03\u7528\u4fe1\u606f\u5230\u8fbe\uff0c\u670d\u52a1\u5668\u83b7\u5f97\u8fdb\u7a0b\u53c2\u6570\uff0c\u8ba1\u7b97\u7ed3\u679c\uff0c\u53d1\u9001\u7b54\u590d\u4fe1\u606f\uff0c\u7136\u540e\u7b49\u5f85\u4e0b\u4e00\u4e2a\u8c03\u7528\u4fe1\u606f\uff0c\u6700\u540e\uff0c\u5ba2\u6237\u7aef\u8c03\u7528\u8fdb\u7a0b\u63a5\u6536\u7b54\u590d\u4fe1\u606f\uff0c\u83b7\u5f97\u8fdb\u7a0b\u7ed3\u679c\uff0c\u7136\u540e\u8c03\u7528\u6267\u884c\u7ee7\u7eed\u8fdb\u884c\u3002<\/p>\n<h4>1.1 \u8fdc\u7a0b\u8c03\u7528\u539f\u7406<\/h4>\n<p>\u6bd4\u5982 A (client) \u8c03\u7528 B (server) \u63d0\u4f9b\u7684remoteAdd\u65b9\u6cd5\uff1a<\/p>\n<p>\u9996\u5148A\u4e0eB\u4e4b\u95f4\u5efa\u7acb\u4e00\u4e2aTCP\u8fde\u63a5\uff1b<br \/>\n\u7136\u540eA\u628a\u9700\u8981\u8c03\u7528\u7684\u65b9\u6cd5\u540d\uff08\u8fd9\u91cc\u662fremoteAdd\uff09\u4ee5\u53ca\u65b9\u6cd5\u53c2\u6570\uff0810\uff0c20\uff09\u5e8f\u5217\u5316\u6210\u5b57\u8282\u6d41\u53d1\u9001\u51fa\u53bb\uff1b<br \/>\nB\u63a5\u53d7A\u53d1\u9001\u8fc7\u6765\u7684\u5b57\u8282\u6d41\uff0c\u7136\u540e\u53cd\u5e8f\u5217\u5316\u5f97\u5230\u76ee\u6807\u65b9\u6cd5\u540d\uff0c\u65b9\u6cd5\u53c2\u6570\uff0c\u63a5\u7740\u6267\u884c\u76f8\u5e94\u7684\u65b9\u6cd5\u8c03\u7528\uff08\u53ef\u80fd\u662flocalAdd\uff09\u5e76\u628a\u7ed3\u679c30\u8fd4\u56de\uff1b<br \/>\nA\u63a5\u53d7\u8fdc\u7a0b\u8c03\u7528\u7ed3\u679c,\u8f93\u51fa30\u3002<\/p>\n<p><strong>RPC\u6846\u67b6\u5c31\u662f\u628a\u6211\u521a\u624d\u8bf4\u7684\u8fd9\u51e0\u70b9\u4e9b\u7ec6\u8282\u7ed9\u5c01\u88c5\u8d77\u6765\uff0c\u7ed9\u7528\u6237\u66b4\u9732\u7b80\u5355\u53cb\u597d\u7684API\u4f7f\u7528\u3002<\/strong><\/p>\n<h4>1.2 \u8fdc\u7a0b\u8c03\u7528\u548cSocket Rest\u7684\u533a\u522b<\/h4>\n<p>RPC\u3001Socket\u3001Rest\u90fd\u662f\u662fclient\/server\u6a21\u5f0f\u7684<\/p>\n<p>\u8ddfSocket\u4ec0\u4e48\u533a\u522b\uff1fsocket\u662fRPC\u7ecf\u5e38\u91c7\u7528\u7684\u901a\u4fe1\u624b\u6bb5\u4e4b\u4e00\uff0cRPC\u662f\u5728Socket\u7684\u57fa\u7840\u4e0a\u5b9e\u73b0\u7684\uff0c\u5b83\u6bd4socket\u9700\u8981\u66f4\u591a\u7684\u7f51\u7edc\u548c\u7cfb\u7edf\u8d44\u6e90\u3002\u9664\u4e86Socket\uff0cRPC\u8fd8\u6709\u5176\u4ed6\u7684\u901a\u4fe1\u65b9\u6cd5\uff0c\u6bd4\u5982\uff1ahttp\u3001\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5e26\u7684\u7ba1\u9053\u7b49\u6280\u672f\u6765\u5b9e\u73b0\u5bf9\u4e8e\u8fdc\u7a0b\u7a0b\u5e8f\u7684\u8c03\u7528\u3002<br \/>\n2.\u8ddfRest\u4ec0\u4e48\u533a\u522b\uff1fREST API \u662f\u57fa\u4e8e HTTP\u534f\u8bae\u7684\uff0cREST\u81f4\u529b\u4e8e\u901a\u8fc7http\u534f\u8bae\u4e2d\u7684POST\/GET\/PUT\/DELETE\u7b49\u65b9\u6cd5\u548c\u4e00\u4e2a\u53ef\u8bfb\u6027\u5f3a\u7684URL\u6765\u63d0\u4f9b\u4e00\u4e2ahttp\u8bf7\u6c42\u3002\u800c RPC \u5219\u53ef\u4ee5\u4e0d\u57fa\u4e8e HTTP\u534f\u8bae<br \/>\n\u56e0\u6b64\uff0c\u5982\u679c\u662f\u540e\u7aef\u4e24\u79cd\u8bed\u8a00\u4e92\u76f8\u8c03\u7528\uff0c\u7528 RPC \u53ef\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\uff08\u7701\u53bb\u4e86 HTTP \u62a5\u5934\u7b49\u4e00\u7cfb\u5217\u4e1c\u897f\uff09\uff0c\u5e94\u8be5\u4e5f\u66f4\u5bb9\u6613\u914d\u7f6e\u3002<\/p>\n<h3>2. \u6d41\u884c\u7684rpc\u6846\u67b6<\/h3>\n<p>phprpc\uff0cyar, thrift, gRPC, swoole, hprose<\/p>\n<h4>2.1 phprpc\u5b9e\u4f8b<\/h4>\n<p>\u5148\u6765\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0cphprpc\u4e5f\u662f\u5206\u4e3a\u670d\u52a1\u5668\u7aef\u548c\u5ba2\u6237\u7aef\u7684\u3002\u6240\u4ee5\u6587\u4ef6\u5939\u4e2d\u5bf9\u5e94\u7684\u5c31\u662fphprpc_server.php \u548c phprpc_client.php<\/p>\n<p>phprpc_client.php<\/p>\n<pre><code class=\"language-php line-numbers\">&lt;?php\ninclude (\"phprpc\/phprpc_client.php\");\n$client = new PHPRPC_Client('http:\/\/127.0.0.1\/server.php');\necho $client-&gt;HelloWorld();\n<\/code><\/pre>\n<p>phprpc_server.php<\/p>\n<pre><code class=\"language-php line-numbers\">&lt;?php\ninclude (\"phprpc\/phprpc_server.php\");\nfunction HelloWorld() {\n    return 'Hello World!';\n}\n$server = new PHPRPC_Server();\n$server-&gt;add('HelloWorld');\n$server-&gt;start();\n<\/code><\/pre>\n<h4>2.2 yar\u5b9e\u4f8b<\/h4>\n<p>\u5148\u6765\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0cyar\u4e5f\u662f\u5206\u4e3a\u670d\u52a1\u5668\u7aef\u548c\u5ba2\u6237\u7aef\u7684\u3002\u6240\u4ee5\u6587\u4ef6\u5939\u4e2d\u5bf9\u5e94\u7684\u5c31\u662fyar_server.php \u548c yar_client.php<\/p>\n<p>yar_client.php<\/p>\n<pre><code class=\"language-php line-numbers\">&lt;?php\n$client = new Yar_Client(\"http:\/\/127.0.0.1\/yar_server.php\");\necho $client-&gt;api('helo word');\n<\/code><\/pre>\n<p>yar_server.php<\/p>\n<pre><code class=\"language-php line-numbers\">&lt;?php\nclass API {\n    public function api($parameter, $option = \"foo\") {\n        return $parameter;\n    }\n    protected function client_can_not_see() {\n    }\n}\n$service = new Yar_Server(new API());\n$service-&gt;handle();\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. rpc\u76f8\u5173\u77e5\u8bc6\u51c6\u5907 RPC\uff08Remote Procedure Call Protocol\uff09\u2014\u2014\u8fdc\u7a0b\u8fc7\u7a0b\u8c03 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[405],"class_list":["post-1040","post","type-post","status-publish","format-standard","hentry","category-15","tag-php"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/1040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1040"}],"version-history":[{"count":2,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/1040\/revisions"}],"predecessor-version":[{"id":1166,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/1040\/revisions\/1166"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}