{"id":2370,"date":"2012-12-19T11:37:42","date_gmt":"2012-12-19T03:37:42","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=2370"},"modified":"2012-12-19T12:57:46","modified_gmt":"2012-12-19T04:57:46","slug":"%e5%b0%81%e8%a3%85thrift%e7%9b%b8%e5%85%b3%e6%93%8d%e4%bd%9c%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/2370.html","title":{"rendered":"\u5c01\u88c5Thrift\u76f8\u5173\u64cd\u4f5c\u7c7b"},"content":{"rendered":"<p><span style=\"font-size: small;\">\u6700\u8fd1\u7814\u7a76Thrift\u4e86\u4e00\u6bb5\u65f6\u95f4\uff0c\u5bf9Thrift\u7684server\u7aef\u548cclient\u7aef\u64cd\u4f5c\u7684\u4ee3\u7801\u8fdb\u884c\u4e86\u7a0d\u5fae\u5c01\u88c5\uff0c\u8fd9\u6837\u7f16\u5199\u4e00\u4e2a\u540e\u53f0\u670d\u52a1\u5668\u7684\u65f6\u5019\uff0c\u5c31\u4e13\u5fc3\u5199\u4e1a\u52a1\u903b\u8f91\u5c31\u884c\u4e86\uff0c\u5bf9thrift\u5185\u90e8\u670d\u52a1\u5668\u4ee3\u7801\u4e0d\u7528\u5173\u5fc3\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u4ee3\u7801\u90fd\u662f\u901a\u7528\u7684\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5982\u679c\u4f60\u4e0d\u77e5\u9053thrift\u662f\u4ec0\u4e48\u4e1c\u4e1c\uff0c\u6211\u4e4b\u524d\u6709\u5199\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<a href=\"https:\/\/kyle.ai\/blog\/2045.html\">thrift\u5165\u95e8hello\u793a\u4f8b<\/a><\/span><\/p>\n<p><span style=\"font-size: small;\">\u6211\u540c\u65f6\u5199\u4e86\u51e0\u4e2a\u8bed\u8a00\u7248\u672c\u7684\u670d\u52a1\u5668\uff0c\u6709php\u3001python\u548cjava\uff0c\u5e76\u4f5c\u4e86\u76f8\u5e94\u6d4b\u8bd5\u3002client\u7aef\u7528\u7684php\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4f5c\u4e3a\u6d4b\u8bd5\uff0c\u6211\u4ee5thrift\u7684IDL\u683c\u5f0f\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1\u63a5\u53e3 mongotest.thrift\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nnamespace php mongotest\r\nnamespace py mongotest\r\n\r\nservice MongoTest {\r\n    string getServerStatus();\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u4e2a\u670d\u52a1\u529f\u80fd\u5c31\u662f\u67e5\u770bmongodb\u6570\u636e\u5e93\u7684\u8fde\u63a5\u72b6\u6001\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u6d4b\u8bd5\u8fd9\u79cd\u5f00\u53d1\u65b9\u5f0f\u8fde\u63a5\u6570\u636e\u5e93\u7684\u4e00\u4e9b\u884c\u4e3a\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0b\u9762\u5e16\u51fa\u5c01\u88c5\u7684server\u7c7b\uff0c\u5e76\u5199\u51fa\u751f\u6210\u4e00\u4e2a\u670d\u52a1\u7aef\u5b9e\u4f8b\u7684\u4ee3\u7801\u3002<\/span><\/p>\n<p><span style=\"font-size: small; color: #ff0000;\">java\u7248server\uff0cThriftServer.java<\/span><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.apache.thrift.server.TServer;\r\nimport org.apache.thrift.server.TServer.Args;\r\nimport org.apache.thrift.server.TSimpleServer;\r\nimport org.apache.thrift.server.TThreadedSelectorServer;\r\nimport org.apache.thrift.server.TThreadPoolServer;\r\nimport org.apache.thrift.transport.TSSLTransportFactory;\r\nimport org.apache.thrift.transport.TServerSocket;\r\nimport org.apache.thrift.transport.TNonblockingServerSocket;\r\nimport org.apache.thrift.transport.TServerTransport;\r\nimport org.apache.thrift.transport.TFramedTransport;\r\nimport org.apache.thrift.transport.TNonblockingServerTransport;\r\nimport org.apache.thrift.transport.TTransportFactory;\r\nimport org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;\r\nimport org.apache.thrift.protocol.TProtocolFactory;\r\nimport org.apache.thrift.protocol.TCompactProtocol;\r\n\r\npublic class ThriftServer {\r\n  public org.apache.thrift.TProcessor processor;\r\n\r\n  public ThriftServer(org.apache.thrift.TProcessor processor){\r\n    this.processor = processor;\r\n  }\r\n\r\n  public void startServer() {\r\n    try {\r\n        \/\/\u8fd9\u91cc\u53ef\u4ee5\u9009\u62e9TThreadPoolServer \u548c TThreadedSelectorServer\r\n      Runnable threadPool = new Runnable() {\r\n        public void run() {\r\n          threadPool(processor);\r\n        }\r\n      };\r\n      \/\/Runnable secure = new Runnable() {\r\n        \/\/public void run() {\r\n          \/\/secure(processor);\r\n        \/\/}\r\n      \/\/};\r\n\r\n      new Thread(threadPool).start();\r\n      \/\/new Thread(secure).start();\r\n    } catch (Exception x) {\r\n      x.printStackTrace();\r\n    }\r\n  }\r\n\r\n  public void threadPool(org.apache.thrift.TProcessor processor) {\r\n    try {\r\n      TServerTransport serverTransport = new TServerSocket(9090);\r\n      \/\/TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));\r\n\r\n      \/\/ Use this for a multithreaded server\r\n      TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport).processor(processor);\r\n      args.maxWorkerThreads = 50;\r\n      TServer server = new TThreadPoolServer(args);\r\n\r\n      System.out.println(&quot;Starting the server...&quot;);\r\n      server.serve();\r\n    } catch (Exception e) {\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n\r\n  public void threadSelector(org.apache.thrift.TProcessor processor) {\r\n    try {\r\n      TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(9090);\r\n      \/\/\u5f02\u6b65IO\uff0c\u9700\u8981\u4f7f\u7528TFramedTransport\uff0c\u5b83\u5c06\u5206\u5757\u7f13\u5b58\u8bfb\u53d6\u3002\r\n      TTransportFactory transportFactory = new TFramedTransport.Factory();\r\n      \/\/\u4f7f\u7528\u9ad8\u5bc6\u5ea6\u4e8c\u8fdb\u5236\u534f\u8bae\r\n      TProtocolFactory proFactory = new TCompactProtocol.Factory();\r\n      TServer server = new TThreadedSelectorServer(\r\n              new TThreadedSelectorServer.Args(serverTransport)\r\n              .protocolFactory(proFactory)\r\n              .transportFactory(transportFactory)\r\n              .processor(processor)\r\n              );\r\n\r\n      System.out.println(&quot;Starting the server...&quot;);\r\n      server.serve();\r\n    } catch (Exception e) {\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n\r\n  public void secure(org.apache.thrift.TProcessor processor) {\r\n    try {\r\n      \/*\r\n       * Use TSSLTransportParameters to setup the required SSL parameters. In this example\r\n       * we are setting the keystore and the keystore password. Other things like algorithms,\r\n       * cipher suites, client auth etc can be set.\r\n       *\/\r\n      TSSLTransportParameters params = new TSSLTransportParameters();\r\n      \/\/ The Keystore contains the private key\r\n      params.setKeyStore(&quot;.\/lib\/java\/test\/.keystore&quot;, &quot;thrift&quot;, null, null);\r\n\r\n      \/*\r\n       * Use any of the TSSLTransportFactory to get a server transport with the appropriate\r\n       * SSL configuration. You can use the default settings if properties are set in the command line.\r\n       * Ex: -Djavax.net.ssl.keyStore=.keystore and -Djavax.net.ssl.keyStorePassword=thrift\r\n       *\r\n       * Note: You need not explicitly call open(). The underlying server socket is bound on return\r\n       * from the factory class.\r\n       *\/\r\n      TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(9093, 0, null, params);\r\n      \/\/TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));\r\n\r\n      \/\/ Use this for a multi threaded server\r\n      TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport).processor(processor);\r\n      args.maxWorkerThreads = 50;\r\n       TServer server = new TThreadPoolServer(args);\r\n\r\n      System.out.println(&quot;Starting the secure server...&quot;);\r\n      server.serve();\r\n    } catch (Exception e) {\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6709\u4e86\u4e0a\u9762\u5c01\u88c5\u7684\u7c7b\u540e\uff0c\u8981\u5199\u4e00\u4e2a\u57fa\u4e8ethrift\u76f8\u5e94\u670d\u52a1\u7684socket\u540e\u53f0\uff0c\u5c31\u76f8\u5f53\u7b80\u5355\u4e86\uff0c\u4e0b\u9762\u662f\u670d\u52a1\u5668\u8c03\u7528\u7684\u4f8b\u5b50\u3000JavaServer.java\uff1a<\/span><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport mongotest.*;\r\n\r\npublic class JavaServer {\r\n\r\n    public static void main(String&#x5B;] args){\r\n        MongoTestHandler handler = new MongoTestHandler();  \/\/\u4e4b\u524d\u5b9a\u4e49\u7684\u63a5\u53e3mongotest.thrift\uff0c\u5177\u4f53\u5b9e\u73b0\u7c7b\r\n        MongoTest.Processor processor = new MongoTest.Processor(handler); \/\/\u8fd9\u4e2a\u8c03\u7528\u7684\u662f\u901a\u8fc7thrift\u81ea\u52a8\u751f\u6210\u7684\u7c7b\r\n        ThriftServer server = new ThriftServer(processor);\r\n        server.startServer();  \/\/\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u628a\u670d\u52a1\u5668\u7684\u4e00\u4e9b\u53c2\u6570\uff0c\u5982\u7aef\u53e3\u7b49\u5c01\u88c5\u8fdb\u53bb\r\n\r\n    }\r\n\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small; color: #ff0000;\">python\u7248server\uff0cThriftServer.py\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python\r\n#encoding=utf-8\r\n\r\nimport sys\r\nsys.path.append('.\/gen-py')\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\nclass ThriftServer:\r\n\r\n    def __init__(self, processor, port=9090):\r\n        self.processor = processor\r\n        self.port = port\r\n\r\n    def startServer(self):\r\n        processor = self.processor\r\n        transport = TSocket.TServerSocket(port=self.port)\r\n        tfactory = TTransport.TBufferedTransportFactory()\r\n        pfactory = TBinaryProtocol.TBinaryProtocolFactory()\r\n\r\n        #server = 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        server.daemon = True #enable ctrl+c to exit the server\r\n        server.setNumThreads(100);\r\n        #server = TServer.TForkingServer(processor, transport, tfactory, pfactory)\r\n\r\n        print 'Starting the server...'\r\n        server.serve()\r\n        print 'done.'\r\n\r\n<\/pre>\n<p><span style=\"font-size: small;\">python\u7248\u8c03\u7528\u793a\u4f8b\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python\r\n#encoding=utf-8\r\n\r\nimport sys\r\nsys.path.append('.\/gen-py')\r\nfrom ThriftServer import ThriftServer\r\nfrom MongoTestHandler import MongoTestHandler\r\nfrom mongotest import MongoTest\r\n\r\nhandler = MongoTestHandler()\r\nprocessor = MongoTest.Processor(handler)\r\nserver = ThriftServer(processor, 9090)\r\n\r\nserver.startServer()\r\n<\/pre>\n<p><span style=\"font-size: small; color: #ff0000;\">php\u7248server\uff0cThriftServer.php\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\nerror_reporting(E_ALL);\r\n\r\nrequire_once __DIR__.'\/lib\/Thrift\/ClassLoader\/ThriftClassLoader.php';\r\n\r\nuse Thrift\\ClassLoader\\ThriftClassLoader;\r\n\r\n$GEN_DIR = realpath(dirname(__FILE__).'').'\/gen-php';\r\n\r\n$loader = new ThriftClassLoader();\r\n$loader-&gt;registerNamespace('Thrift', __DIR__ . '\/lib');\r\n$loader-&gt;register();\r\nuse Thrift\\Protocol\\TBinaryProtocol;\r\nuse Thrift\\Transport\\TPhpStream;\r\nuse Thrift\\Transport\\TBufferedTransport;\r\nuse Thrift\\Server\\TServerSocket;\r\nuse Thrift\\Server\\TForkingServer;\r\nuse Thrift\\Factory\\TTransportFactory;\r\nuse Thrift\\Factory\\TBinaryProtocolFactory;\r\n\r\nclass ThriftServer {\r\n\r\n    private $processor;\r\n    private $server_ip;\r\n    private $server_port;\r\n\r\n    function __construct($processor, $server_ip=&quot;localhost&quot;, $server_port=9090){\r\n        $this-&gt;processor = $processor;\r\n        $this-&gt;server_ip = $server_ip;\r\n        $this-&gt;server_port = $server_port;\r\n    }\r\n\r\n    function startServer(){\r\n        $processor = $this-&gt;processor;\r\n        try {\r\n            $transport = new TServerSocket($this-&gt;server_ip, $this-&gt;server_port);\r\n        } catch (Exception $e) {\r\n            echo 'port already in use.';\r\n            exit();\r\n        }\r\n\r\n        $outputTransportFactory = $inputTransportFactory = new TTransportFactory($transport);\r\n        $outputProtocolFactory = $inputProtocolFactory = new TBinaryProtocolFactory();\r\n\r\n        $server = new TForkingServer(\r\n            $processor,\r\n            $transport,\r\n            $inputTransportFactory,\r\n            $outputTransportFactory,\r\n            $inputProtocolFactory,\r\n            $outputProtocolFactory\r\n        );\r\n\r\n        header('Content-Type: application\/x-thrift');\r\n        print 'Starting the server...';\r\n        $server-&gt;serve();\r\n    }\r\n\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">php\u8c03\u7528\u793a\u4f8b\uff0cPhpServer.php\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\ninclude_once __DIR__.'\/gen-php\/mongotest\/MongoTest.php';\r\ninclude_once __DIR__.'\/MongoTestHandler.php';\r\ninclude_once __DIR__.'\/ThriftServer.php';\r\nerror_reporting(E_ALL);\r\n\r\n$handler = new MongoTestHandler();\r\n$processor = new \\mongotest\\MongoTestProcessor($handler);\r\n$server = new ThriftServer($processor);\r\n$server-&gt;startServer();\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5bf9\u4e8e\u5ba2\u6237\u7aef\uff0c\u53ea\u505a\u4e86php\u4e00\u4e2a\u7248\u672c\uff0c\u5176\u5b83\u8bed\u8a00\u4e5f\u5f88\u5bb9\u6613\uff0c\u4e0b\u9762\u662fphp\u7248client\u5c01\u88c5\u7c7b,ThriftClient.php\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\nerror_reporting(E_ALL);\r\n\r\nrequire_once __DIR__.'\/lib\/Thrift\/ClassLoader\/ThriftClassLoader.php';\r\n\r\nuse Thrift\\ClassLoader\\ThriftClassLoader;\r\n\r\n$GEN_DIR = realpath(dirname(__FILE__)).'\/gen-php';\r\n\r\n$loader = new ThriftClassLoader();\r\n$loader-&gt;registerNamespace('Thrift', __DIR__ . '\/lib');\r\n$loader-&gt;register();\r\n\r\nuse Thrift\\Protocol\\TBinaryProtocol;\r\nuse Thrift\\Transport\\TSocket;\r\nuse Thrift\\Transport\\THttpClient;\r\nuse Thrift\\Transport\\TBufferedTransport;\r\nuse Thrift\\Exception\\TException;\r\n\r\nclass ThriftClient {\r\n\r\n    public $client;\r\n    private $transport;\r\n    private $protocol;\r\n    private $client_class;\r\n    private $server_ip;\r\n    private $server_port;\r\n    private $socket;\r\n\r\n    function __construct($client_class, $server_ip='localhost', $server_port=9090){\r\n        $this-&gt;client_class = $client_class;\r\n        $this-&gt;server_ip = $server_ip;\r\n        $this-&gt;server_port = $server_port;\r\n    }\r\n\r\n    function __destruct(){\r\n        $this-&gt;transport-&gt;close();\r\n    }\r\n\r\n    public function getClient(){\r\n        $this-&gt;socket = new TSocket($this-&gt;server_ip, $this-&gt;server_port);\r\n        $this-&gt;transport = new TBufferedTransport($this-&gt;socket, 1024, 1024);\r\n        $this-&gt;protocol = new TBinaryProtocol($this-&gt;transport);\r\n        $this-&gt;client = new \\mongotest\\MongoTestClient($this-&gt;protocol);\r\n        $this-&gt;transport-&gt;open();\r\n        return $this-&gt;client;\r\n    }\r\n}\r\n\r\n?&gt;\r\n<\/pre>\n<p>php\u7248client\u8c03\u7528\u793a\u4f8b\uff0cMongoTest-client-thrift.php\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\ninclude_once __DIR__.'\/gen-php\/mongotest\/MongoTest.php';\r\ninclude_once __DIR__.'\/ThriftClient.php';\r\n\r\n$thrift = new ThriftClient('\\mongotest\\MongoTestClient', 'localhost', 9090);\r\n$client = $thrift-&gt;getClient();\r\n$ret = $client-&gt;getServerStatus();   \/\/\u8c03\u7528\u670d\u52a1\u65b9\u6cd5\r\necho $ret;\r\n\r\n?&gt;\r\n<\/pre>\n<p><span style=\"font-size: small;\"><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2012\/12\/thrift-php.zip\">\u76f8\u5173\u4ee3\u7801\u6253\u5305<\/a>\uff08\u7701\u7565\u4e86thrift\u7684\u5e93\u548c\u901a\u8fc7thrift\u547d\u4ee4\u81ea\u52a8\u751f\u6210\u7684\u4ee3\u7801\uff0c\u53ca\u4e00\u4e9bjar\u4f9d\u8d56\u5305\uff0c\u5982mongodb\u5305\uff09<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u7814\u7a76Thrift\u4e86\u4e00\u6bb5\u65f6\u95f4\uff0c\u5bf9Thrift\u7684server\u7aef\u548cclient\u7aef\u64cd\u4f5c\u7684\u4ee3\u7801\u8fdb\u884c\u4e86\u7a0d\u5fae\u5c01\u88c5\uff0c\u8fd9\u6837\u7f16 [&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-2370","post","type-post","status-publish","format-standard","hentry","category-diary"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2370","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=2370"}],"version-history":[{"count":6,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2370\/revisions"}],"predecessor-version":[{"id":2373,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2370\/revisions\/2373"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=2370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=2370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=2370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}