{"id":3999,"date":"2018-06-25T20:27:56","date_gmt":"2018-06-25T12:27:56","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3999"},"modified":"2018-06-25T20:27:56","modified_gmt":"2018-06-25T12:27:56","slug":"apache-thrift%e5%ad%a6%e4%b9%a0%e5%85%a5%e9%97%a8%e4%b9%8b%e4%bb%a3%e7%a0%81%e6%bc%94%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3999.html","title":{"rendered":"Apache Thrift\u5b66\u4e60\u5165\u95e8\u4e4b\u4ee3\u7801\u6f14\u793a"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u4e4b\u524d\u5728\u5b66\u4e60Apache Thrift\u7684\u8fc7\u7a0b\u4e2d\u8bb0\u5f55\u4e86\u4e00\u7bc7\u6587\u7ae0\uff0c\u4f46\u6ca1\u6709\u5305\u542b\u4ee3\u7801\u6f14\u793a\uff0c\u8fd9\u6b21\u52a0\u4e0a\u53c2\u8003\u6587\u7ae0\u4e2d\u9a8c\u8bc1\u8fc7\u53ef\u7528\u7684Java\u4ee3\u7801\uff0c\u540c\u65f6\u8865\u4e0a\u5176\u5b83\u8bed\u8a00\uff08\u8fd9\u91cc\u662fPython\uff09\u7684client\u7aef\u8c03\u7528\u4ee3\u7801\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p><span style=\"color: #ff0000;\"><strong>1. \u5148\u521b\u5efa Thrift(IDL) \u6587\u4ef6\uff0c\u540c\u65f6\u751f\u6210\u4ee3\u7801<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">namespace java com.ixyzero.learn.thrift \/\/\u8fd9\u4e00\u884c\u91cc\u9762\u7684java\u5173\u952e\u5b57\u5fc5\u4e0d\u53ef\u5c11\uff0c\u5426\u5219\u751f\u6210Java\u4ee3\u7801\u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u62a5\u9519\r\n\r\nservice HelloWorldService {\r\n  string sayHello(1:string username)\r\n}<\/pre>\n<p>\u6211\u9047\u5230\u8fc7\u7684\u4e00\u4e9b\u62a5\u9519\u4fe1\u606f\uff1a<\/p>\n<pre class=\"lang:default decode:true \">(last token was 'service')\r\nsyntax error\r\nParser error during include pass.<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \">#\u751f\u6210Java\u7248\u672c\uff0c\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u751f\u6210 gen-java \u5b50\u76ee\u5f55\r\nthrift -r --gen java helloWorld.thrift\r\n\r\n#\u751f\u6210Python\u7248\u672c\uff0c\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u751f\u6210 gen-py \u5b50\u76ee\u5f55\r\nthrift -r --gen py helloWorld.thrift<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>2. \u5b9e\u73b0\u63a5\u53e3 Iface<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.thrift;\r\n\r\nimport org.apache.thrift.TException;\r\n\r\n\/**\r\n * Created by ixyzero.com on 2018\/5\/30.\r\n *\/\r\npublic class HelloWorldImpl implements HelloWorldService.Iface {\r\n\r\n    public HelloWorldImpl() {\r\n    }\r\n\r\n    @Override\r\n    public String sayHello(String username) throws TException {\r\n        long time1 = System.currentTimeMillis();\r\n        System.out.println(time1);\r\n        try {\r\n            Thread.sleep(1000L * 1);\r\n        } catch (InterruptedException e) {\r\n            e.printStackTrace();\r\n        }\r\n        System.out.println(System.currentTimeMillis());\r\n        System.out.println(System.currentTimeMillis() - time1);\r\n        return \"Hi, '\" + username + \"' welcome to thrift world\";\r\n    }\r\n}<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>3. \u7f16\u5199 Server\u7aef \u4ee3\u7801<\/strong><\/span><\/p>\n<p># Java \u7248\u672c<\/p>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.thrift;\r\n\r\nimport org.apache.thrift.TProcessor;\r\nimport org.apache.thrift.protocol.TBinaryProtocol;\r\nimport org.apache.thrift.server.TServer;\r\nimport org.apache.thrift.server.TSimpleServer;\r\nimport org.apache.thrift.transport.TServerSocket;\r\n\r\npublic class HelloServerDemo {\r\n    public static final int SERVER_PORT = 8090;\r\n\r\n    public void startServer() {\r\n        try {\r\n            System.out.println(\"HelloWorld TSimpleServer start ....\");\r\n\r\n            TProcessor tprocessor = new HelloWorldService.Processor&lt;HelloWorldService.Iface&gt;(new HelloWorldImpl());\r\n\r\n            \/\/ \u7b80\u5355\u7684\u5355\u7ebf\u7a0b\u670d\u52a1\u6a21\u578b\uff0c\u4e00\u822c\u7528\u4e8e\u6d4b\u8bd5\r\n            TServerSocket serverTransport = new TServerSocket(SERVER_PORT);\r\n            TServer.Args tArgs = new TServer.Args(serverTransport);\r\n            tArgs.processor(tprocessor);\r\n            tArgs.protocolFactory(new TBinaryProtocol.Factory());\r\n            \/\/ tArgs.protocolFactory(new TCompactProtocol.Factory());\r\n            \/\/ tArgs.protocolFactory(new TJSONProtocol.Factory());\r\n            TServer server = new TSimpleServer(tArgs);\r\n            server.serve();\r\n\r\n        } catch (Exception e) {\r\n            System.out.println(\"Server start error!!!\");\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n\r\n    public static void main(String[] args) {\r\n        HelloServerDemo server = new HelloServerDemo();\r\n        server.startServer();\r\n    }\r\n}<\/pre>\n<p># Python\u7248\u672c<\/p>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\r\n# coding=utf-8\r\n\r\nimport socket\r\nimport sys\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\nfrom helloWorld import HelloWorldService\r\nfrom helloWorld.ttypes import *\r\n\r\nclass HelloWorldServiceHandler:\r\n    def sayHello(self, msg):\r\n        ret = \"Received: \" + msg\r\n        print ret\r\n        return ret\r\n\r\nhandler = HelloWorldServiceHandler()\r\nprocessor = HelloWorldService.Processor(handler)\r\ntransport = TSocket.TServerSocket(\"localhost\", 9090)\r\ntfactory = TTransport.TBufferedTransportFactory()\r\npfactory = TBinaryProtocol.TBinaryProtocolFactory()\r\n\r\nserver = TServer.TSimpleServer(processor, transport, tfactory, pfactory)\r\n\r\nprint \"Starting thrift server in python...\"\r\nserver.serve()\r\nprint \"done!\"<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>4. \u7f16\u5199 Client\u7aef \u4ee3\u7801<\/strong><\/span><\/p>\n<p># Java\u7248\u672c<\/p>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.thrift;\r\n\r\nimport org.apache.thrift.TException;\r\nimport org.apache.thrift.protocol.TBinaryProtocol;\r\nimport org.apache.thrift.protocol.TProtocol;\r\nimport org.apache.thrift.transport.TSocket;\r\nimport org.apache.thrift.transport.TTransport;\r\nimport org.apache.thrift.transport.TTransportException;\r\n\r\npublic class HelloClientDemo {\r\n\r\n    public static final String SERVER_IP = \"localhost\";\r\n    public static final int SERVER_PORT = 8090;\r\n    public static final int TIMEOUT = 30000;\r\n\r\n    \/**\r\n     * @param userName\r\n     *\/\r\n    public void startClient(String userName) {\r\n        TTransport transport = null;\r\n        try {\r\n            transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);\r\n            \/\/ \u534f\u8bae\u8981\u548c\u670d\u52a1\u7aef\u4e00\u81f4\r\n            TProtocol protocol = new TBinaryProtocol(transport);\r\n            \/\/ TProtocol protocol = new TCompactProtocol(transport);\r\n            \/\/ TProtocol protocol = new TJSONProtocol(transport);\r\n            HelloWorldService.Client client = new HelloWorldService.Client(protocol);\r\n            transport.open();\r\n            long time1 = System.currentTimeMillis();\r\n            String result = client.sayHello(userName);\r\n            long time2 = System.currentTimeMillis();\r\n            System.out.print(\"spend \");\r\n            System.out.print(time2 - time1);\r\n            System.out.println(\" ms to call method client.sayHello()\");\r\n            System.out.println(\"Thrift client result =: \" + result);\r\n        } catch (TTransportException e) {\r\n            e.printStackTrace();\r\n        } catch (TException e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            if (null != transport) {\r\n                transport.close();\r\n            }\r\n        }\r\n    }\r\n\r\n    public static void main(String[] args) {\r\n        HelloClientDemo client = new HelloClientDemo();\r\n        client.startClient(\"china\");\r\n    }\r\n}<\/pre>\n<p># Python\u7248\u672c<\/p>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\r\n# coding=utf-8\r\n\r\nimport sys\r\n\r\nfrom thrift import Thrift\r\nfrom thrift.transport import TSocket\r\nfrom thrift.transport import TTransport\r\nfrom thrift.protocol import TBinaryProtocol\r\n\r\nfrom helloWorld import HelloWorldService\r\n\r\ntry:\r\n    socket = TSocket.TSocket('localhost', 8090) # 8090\u8c03\u7528\u7684Java\uff0c9090\u8c03\u7528\u7684Python\r\n    socket.setTimeout(2000)\r\n    # transport = TTransport.TFramedTransport(socket) #\u7528\u8fd9\u4e2a\u4f1a\u51fa\u73b0time out\u5f02\u5e38\r\n    transport = TTransport.TBufferedTransport(socket)  #\u7528\u8fd9\u4e2a\u5c31\u6ca1\u95ee\u9898\uff0c\u56e0\u4e3a\u548cServer\u7aef\u4e00\u81f4\r\n    protocol = TBinaryProtocol.TBinaryProtocol(transport)\r\n    client = HelloWorldService.Client(protocol)\r\n    transport.open()\r\n\r\n    msg = client.sayHello(\"hello\")\r\n    print msg\r\n    msg = client.sayHello(\"nihao\")\r\n    print msg\r\n\r\n    transport.close()\r\n\r\nexcept:\r\n    print \"{0}\".format(traceback.format_exc())<\/pre>\n<p>Server\u7aef \u548c Client\u7aef \u7684 transport \u8981\u4e00\u81f4\uff0cprotocol \u4e5f\u8981\u4e00\u81f4\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u4e00\u4e9b\u95ee\u9898\uff0c\u6bd4\u5982 time out \u3002<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/43578634\/php-and-python-thrift\">PHP and python thrift<\/a><\/li>\n<li><a href=\"https:\/\/www.kancloud.cn\/digest\/batu-go\/153528\">Thrift RPC \u4f7f\u7528\u6307\u5357\u5b9e\u6218(\u9644golang &amp; PHP\u4ee3\u7801)<\/a><\/li>\n<li><a href=\"https:\/\/mshk.top\/2014\/08\/golang-php-python-java-thrift\/\">Golang\u3001Php\u3001Python\u3001Java\u57fa\u4e8eThrift0.9.1\u5b9e\u73b0\u8de8\u8bed\u8a00\u8c03\u7528<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/dutsoft\/article\/details\/71178655\">Python Thrift\u793a\u4f8b<\/a># \u5148\u5199 IDL \u6587\u4ef6\uff0c\u518d\u7528Python\u5206\u522b\u5b9e\u73b0 Server\u7aef \u548c Client\u7aef<\/li>\n<li><a href=\"https:\/\/thrift.apache.org\/docs\/concepts\">https:\/\/thrift.apache.org\/docs\/concepts<\/a><\/li>\n<li><a href=\"http:\/\/outofmemory.cn\/code-snippet\/35355\/python-thrift-demo\">Python thrift\u4f7f\u7528\u793a\u4f8b<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/duanxz\/p\/5516558.html\">Apache Thrift\u5b66\u4e60\u4e4b\u4e00\uff08\u5165\u95e8\u53caJava\u5b9e\u4f8b\u6f14\u793a\uff09<\/a><\/li>\n<\/ul>\n<div>=END=<\/div>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u4e4b\u524d\u5728\u5b66\u4e60Apache Thrift\u7684\u8fc7\u7a0b\u4e2d\u8bb0\u5f55\u4e86\u4e00\u7bc7\u6587\u7ae0\uff0c\u4f46\u6ca1\u6709\u5305\u542b\u4ee3\u7801\u6f14\u793a\uff0c\u8fd9\u6b21\u52a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[61,8,1139],"class_list":["post-3999","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-java","tag-python","tag-thrift"],"views":4683,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3999"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3999\/revisions"}],"predecessor-version":[{"id":4000,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3999\/revisions\/4000"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}