Windows安装、测试Thrift

Windows安装、测试Thrift
一、Thrift是跨平台传输工具,windows环境下、通过安装、测试来学习Thrift。
重命名为 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 配置
<span style="color:#993300;"> </span><dependency>  
            <groupId>org.apache.thrift</groupId>  
            <artifactId>libthrift</artifactId>  
            <version>0.9.0</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-api</artifactId>  
            <version>1.7.5</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-simple</artifactId>  
            <version>1.7.5</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-log4j12</artifactId>  
            <version>1.7.5</version>  
        </dependency>  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.17</version>  
        </dependency>

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;
	}

}<span style="color:#993300;">
</span>

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<ThriftService.Iface>(  
                    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();  
        }  
    }  
}<span style="color:#993300;">
</span>


测试结果:
Server端

Client端

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值