Windows安装、测试Thrift

Windows安装、测试Thrift
一、Thrift是跨平台传输工具,windows环境下、通过安装、测试来学习Thrift。
二、下载地址:http://download.csdn.net/detail/azhao_dn/5341602
重命名为 thrift.exe,然后放在 C:\Windows\下
三、配置:
设置环境变量:
 定义文件:
namespace java thrift  // defines the namespace    
     
typedef i32 int  //typedefs to get convenient names for your types  
  
service ThriftService {    
    int add(1:int a,2:int b),  
}  

   
执行命令:
  maven的pom.xml 配置
   
            org.apache.thrift  
            libthrift  
            0.9.0  
          
          
            org.slf4j  
            slf4j-api  
            1.7.5  
          
          
            org.slf4j  
            slf4j-simple  
            1.7.5  
          
  
          
            org.slf4j  
            slf4j-log4j12  
            1.7.5  
          
          
            log4j  
            log4j  
            1.2.17  
        

ThriftTestImpl
package thriftTest.thriftTest;

import org.apache.thrift.TException;

import thriftTest.thriftTest.ThriftService.Iface;

public class ThriftTestImpl implements Iface{

	public int add(int a, int b) throws TException {
		// TODO Auto-generated method stub
		System.out.println(a+" "+b);
		return a+b;
	}

}

ThriftServer:
package thriftTest.thriftTest;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

public class ThriftServer {
	public static void main(String[] args){
		try{
			TServerSocket tsst = new TServerSocket(7911);
			
			Factory proFactory  = new TBinaryProtocol.Factory();
			
			TProcessor processor = new ThriftService.Processor(  
                    new ThriftTestImpl()); 
			TServer.Args tArgs = new TServer.Args(tsst);  
            tArgs.processor(processor);  
            tArgs.protocolFactory(proFactory);  
  
            TServer server = new TSimpleServer(tArgs);  
            System.out.println("Start server on port 7911....");  
            server.serve();  
		}catch(TTransportException  e){
			e.printStackTrace();
		}
	}
}
ThriftClient 
package thriftTest.thriftTest;

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;

public class ThriftClient {
	public static void main(String[] args) {  
        try {  
            TTransport transport = new TSocket("localhost", 7911);  
            transport.open();  
            TProtocol protocol = new TBinaryProtocol(transport);  
            ThriftService.Client client = new ThriftService.Client(protocol);  
            System.out.println(client.add(77, 5));  
            transport.close();  
  
        } catch (TTransportException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (TException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
}


测试结果:
Server端

Client端

你可能感兴趣的:(工具)