1)安裝thrift:到thrift官網下載exe文件,而後將文件重命名爲thrift.exe,拷貝到c:\windows目錄下(或者任何目錄下),而後就能夠在dos環境下使用了php
c:\windows>thrift -gen java D:\mywork\javaProject\thriftTest\test.thrift ,輸出的java文件默認輸出到當前目錄下c:\windows,也可使用-o參數指定輸出路徑java
2)下載相關依賴包apache
2.1)libthrift.jar ,下載地址:http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.9.0/windows
2.2)slf4j-api.jarapi
2.3)slf4j-simple.jarmaven
3)編寫thrift 接口文件ide
- namespace cpp zam.thrift.test
- namespace py thriftTest
- namespace java com.zam.thrift.test
- namespace php thriftTest
-
- service Hello {
- string helloString(1:string word)
- }
4)編寫接口實現代碼ui
- 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;
- @Override
- public String helloString(String word) throws TException {
- // TODO Auto-generated method stub
- count += 1;
- System.out.println("get " + word + " " +count); return "hello " + word + " " + count;
- }
- }
5)編寫server代碼spa
- 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 代碼.net
- 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 init
- thrift server start
- thrift server open port 1234
8.2)啓動client端
- thrift client init
- thrift client start
- thrift client connext server at 1234 port
- hello panguso 1
- thrift client close connextion
- thrift client end
下載地址:http://download.csdn.net/download/liyonghui123/5742211