thrift - helloworld

模板

新建文件命名爲: helloThrift.thrift

namespace java com.ricisung.test.thrift
service HelloWordService  {
    string sayHello(1: i32 num, 2: string name);
}

生成代碼



D:\develporDir\thrift\testhome>
D:\develporDir\thrift\testhome>thrift -gen java helloThrift.thrift

D:\develporDir\thrift\testhome>

##沒有消息提示就是好消息

client

package com.ricisung.test.thrift;

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;

public class Client {

    public static void main(String[] args) throws TException {
        TTransport transport = new TSocket("localhost", 9090);
        transport.open();

        TProtocol protocol = new TBinaryProtocol(transport);

        HelloWordService.Client client = new HelloWordService.Client(protocol);
        long startTime = System.currentTimeMillis();

        for (int index = 0; index < 10000; index ++) {
            String response = client.sayHello(1111, "hello i am client");
            System.out.println(response);
        }

        long endTime = System.currentTimeMillis();

        System.out.println(endTime - startTime);

        transport.close();



    }

}

server

package com.ricisung.test.thrift;

import org.apache.thrift.server.TServer;  
import org.apache.thrift.server.TServer.Args;  
import org.apache.thrift.server.TSimpleServer;  
import org.apache.thrift.transport.TServerSocket;  
import org.apache.thrift.transport.TServerTransport;  
import org.apache.thrift.transport.TTransportException;

public class Thrift_HelloWorldServer {

    public static void main(String[] args) throws TTransportException {

        HelloWordService.Processor<HelloWordService.Iface> processor =  new HelloWordService.Processor
                <HelloWordService.Iface>(new HelloWorldServiceImpl());

        TServerTransport serverTransport = new TServerSocket(9090);  
        TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));  
        server.serve(); 

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章