windows配置thrift開發環境

來源:互聯網
上載者:User

     1)安裝thrift:到thrift官網下載exe檔案,然後將檔案重新命名為thrift.exe,拷貝到c:\windows目錄下,然後就可以在dos環境下使用了

           如:thrift -gen java D:\mywork\javaProject\thriftTest\test.thrift ,輸出的java檔案預設輸出到目前的目錄下,也可以使用-o參數指定輸出路徑

     2)下載相關依賴包

          2.1)libthrift.jar ,:http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.9.0/

          2.2)slf4j-api.jar

          2.3)slf4j-simple.jar

     3)編寫thrift 介面檔案

namespace cpp zam.thrift.testnamespace py thriftTestnamespace java com.zam.thrift.testnamespace php thriftTestservice Hello {      string helloString(1:string word)  }

    4)編寫介面實現代碼

package com.zam.server;import org.apache.thrift.TException;import com.zam.thrift.test.Hello.Iface;public class HelloImpl implements Iface{private static int count = 0;@Overridepublic String helloString(String word) throws TException {// TODO Auto-generated method stubcount += 1;System.out.println("get " + word + " " +count); return "hello " + word + " " + count;}}

    5)編寫server代碼

package com.zam.server;import org.apache.thrift.protocol.TBinaryProtocol;  import org.apache.thrift.protocol.TBinaryProtocol.Factory;  import org.apache.thrift.server.TServer;  import org.apache.thrift.server.TThreadPoolServer;  import org.apache.thrift.server.TThreadPoolServer.Args;  import org.apache.thrift.transport.TServerSocket;  import org.apache.thrift.transport.TTransportException; import com.zam.thrift.test.Hello;import com.zam.thrift.test.Hello.Processor;public class Server {    public void startServer() {          try {              System.out.println("thrift server open port 1234");            TServerSocket serverTransport = new TServerSocket(1234);              Hello.Processor process = new Processor(new HelloImpl());              Factory portFactory = new TBinaryProtocol.Factory(true, true);              Args args = new Args(serverTransport);              args.processor(process);              args.protocolFactory(portFactory);              TServer server = new TThreadPoolServer(args);              server.serve();          } catch (TTransportException e) {              e.printStackTrace();          }      }            public static void main(String[] args) {      System.out.println("thrift server init");        Server server = new Server();          System.out.println("thrift server start");        server.startServer();          System.out.println("thrift server end");    }  }

    6)編寫client 代碼

package com.zam.server;import org.apache.thrift.TException;  import org.apache.thrift.protocol.TBinaryProtocol;  import org.apache.thrift.protocol.TProtocol;  import org.apache.thrift.transport.TSocket;  import org.apache.thrift.transport.TTransport;  import org.apache.thrift.transport.TTransportException; import com.zam.thrift.test.Hello;public class Client {    public void startClient() {          TTransport transport;          try {              System.out.println("thrift client connext server at 1234 port ");            transport = new TSocket("localhost", 1234);              TProtocol protocol = new TBinaryProtocol(transport);              Hello.Client client = new Hello.Client(protocol);              transport.open();              System.out.println(client.helloString("panguso"));              transport.close();              System.out.println("thrift client close connextion");        } catch (TTransportException e) {              e.printStackTrace();          } catch (TException e) {              e.printStackTrace();          }      }        public static void main(String[] args) {      System.out.println("thrift client init ");        Client client = new Client();          System.out.println("thrift client start ");        client.startClient();          System.out.println("thrift client end ");    }  }

     8)運行server和client代碼

           8.1)啟動server端

thrift server initthrift server startthrift server open port 1234

         8.2)啟動client端

thrift client init thrift client start thrift client connext server at 1234 port hello panguso 1thrift client close connextionthrift client end 

       9)範例程式碼:http://download.csdn.net/detail/azhao_dn/5341602

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.