{"id":24669,"date":"2024-11-21T12:45:26","date_gmt":"2024-11-21T04:45:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24669\/"},"modified":"2024-11-21T12:45:26","modified_gmt":"2024-11-21T04:45:26","slug":"tp6-think-swoole-rpc%e6%9c%8d%e5%8a%a1%e7%9a%84%e6%95%b0%e6%8d%ae%e5%8a%a0%e5%af%86%e4%b8%8e%e8%ba%ab%e4%bb%bd%e8%ae%a4%e8%af%81%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24669\/","title":{"rendered":"TP6 Think-Swoole RPC\u670d\u52a1\u7684\u6570\u636e\u52a0\u5bc6\u4e0e\u8eab\u4efd\u8ba4\u8bc1\u673a\u5236"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169708134665899.jpg\" class=\"aligncenter\" title=\"TP6 Think-Swoole RPC\u670d\u52a1\u7684\u6570\u636e\u52a0\u5bc6\u4e0e\u8eab\u4efd\u8ba4\u8bc1\u673a\u5236\u63d2\u56fe\" alt=\"TP6 Think-Swoole RPC\u670d\u52a1\u7684\u6570\u636e\u52a0\u5bc6\u4e0e\u8eab\u4efd\u8ba4\u8bc1\u673a\u5236\u63d2\u56fe\" \/><\/p>\n<p>TP6 Think-Swoole RPC\u670d\u52a1\u7684\u6570\u636e\u52a0\u5bc6\u4e0e\u8eab\u4efd\u8ba4\u8bc1\u673a\u5236<\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u8fdb\u884c\u8fdc\u7a0b\u8c03\u7528\uff0c\u4ee5\u5b9e\u73b0\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u4e92\u548c\u529f\u80fd\u8c03\u7528\u3002\u5728\u8fd9\u6837\u7684\u80cc\u666f\u4e0b\uff0cRPC\uff08Remote Procedure Call\uff09\u5c31\u6210\u4e86\u4e00\u79cd\u91cd\u8981\u7684\u901a\u4fe1\u65b9\u5f0f\u3002TP6 Think-Swoole\u6846\u67b6\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684RPC\u670d\u52a1\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u6570\u636e\u52a0\u5bc6\u4e0e\u8eab\u4efd\u8ba4\u8bc1\u673a\u5236\u6765\u4fdd\u969cRPC\u8c03\u7528\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>\u4e00\u3001\u6570\u636e\u52a0\u5bc6\u673a\u5236<\/p>\n<ol>\n<li>\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5<\/li>\n<\/ol>\n<p>\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u662f\u6307\u52a0\u89e3\u5bc6\u4f7f\u7528\u76f8\u540c\u5bc6\u94a5\u7684\u4e00\u7c7b\u52a0\u5bc6\u7b97\u6cd5\u3002\u5e38\u89c1\u7684\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u6709AES\u3001DES\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528TP6 Think-Swoole\u6846\u67b6\u4e2d\u7684thinkencrytionDriver\u7c7b\u6765\u5b9e\u73b0\u5bf9\u79f0\u52a0\u5bc6\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2aEncrypt\u7c7b\uff0c\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\uff1a<\/p>\n<pre>&lt;?php namespace appcommon;\n\nuse thinkencryptionDriver;\n\nclass Encrypt\n{\n    private static $key = 'Your Secret Key';\n\n    public static function encrypt($data)\n    {\n        $encrypter = new Driver('AES-256-CBC', self::$key);\n        return $encrypter-&gt;encrypt($data);\n    }\n\n    public static function decrypt($data)\n    {\n        $encrypter = new Driver('AES-256-CBC', self::$key);\n        return $encrypter-&amp;gt;decrypt($data);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728RPC\u8c03\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Encrypt\u7c7b\u6765\u5bf9\u9700\u8981\u52a0\u5bc6\u7684\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\uff1a<\/p>\n<pre>&lt;?php use appcommonEncrypt;\n\n$data = ['key' =&gt; 'value'];\n$encryptedData = Encrypt::encrypt(json_encode($data));<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5<\/li>\n<\/ol>\n<p>\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u662f\u6307\u52a0\u89e3\u5bc6\u4f7f\u7528\u4e0d\u540c\u5bc6\u94a5\u7684\u4e00\u7c7b\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u662fRSA\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528RSA\u6765\u5b9e\u73b0\u516c\u94a5\u52a0\u5bc6\u3001\u79c1\u94a5\u89e3\u5bc6\u7684\u64cd\u4f5c\uff0c\u5728RPC\u8c03\u7528\u4e2d\uff0c\u5ba2\u6237\u7aef\u4f7f\u7528\u670d\u52a1\u7aef\u7684\u516c\u94a5\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\uff0c\u670d\u52a1\u7aef\u4f7f\u7528\u79c1\u94a5\u5bf9\u6570\u636e\u8fdb\u884c\u89e3\u5bc6\u3002<\/p>\n<p>\u5728TP6 Think-Swoole\u6846\u67b6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528thinkencryptionDriver\u7c7b\u6765\u5b9e\u73b0\u975e\u5bf9\u79f0\u52a0\u5bc6\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2aEncrypt\u7c7b\uff0c\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u516c\u94a5\u52a0\u5bc6\u548c\u79c1\u94a5\u89e3\u5bc6\u7684\u64cd\u4f5c\uff1a<\/p>\n<pre>&lt;?php namespace appcommon;\n\nuse thinkencryptionDriver;\n\nclass Encrypt\n{\n    private static $publicKey = 'Your Public Key';\n    private static $privateKey = 'Your Private Key';\n\n    public static function encrypt($data)\n    {\n        $encrypter = new Driver('RSA', self::$publicKey);\n        return $encrypter-&gt;encrypt($data);\n    }\n\n    public static function decrypt($data)\n    {\n        $encrypter = new Driver('RSA', self::$privateKey);\n        return $encrypter-&amp;gt;decrypt($data);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728RPC\u8c03\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Encrypt\u7c7b\u6765\u5bf9\u9700\u8981\u52a0\u5bc6\u7684\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\uff1a<\/p>\n<pre>&lt;?php use appcommonEncrypt;\n\n$data = ['key' =&gt; 'value'];\n$encryptedData = Encrypt::encrypt(json_encode($data));<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e8c\u3001\u8eab\u4efd\u8ba4\u8bc1\u673a\u5236<\/p>\n<ol>\n<li>Token\u8ba4\u8bc1<\/li>\n<\/ol>\n<p>\u5728RPC\u8c03\u7528\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7Token\u6765\u8fdb\u884c\u8eab\u4efd\u8ba4\u8bc1\u3002\u5ba2\u6237\u7aef\u5728\u53d1\u8d77RPC\u8bf7\u6c42\u65f6\uff0c\u5c06Token\u4f5c\u4e3a\u8bf7\u6c42\u7684\u4e00\u90e8\u5206\u53d1\u9001\u7ed9\u670d\u52a1\u7aef\u3002\u670d\u52a1\u7aef\u5728\u8fdb\u884c\u8bf7\u6c42\u5904\u7406\u65f6\uff0c\u9a8c\u8bc1Token\u7684\u6709\u6548\u6027\uff0c\u5982\u679c\u9a8c\u8bc1\u901a\u8fc7\uff0c\u5219\u7ee7\u7eed\u5904\u7406\u8bf7\u6c42\uff0c\u5426\u5219\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528TP6 Think-Swoole\u6846\u67b6\u7684thinkacadeRequest\u7c7b\u6765\u83b7\u53d6\u8bf7\u6c42\u5934\u4e2d\u7684Token\uff0c\u5e76\u8fdb\u884c\u9a8c\u8bc1\uff1a<\/p>\n<pre>acadeRequest;\n\n$token = Request::header('Authorization');\nif($token !== 'Your Secret Token'){\n    \/\/ Token\u9a8c\u8bc1\u5931\u8d25\uff0c\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\n    return 'Invalid Token';\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>HTTPS\u534f\u8bae<\/li>\n<\/ol>\n<p>\u4f7f\u7528HTTPS\u534f\u8bae\u53ef\u4ee5\u786e\u4fdd\u901a\u4fe1\u8fc7\u7a0b\u7684\u5b89\u5168\u6027\uff0c\u53ef\u4ee5\u907f\u514d\u6570\u636e\u88ab\u7a83\u542c\u3001\u7be1\u6539\u548c\u4f2a\u9020\u3002\u5728TP6 Think-Swoole\u6846\u67b6\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6econfig\/swoole.php\u6587\u4ef6\u6765\u542f\u7528HTTPS\u534f\u8bae\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5728swoole.php\u6587\u4ef6\u4e2d\u5c06ssl_cert_file\u548cssl_key_file\u914d\u7f6e\u4e3aSSL\u8bc1\u4e66\u7684\u8def\u5f84\uff1a<\/p>\n<pre>&lt;?php return [\n    'host'              =&gt; '0.0.0.0',\n    'port'              =&amp;gt; 9501,\n    'ssl_cert_file'     =&amp;gt; 'path\/to\/ssl_cert_file',\n    'ssl_key_file'      =&amp;gt; 'path\/to\/ssl_key_file',\n    \/\/\u5176\u4ed6\u914d\u7f6e\u9879...\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\uff0cRPC\u8c03\u7528\u5c06\u901a\u8fc7HTTPS\u534f\u8bae\u8fdb\u884c\u5b89\u5168\u901a\u4fe1\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cTP6 Think-Swoole\u6846\u67b6\u63d0\u4f9b\u4e86\u6570\u636e\u52a0\u5bc6\u4e0e\u8eab\u4efd\u8ba4\u8bc1\u673a\u5236\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4fdd\u969cRPC\u8c03\u7528\u7684\u5b89\u5168\u6027\u3002\u901a\u8fc7\u4f7f\u7528\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u548c\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\uff1b\u901a\u8fc7Token\u8ba4\u8bc1\u548cHTTPS\u534f\u8bae\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u8eab\u4efd\u8ba4\u8bc1\u548c\u786e\u4fdd\u901a\u4fe1\u5b89\u5168\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u5b89\u5168\u673a\u5236\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fddRPC\u8c03\u7528\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>\u3010\u6ce8\u3011\u4ee5\u4e0a\u4ee3\u7801\u793a\u4f8b\u4ec5\u4e3a\u6f14\u793a\u793a\u4f8b\uff0c\u5b9e\u9645\u4f7f\u7528\u4e2d\u9700\u8981\u6839\u636e\u5177\u4f53\u4e1a\u52a1\u9700\u6c42\u8fdb\u884c\u4fee\u6539\u548c\u5b8c\u5584\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fTP6 Think-Swoole RPC\u670d\u52a1\u7684\u6570\u636e\u52a0\u5bc6\u4e0e\u8eab\u4efd\u8ba4\u8bc1\u673a\u5236\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TP6 Think-Swoole RPC\u670d\u52a1\u7684\u6570\u636e\u52a0\u5bc6\u4e0e\u8eab\u4efd\u8ba4\u8bc1\u673a\u5236 \u968f\u7740\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u8fdb\u884c\u8fdc\u7a0b\u8c03\u7528\uff0c\u4ee5\u5b9e\u73b0\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u4e92\u548c\u529f\u80fd\u8c03\u7528\u3002\u5728\u8fd9\u6837\u7684\u80cc\u666f\u4e0b\uff0cRPC\uff08Remote Procedure Call\uff09\u5c31\u6210\u4e86\u4e00\u79cd\u91cd\u8981\u7684\u901a\u4fe1\u65b9\u5f0f\u3002TP6 Think-Swoole\u6846\u67b6\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684RPC\u670d\u52a1\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u6570\u636e\u52a0\u5bc6\u4e0e\u8eab\u4efd\u8ba4\u8bc1\u673a\u5236\u6765\u4fdd\u969cRPC\u8c03\u7528\u7684\u5b89\u5168\u6027\u3002 \u4e00\u3001\u6570\u636e\u52a0\u5bc6\u673a\u5236 \u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5 \u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u662f\u6307\u52a0\u89e3\u5bc6\u4f7f\u7528\u76f8\u540c\u5bc6\u94a5\u7684\u4e00\u7c7b\u52a0\u5bc6\u7b97\u6cd5\u3002\u5e38\u89c1\u7684\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u6709AES\u3001DES\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528TP6 Think-Swoole\u6846\u67b6\u4e2d\u7684thinkencrytionDriver\u7c7b\u6765\u5b9e\u73b0\u5bf9\u79f0\u52a0\u5bc6\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2aEncrypt\u7c7b\uff0c\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\uff1a &lt;?php namespace appcommon; use thinkencryptionDriver; class Encrypt { private static $key = &#8216;Your Secret Key&#8217;; public static function encrypt($data) { $encrypter = new Driver(&#8216;AES-256-CBC&#8217;, self::$key); return $encrypter-&gt;encrypt($data); } public static function decrypt($data) { $encrypter = new Driver(&#8216;AES-256-CBC&#8217;, self::$key); return $encrypter-&amp;gt;decrypt($data); } } \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24669","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24669"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24669\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}