{"id":2045,"date":"2012-09-09T11:34:27","date_gmt":"2012-09-09T03:34:27","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=2045"},"modified":"2013-11-17T11:05:32","modified_gmt":"2013-11-17T03:05:32","slug":"thrift%e5%85%a5%e9%97%a8hello%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/2045.html","title":{"rendered":"thrift\u5165\u95e8hello\u793a\u4f8b"},"content":{"rendered":"<p>\u5f15\u7528\u7f51\u4e0a\u5bf9thrift\u7684\u4ecb\u7ecd\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nThrift\u6e90\u4e8e\u5927\u540d\u9f0e\u9f0e\u7684facebook\u4e4b\u624b\uff0c\u57282007\u5e74facebook\u63d0\u4ea4Apache\u57fa\u91d1\u4f1a\u5c06Thrift\u4f5c\u4e3a\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\r\n\u5bf9\u4e8e\u5f53\u65f6\u7684facebook\u6765\u8bf4\u521b\u9020thrift\u662f\u4e3a\u4e86\u89e3\u51b3facebook\u7cfb\u7edf\u4e2d\u5404\u7cfb\u7edf\u95f4\u5927\u6570\u636e\u91cf\u7684\u4f20 \u8f93\u901a\u4fe1\u4ee5\u53ca\u7cfb\u7edf\u4e4b\u95f4\u8bed\u8a00\u73af\u5883\u4e0d\u540c\u9700\u8981\u8de8\u5e73\u53f0\u7684\u7279\u6027\u3002\r\n\u6240\u4ee5thrift\u53ef\u4ee5\u652f\u6301\u591a\u79cd\u7a0b\u5e8f\u8bed\u8a00\uff0c\u4f8b\u5982:  C++, C#, Cocoa, Erlang, Haskell, Java, Ocami, Perl, PHP, Python, Ruby, Smalltalk.\r\n\u5728\u591a\u79cd\u4e0d\u540c\u7684\u8bed\u8a00\u4e4b\u95f4\u901a\u4fe1thrift\u53ef\u4ee5\u4f5c\u4e3a\u4e8c\u8fdb\u5236\u7684\u9ad8\u6027\u80fd\u7684\u901a\u8baf\u4e2d\u95f4\u4ef6\uff0c\u652f\u6301\u6570\u636e(\u5bf9\u8c61)\u5e8f\u5217\u5316\u548c\u591a\u79cd\u7c7b\u578b\u7684RPC\u670d\u52a1\u3002\r\nThrift\u9002\u7528\u4e8e\u7a0b\u5e8f\u5bf9\u7a0b \u5e8f\u9759\u6001\u7684\u6570\u636e\u4ea4\u6362\uff0c\u9700\u8981\u5148\u786e\u5b9a\u597d\u4ed6\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ed6\u662f\u5b8c\u5168\u9759\u6001\u5316\u7684\uff0c\u5f53\u6570\u636e\u7ed3\u6784\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5fc5\u987b\u91cd\u65b0\u7f16\u8f91IDL\u6587\u4ef6\uff0c\r\n\u4ee3\u7801\u751f\u6210\uff0c\u518d\u7f16\u8bd1\u8f7d\u5165\u7684\u6d41\u7a0b\uff0c\u8ddf\u5176\u4ed6IDL\u5de5\u5177\u76f8\u6bd4\u8f83\u53ef\u4ee5\u89c6\u4e3a\u662fThrift\u7684\u5f31\u9879\uff0cThrift\u9002\u7528\u4e8e\u642d\u5efa\u5927\u578b\u6570\u636e\u4ea4\u6362\u53ca\u5b58\u50a8\u7684\u901a\u7528\u5de5\u5177\uff0c\r\n\u5bf9\u4e8e\u5927\u578b\u7cfb\u7edf\u4e2d\u7684\u5185\u90e8\u6570\u636e\u4f20\u8f93\u76f8\u5bf9\u4e8eJSON\u548cxml\u65e0\u8bba\u5728\u6027\u80fd\u3001\u4f20\u8f93\u5927\u5c0f\u4e0a\u6709\u660e\u663e\u7684\u4f18\u52bf\u3002\r\n<\/pre>\n<p>\u4e3b\u8981\u6839\u636e\u8fd9\u91cc\u7684\u6559\u7a0b\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhttp:\/\/www.thrift.pl\/Thrift-tutorial-our-own-hello-world.html\r\n<\/pre>\n<p>\u8fd9\u91cc\u9762\u7684\u6709\u51e0\u5904\u9519\u8bef\uff0c\u53ef\u80fd\u662f\u8001\u7248\u672c\u7684thrift\u3002<\/p>\n<p>\u9996\u5148\u4e0b\u8f7d\u5b89\u88c5thrift\u6700\u65b0\u7248\u672c\u3002\u7f16\u8bd1\u5b89\u88c5\u6b65\u9aa4\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev\r\nwget -c https:\/\/dist.apache.org\/repos\/dist\/release\/thrift\/0.8.0\/thrift-0.8.0.tar.gz\r\ntar -zvxf thrift-0.8.0.tar.gz\r\ncd thrift-0.8.0\r\n.\/configure\r\nmake\r\nsudo make install\r\n<\/pre>\n<p>\u5b89\u88c5\u597d\u540e\uff0c\u5b89\u88c5\u76f8\u5e94\u7684python\u5e93\uff0c\u4e0d\u7136\u5728\u8fd0\u884cpython\u5b9e\u4f8b\u7684\u65f6\u5019\u4f1a\u63d0\u793a\u201cImportError: No module named Thrift\u201d\u9519\u8bef\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncd thrift-0.8.0\/lib\/py\r\nsudo python setup.py install\r\n<\/pre>\n<p>ok\uff0c\u73b0\u5728\u6211\u4eec\u6765\u751f\u6210\u4e00\u4e2athrift\u6587\u4ef6\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u4e0e\u65b9\u6cd5\uff0c\u5728thrift-0.8.0\u6587\u4ef6\u5939\u4e0b\u9762\u521b\u5efahello.thrift\u6587\u4ef6\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nnamespace php hello\r\n\r\nenum SexType {\r\n    MALE = 1,\r\n    FEMALE = 2\r\n}\r\n\r\nstruct User {\r\n    1: string firstname,\r\n    2: string lastname,\r\n    3: i32 user_id,\r\n    4: SexType sex,\r\n    5: bool active = false,\r\n    6: optional string description\r\n}\r\n\r\nexception InvalidValueException {\r\n    1: i32 error_code,\r\n    2: string error_msg\r\n}\r\n\r\nservice UserManager {\r\n    void ping(),\r\n    i32 add_user(1:User u) throws (1: InvalidValueException e),\r\n    User get_user(1:i32 uid) throws (1: InvalidValueException e),\r\n    oneway void clear_list()\r\n}\r\n<\/pre>\n<p>\u9996\u5148\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5728php\u4ee3\u7801\u4e0b\u7684\u547d\u540d\u7a7a\u95f4\uff0cnamespace php hello\u3002\u8fd9\u6837\u5728\u751f\u6210\u76f8\u5e94\u7684php\u4ee3\u7801\u540e\uff0cphp\u7c7b\u540d\u524d\u52a0\u6709hello_\u7684\u524d\u7f00\u3002<br \/>\n\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6027\u522b\u7684\u679a\u4e3e\u7c7b\u578bSexType\u3002<br \/>\n\u518d\u4e0b\u9762\u5c31\u662f\u7ed3\u6784\u4f53User\uff0c\u5728\u5b9a\u4e49\u5b57\u6bb5\u7684\u65f6\u5019\uff0c\u6bcf\u4e2a\u5b57\u6bb5\u524d\u5fc5\u987b\u52a0\u4e00\u4e2a\u5e8f\u53f7\uff0c\u540e\u9762\u6839\u6570\u636e\u7c7b\u578b\u3002\u53ef\u4ee5\u7ed9\u5b57\u6bb5\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u5982bool active = false\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6240\u6709\u5b57\u6bb5\u90fd\u4e3a\u5fc5\u9700\uff0c\u8981\u4f7f\u5176\u4e3a\u53ef\u9009\uff0c\u53ef\u52a0optional\u4fee\u9970\u7b26\u3002<br \/>\nexception InvalidValueException \u5b9a\u4e49\u4e86\u4e00\u4e2a\u5f02\u5e38\uff0c\u5f53\u4f20\u9012\u7684\u53c2\u6570\u4e0d\u5408\u6cd5\u65f6\uff0c\u5c31\u629b\u51fa\u6b64\u5f02\u5e38\u3002<br \/>\n\u63a5\u4e0b\u6765\u5b9a\u4e49\u4e86\u4e00\u4e2a\u670d\u52a1\u7c7b UserManager\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u65b9\u6cd5\u3002\u683c\u5f0f\u6839\u6211\u4eec\u7f16\u7a0b\u5b9a\u4e49\u7684\u7c7b\u4f3c\uff0c\u5f88\u5bb9\u6613\u770b\u61c2\u3002<br \/>\n\u6700\u540e\u4e00\u4e2a\u65b9\u6cd5\u52a0\u4e86oneway\uff0c\u8868\u540d\u5728\u5ba2\u6237\u7aef\u8c03\u7528\u6b64\u65b9\u6cd5\u65f6\uff0c\u4e0d\u7528\u7b49\u5f85\u670d\u52a1\u7aef\u8fd4\u56de\u5c31\u7acb\u9a6c\u7ed3\u675f\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u7528thrift\u547d\u540d\u6765\u751f\u6210\u76f8\u5e94\u7684\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nthrift -r --gen php hello.thrift\r\nthrift -r --gen py hello.thrift\r\n<\/pre>\n<p>\u8fd9\u65f6\u5019\uff0c\u5728\u76ee\u5f55\u4e0b\u9762\u51fa\u73b0\u4e24\u4e2a\u76ee\u5f55\uff0cgen-py\u548cgen-php\uff0c\u5206\u522b\u4e3aphp\u4e0epython\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u6211\u4eec\u7528python\u6765\u5199\u670d\u52a1\u7aef\uff0c\u65b0\u5efapython_server.py\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python\r\n\r\nimport sys\r\nsys.path.append('.\/gen-py')\r\n\r\nfrom hello import UserManager\r\nfrom hello.ttypes import *\r\n\r\nfrom thrift.transport import TSocket\r\nfrom thrift.transport import TTransport\r\nfrom thrift.protocol import TBinaryProtocol\r\nfrom thrift.server import TServer\r\n\r\nusers = &#x5B;]\r\n\r\nclass UserManagerHandler:\r\n\tdef __init__(self):\r\n\t\tpass\r\n\t\t#self.log = {}\r\n\r\n\tdef ping(self):\r\n\t\tprint 'ping()'\r\n\r\n\tdef add_user(self, user):\r\n\t\tif user.firstname == None:\r\n\t\t\traise InvalidValueException(1,'no firstname exception')\r\n\t\tif user.lastname == None:\r\n\t\t\traise InvalidValueException(2, 'no lastname exception')\r\n\t\tif user.user_id &lt;= 0:\r\n\t\t\traise InvalidValueException(3, 'wrong user_id')\r\n\t\tif user.sex != SexType.MALE and user.sex != SexType.FEMALE:\r\n\t\t\traise InvalidValueException(4, 'wrong sex id')\r\n\t\tprint 'Processing user '+user.firstname+' '+user.lastname\r\n\t\tusers.append(user)\r\n\t\tprint users\r\n\t\treturn True\r\n\r\n\tdef get_user(self, user_id):\r\n\t\tif user_id &lt; 0:\r\n\t\t\traise InvalidValueException(5, 'wrong id')\r\n\t\treturn users&#x5B;user_id]\r\n\r\n\tdef clear_list(self):\r\n\t\tprint 'Clearing list'\r\n\t\tprint users\r\n\t\tdel users &#x5B;:]\r\n\t\tprint users\r\n\r\nhandler = UserManagerHandler()\r\nprocessor = UserManager.Processor(handler)\r\ntransport = TSocket.TServerSocket(port=9090)\r\ntfactory = TTransport.TBufferedTransportFactory()\r\npfactory = TBinaryProtocol.TBinaryProtocolFactory()\r\n\r\nserver = TServer.TSimpleServer(processor, transport, tfactory, pfactory)\r\n\r\n# You could do one of these for a multithreaded server\r\n#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)\r\n#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)\r\n\r\nprint 'Starting the server...'\r\nserver.serve()\r\nprint 'done.'\r\n<\/pre>\n<p>sys.path.append(&#8216;.\/gen-py&#8217;) \u8fd9\u53e5\u8bdd\u662f\u628a\u6211\u4eec\u4e4b\u524d\u7528thrift\u751f\u6210\u7684python\u7c7b\u4ee3\u7801\u5f15\u7528\u8fdb\u6765\u3002<br \/>\n\u7136\u540e\u6211\u4eec\u8fd0\u884c\u6b64\u670d\u52a1\u7aef\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nchmod +x .\/python_server.py\r\n.\/python_server.py\r\n<\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u7528php\u6765\u5b9e\u73b0\u5ba2\u6237\u7aef\uff0c\u5f53\u6267\u884cphp\u9875\u9762\u540e\uff0cphp\u53bb\u8fde\u63a5python\u7684\u670d\u52a1\u7aef\uff0c\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u7b49\u3002<\/p>\n<p>\u5728apache\u7f51\u7ad9\u6839\u76ee\u5f55\u4e0b\u9762\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\u6765\u653e\u793a\u4f8bphp\u4ee3\u7801\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncd ~\/www\/\r\nmkdir thrift-php\r\ncd thrift-php\r\ncp -r ~\/thrift-0.8.0\/lib\/php\/src\/ .   #\u8981\u7528\u5230thrift\u7684php\u7c7b\u5e93\r\ncd src\r\nmkdir packages\r\ncd packages\r\ncp -r ~\/thrift-0.8.0\/gen-php\/hello\/ .  #\u5c06\u6211\u4eec\u4e4b\u524d\u751f\u6210\u7684hello\u7684php\u90e8\u5206\u4ee3\u7801\u590d\u5236\u8fc7\u6765\r\n<\/pre>\n<p>\u7136\u540e\u6211\u4eec\u5728thrift-php\u76ee\u5f55\u4e0b\u9762\u65b0\u5efahello.php\u6587\u4ef6\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n$GLOBALS&#x5B;'THRIFT_ROOT'] = 'src';\r\n\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/Thrift.php';\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/protocol\/TBinaryProtocol.php';\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/transport\/TSocket.php';\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/transport\/THttpClient.php';\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/transport\/TBufferedTransport.php';\r\n\r\nrequire_once $GLOBALS&#x5B;'THRIFT_ROOT'].'\/packages\/hello\/UserManager.php';\r\ntry {\r\n\r\n\t$socket = new TSocket('localhost', 9090);\r\n\t$transport = new TBufferedTransport($socket, 1024, 1024);\r\n\t$protocol = new TBinaryProtocol($transport);\r\n\t$client = new hello_UserManagerClient($protocol);\r\n\r\n\t$transport-&gt;open();\r\n\t$client-&gt;ping();\r\n\t$u = new hello_User();\r\n\t$u-&gt;user_id = 1;\r\n\t$u-&gt;firstname = 'John';\r\n\t$u-&gt;lastname = 'Smith';\r\n\t$u-&gt;sex = SexType::MALE;\r\n\tif ($client-&gt;add_user($u))\r\n\t{\r\n\t\techo 'user added succesfully&lt;\/br&gt;';\r\n\t}\r\n\r\n\tvar_dump($client-&gt;get_user(0));\r\n\r\n\t$client-&gt;clear_list();\r\n\r\n\t$u2 = new hello_User();\r\n\t$client-&gt;add_user($u2);\r\n\r\n} catch (hello_InvalidValueException $e) {\r\n\techo $e-&gt;error_msg.'&lt;br\/&gt;';\r\n}\r\n?&gt;\r\n<\/pre>\n<p>\u73b0\u5728\u5c31\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u8fd0\u884c\u6211\u4eec\u7684php\u5ba2\u6237\u7aef\u4e86\uff0c\u6253\u5f00 http:\/\/127.0.0.1\/thrift-php\/hello.php\uff0c\u663e\u793a\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nuser added succesfully&lt;\/br&gt;object(hello_User)#7 (6) {\r\n  &#x5B;&quot;firstname&quot;]=&gt;\r\n  string(4) &quot;John&quot;\r\n  &#x5B;&quot;lastname&quot;]=&gt;\r\n  string(5) &quot;Smith&quot;\r\n  &#x5B;&quot;user_id&quot;]=&gt;\r\n  int(1)\r\n  &#x5B;&quot;sex&quot;]=&gt;\r\n  int(1)\r\n  &#x5B;&quot;active&quot;]=&gt;\r\n  bool(false)\r\n  &#x5B;&quot;description&quot;]=&gt;\r\n  NULL\r\n}\r\nno firstname exception\r\n<\/pre>\n<p>\u5728python\u7684\u670d\u52a1\u5668\u8f93\u51fa\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nStarting the server...\r\nping()\r\nProcessing user John Smith\r\n&#x5B;User(user_id=1, description=None, firstname='John', lastname='Smith', sex=1, active=False)]\r\nClearing list\r\n&#x5B;User(user_id=1, description=None, firstname='John', lastname='Smith', sex=1, active=False)]\r\n&#x5B;]\r\n<\/pre>\n<p>ok\uff0c\u6700\u57fa\u672c\u7684hello\u4f8b\u5b50\u5c31\u5b8c\u6210\u4e86\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\u8fd8\u80fd\u627e\u5230\u66f4\u52a0\u4e30\u5bcc\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhttps:\/\/github.com\/apache\/thrift\/tree\/trunk\/tutorial\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u7528\u7f51\u4e0a\u5bf9thrift\u7684\u4ecb\u7ecd\uff1a Thrift\u6e90\u4e8e\u5927\u540d\u9f0e\u9f0e\u7684facebook\u4e4b\u624b\uff0c\u57282007\u5e74facebook\u63d0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2045","post","type-post","status-publish","format-standard","hentry","category-diary"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=2045"}],"version-history":[{"count":5,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2045\/revisions"}],"predecessor-version":[{"id":4488,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2045\/revisions\/4488"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=2045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=2045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=2045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}