{"id":4026,"date":"2018-07-07T17:37:24","date_gmt":"2018-07-07T09:37:24","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4026"},"modified":"2018-07-07T17:37:24","modified_gmt":"2018-07-07T09:37:24","slug":"apache-thrift%e5%ad%a6%e4%b9%a0%e5%85%a5%e9%97%a8%e4%b9%8b%e5%8c%85%e5%90%abstruct%e7%bb%93%e6%9e%84%e7%9a%84thrift%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4026.html","title":{"rendered":"Apache Thrift\u5b66\u4e60\u5165\u95e8\u4e4b\u5305\u542bstruct\u7ed3\u6784\u7684thrift\u6587\u4ef6"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u4e4b\u524d\u5728\u5b66\u4e60Thrift\u7684\u65f6\u5019\u6574\u7406\u4e86\u4e00\u7bc7\u300a<a href=\"https:\/\/ixyzero.com\/blog\/archives\/3999.html\">Apache Thrift\u5b66\u4e60\u5165\u95e8\u4e4b\u4ee3\u7801\u6f14\u793a<\/a>\u300b\u7684\u6587\u7ae0\uff0c\u57fa\u672c\u539f\u7406\u4ee5\u53ca\u7f16\u5199\u65b9\u6cd5\u91cc\u9762\u57fa\u672c\u4e0a\u90fd\u6709\uff0c\u4f46\u662f\u5bf9\u5e94\u7684.thrift\u6587\u4ef6\u975e\u5e38\u7b80\u5355\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u53ef\u80fd\u4e0d\u592a\u9002\u7528\uff0c\u6240\u4ee5\uff0c\u8fd9\u91cc\u628a\u6700\u8fd1\u7528\u5230\u7684\u5305\u542bstruct\u7ed3\u6784\u7684thrift\u6587\u4ef6\u53ca\u76f8\u5173\u6837\u4f8b\u4ee3\u7801\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\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>\u5047\u8bbe\u6709\u4e00\u4e2a\u573a\u666f\u2014\u2014\u9700\u8981\u7528thrift\u4f20map\uff0c\u4f46\u662fmap\u7684key\/value\u7c7b\u578b\u53c8\u4e0d\u56fa\u5b9a\uff0c\u5b58\u5728String\/Integer\u7b49\u591a\u79cd\u53ef\u80fd\u3002\u8fd9\u91cc\u4e3a\u4e86\u6f14\u793a\u65b9\u4fbf\u8d77\u89c1\uff0c\u5047\u8bbekey\u7684\u7c7b\u578b\u56fa\u5b9a\u4e3aString\uff0cvalue\u7684\u7c7b\u578b\u53ef\u80fd\u4e3aString\uff0c\u4e5f\u53ef\u80fd\u4e3aInteger\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre class=\"lang:default decode:true \">paraMap = {\r\n\t\"methdo\": \"GET\",\r\n\t\"actionType\": \"\u67e5\u8be2\",\r\n\t\"searchId\": 123456,\r\n\t\"status\": 200\r\n}<\/pre>\n<p>\u5728Java\u91cc\u9762\u53ef\u80fd\u8fd8\u6bd4\u8f83\u597d\u5904\u7406\uff0cnew\u4e00\u4e2aMap&lt;String, Object&gt;\u5bf9\u8c61\u5c31\u884c\u4e86\uff0cvalue\u968f\u4fbf\u662f\u4ec0\u4e48\u7c7b\u578b\u90fd\u53ef\u4ee5\u5e94\u5bf9\u3002\u4f46\u662fThrift\u91cc\u9762\u662f\u6ca1\u6709Object\u7c7b\u578b\u7684\uff0c\u6240\u4ee5\uff0c\u5bf9\u5e94\u7684.thrift\u6587\u4ef6\u8be5\u600e\u4e48\u5199\u5462\uff1f\u54a8\u8be2\u4e86\u522b\u4eba\u4e4b\u540e\uff0c\u4e86\u89e3\u5230\uff0c\u53ef\u4ee5\u901a\u8fc7struct\u6765\u95f4\u63a5\u5b9e\u73b0\uff1a<\/p>\n<pre class=\"lang:default decode:true\">namespace java com.ixyzero.learn.thrift\r\n\r\n\/**\r\n* \u5728Thrift\u4e2d\u5b9a\u4e49 struct \u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u7c7b\r\n* 1\u3001\u5c5e\u6027\u9700\u8981\u4e00\u4e2a\u8868\u793a\u6570\u5b57\uff08\u5982\uff1a1\uff0c2\uff0c3\uff09\uff1b\u8fd9\u4e2a\u6570\u5b57\u5728\u540c\u4e2a\u4f5c\u7528\u57df\u5185\u4e0d\u80fd\u91cd\u590d\uff1b\r\n* 2\u3001\u5c5e\u6027\u9ed8\u8ba4\u90fd\u662f\u5fc5\u8f93\u5c5e\u6027\uff0c\u5982\u679c\u9700\u8981\u5c5e\u6027\u53ef\u9009\u5728\u5c5e\u6027\u524d\u9762\u52a0\u4e0aoptional\u5173\u952e\u5b57\uff1b\r\n* 3\u3001\u53ef\u4ee5\u4e3a\u5c5e\u6027\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u683c\u5f0f\u662f\u5728\u5c5e\u6027\u540d\u79f0\u540e\u9762\u7528\u7b49\u53f7\u3002\u5982\uff1anum1 =0,0\u5c31\u662fnum1\u7684\u9ed8\u8ba4\u503c\uff1b\r\n**\/\r\nstruct value {\r\n    1: optional i32\tintValue;\r\n    2: optional string\tstrValue;\r\n}\r\n\r\nservice ParaMapService {\r\n  string sayHello(1:string username)\r\n\r\n  void ruleExecute1(1:map&lt;string, string&gt; paramMap)\r\n  void ruleExecute2(1:map&lt;string, i64&gt; paramMap)\r\n\r\n  void ruleExecuteX(1:map&lt;string, value&gt; paramMap)\r\n}<\/pre>\n<p>\u901a\u8fc7\u547d\u4ee4\u751f\u6210\u4ee3\u7801\u4e4b\u540e\uff0c\u5c06\u5bf9\u5e94\u7684ParaMapService.java\u6587\u4ef6\u4ee5\u53cavalue.java\u6587\u4ef6\u62f7\u8d1d\u5230\u81ea\u5df1\u7684\u9879\u76ee\u5bf9\u5e94\u8def\u5f84\u4e2d\u3002\u5e76\u7f16\u5199\u5b9e\u73b0\u63a5\u53e3Iface\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.thrift;\r\n\r\nimport org.apache.thrift.TException;\r\nimport java.util.Map;\r\n\r\npublic class ParaMapImpl implements ParaMapService.Iface {\r\n\r\n    public ParaMapImpl() {\r\n    }\r\n\r\n    @Override\r\n    public String sayHello(String username) throws TException {\r\n        return \"Hi,\" + username + \" welcome to thrift world\";\r\n    }\r\n\r\n    @Override\r\n    public void ruleExecute1(Map&lt;String, String&gt; paramMap) throws TException {\r\n        for (String item: paramMap.keySet()) {\r\n            System.out.println(\"Key: \" + item + \",\\tValue: \" + paramMap.get(item) + \",\\tType: \" + paramMap.get(item).getClass());\r\n        }\r\n    }\r\n\r\n    @Override\r\n    public void ruleExecute2(Map&lt;String, Long&gt; paramMap) throws TException {\r\n        for (String item: paramMap.keySet()) {\r\n            System.out.println(\"Key: \" + item + \",\\tValue: \" + paramMap.get(item) + \",\\tType: \" + paramMap.get(item).getClass());\r\n        }\r\n    }\r\n\r\n    @Override\r\n    public void ruleExecuteX(Map&lt;String, value&gt; paramMap) throws TException {\r\n        for (String item: paramMap.keySet()) {\r\n            int val = paramMap.get(item).getIntValue();\r\n            String strVal = paramMap.get(item).getStrValue();\r\n            if (val != 0) { \/\/ \u5bf9\u4e8e\u672c\u8eab\u503c\u4e3a0\u7684\u60c5\u51b5\u4f1a\u51fa\u73b0\u8bef\u5224\u2026\u2026\r\n                System.out.print(\"getIntValue() = \" + val + \", \");\r\n            } else {\r\n                System.out.print(\"getStrValue() = \" + strVal + \", \");\r\n            }\r\n\r\n            System.out.println(\"Key: \" + item + \",\\tValue: \" + paramMap.get(item) + \",\\tType: \" + paramMap.get(item).getClass());\r\n        }\r\n    }\r\n\r\n}<\/pre>\n<p>\u518d\u6765\u7f16\u5199Server\u7aef\u4ee3\u7801\uff0c\u548c\u4e4b\u524d\u7684\u57fa\u672c\u6ca1\u533a\u522b\uff0c\u5c31\u662f\u540d\u5b57\u4e0d\u4e00\u6837\uff1a<\/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 ParaMapServer {\r\n    public static final int SERVER_PORT = 8090;\r\n\r\n    public void startServer() {\r\n        try {\r\n            System.out.println(\"ParaMap TSimpleServer start ....\");\r\n\r\n            TProcessor tprocessor = new ParaMapService.Processor&lt;ParaMapService.Iface&gt;(new ParaMapImpl());\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        ParaMapServer server = new ParaMapServer();\r\n        server.startServer();\r\n    }\r\n\r\n}<\/pre>\n<p>\u7136\u540e\u662fClient\u8c03\u7528\u7aef\u4ee3\u7801\uff1a<\/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\nimport java.util.HashMap;\r\nimport java.util.Map;\r\n\r\npublic class ParaMapClient {\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    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            ParaMapService.Client client = new ParaMapService.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\r\n\r\n            Map&lt;String, value&gt; paramMap = new HashMap&lt;&gt;();\r\n            value v = new value();\r\n            v.setStrValue(\"username\"); \/\/ \u8fd9\u4e2a\u65b9\u6cd5\u662f\u81ea\u52a8\u751f\u6210\u7684\r\n            paramMap.put(\"mis\", v); \/\/ \u5f80map\u91ccput\u7684\u65f6\u5019value\u9700\u8981\u662fvalue\u7c7b\u578b\r\n            value v2 = new value();\r\n            v2.setIntValue(28);\r\n            paramMap.put(\"count\", v2);\r\n            value v3 = new value();\r\n            v3.setStrValue(\"28\");\r\n            paramMap.put(\"count_str\", v3);\r\n            value v4 = new value();\r\n            v4.setIntValue(200);\r\n            paramMap.put(\"status\", v4);\r\n            System.out.println(paramMap);\r\n            \/\/ client.ruleExecute1(paramMap);\r\n            \/\/ client.ruleExecute2(paramMap);\r\n            client.ruleExecuteX(paramMap);\r\n\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        ParaMapClient client = new ParaMapClient();\r\n        client.startClient(\"china\");\r\n    }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u6765\u770b\uff0c\u5bf9\u4e8e\u4e0d\u786e\u5b9a\u7c7b\u578b\u7684\u60c5\u51b5Thrift\u5904\u7406\u8d77\u6765\u4f1a\u6bd4\u8f83\u9ebb\u70e6\uff08\u5ba2\u6237\u7aef\u5f97set\uff0c\u670d\u52a1\u7aef\u4e5f\u5f97get\uff0c\u800c\u4e14\u5728get\u7684\u65f6\u5019\u8fd8\u8981\u8fdb\u884c\u5224\u65ad\uff09\uff0cThrift\u7684\u89c4\u8303\u6bd4\u8f83\u6b7b\u677f\uff08\u4e0d\u7075\u6d3b\uff09\uff0c\u4f46\u4e5f\u5c31\u662f\u8fd9\u79cd\u6b7b\u677f\u51cf\u5c11\u4e86\u51fa\u9519\u7684\u53ef\u80fd\u6027\uff0c\u6309\u9700\u9009\u62e9\u5427\u3002<\/span><\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/ju.outofmemory.cn\/entry\/127335\">thrift \u6587\u4ef6\u7f16\u5199\u6559\u7a0b<\/a><\/li>\n<li><a href=\"http:\/\/dongxicheng.org\/search-engine\/thrift-guide\/\">Thrift\u4f7f\u7528\u6307\u5357<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/qq_36372507\/article\/details\/79183513\">java\u5b66\u4e60\u603b\u7ed3\u2014\u2014Apache thrift\u7684.thrift\u6587\u4ef6\u7684\u7f16\u5199<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/chenny7\/p\/4224720.html\">Thrift \u7684\u539f\u7406\u548c\u4f7f\u7528<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u4e4b\u524d\u5728\u5b66\u4e60Thrift\u7684\u65f6\u5019\u6574\u7406\u4e86\u4e00\u7bc7\u300aApache Thrift\u5b66\u4e60\u5165\u95e8\u4e4b\u4ee3\u7801\u6f14\u793a\u300b [&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,416,1139],"class_list":["post-4026","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-java","tag-struct","tag-thrift"],"views":4773,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4026","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=4026"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4026\/revisions"}],"predecessor-version":[{"id":4027,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4026\/revisions\/4027"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}