Thrift实例

要学习Thrift,它的官网上有资料介绍以及实例,可以到官网:http://thrift.apache.org/上查看。

在官网下载thrift的资源包,安装编译生成相应语言下的jar包,具体在windows下或者ubuntu下的安装参见:

Thrift 学习笔记2——Windows环境下Thrift的安装、编译以及测试

Thrift 学习笔记1——Ubuntu环境下Thrift的安装、编译以及测试

我主要是利用java语言完成Thrift的实例。因为感觉安装起来比较麻烦,所以不想去安装thrift,我直接使用别人已经编译好的jar包,将这些jar包直接添加到我的Myeclipse工程下就可以编写Thrift的项目了。此外,thrift文件自动生成java代码需要使用thrift-0.9.0.exe,以上文件可以到这里去下载:

thrift-0.9.0.exe下载地址

Java语言Thrift工程需要的jar包下载地址

libthrift-0.9.0.jar

下面通过几个实例来说明Thrift的使用方法:

1、我们将下载的thrift-0.9.0.exe放置在目录D:\Thrift下

2、编写Hello.thrift文件

service Hello{
	string helloString(1:string para)
	i32 helloInt(1:i32 para)
	bool helloBoolean(1:bool para)
	void helloVoid()
	string helloNull()
}
这是使用IDL描述性语言编写的Thrift文件,包括了5个方法,每个方法包含一个方法名,参数列表和返回类型。每个参数包括参数序号,参数类型以及参数名。 Thrift 是对 IDL(Interface Definition Language) 描述性语言的一种具体实现。

3、自动生成java代码

将Hello.thrift文件和thrift-0.9.0.exe放置到相同目录下,即D:\Thrift,运行cmd,打开窗口命令行,定位到D:\Thrift

执行命令:

D:\Thrift>thrift-0.9.0.exe -gen java Hello.thrift
此时会在D:\Thrift下生成一个目录gen-java,里面有Hello.java

4、创建Java工程

打开Eclipse或者Myeclipse,创建一个Java工程:Hello,导入刚才生成的Hello.java文件,同时新建一个自由文件夹,Thrift工程需要的jar包以及libthrift-0.9.0.jar放置到文件夹下,同时在Java Build Path中添加引用。

5、编写接口

import org.apache.thrift.TException;
public class HelloServiceImpl implements Hello.Iface{
	public boolean helloBoolean(boolean para) throws TException{
		return para;
	}
	public int helloInt(int para) throws TException{
		try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return para;
	}
	
	public String helloNull() throws TException{
		return null;
	}
	
	public String helloString(String para) throws TException{
		return para;
	}
	
	public void helloVoid() throws TException{
		System.out.println("Hello World!");
	}

}
接口实现Thrift定义文件中的服务。

6、编写服务器端

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.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;

public class HelloServiceServer {
	/**
	 * 启动thrift服务器
	 * @param args
	 */
	public static void main(String[] args) {		
		try{
		//设置服务器端口为7911
		TServerSocket serverTransport = new TServerSocket(7911);
		//设置协议工厂为TBinaryProtocol.Factory
		Factory proFactory = new TBinaryProtocol.Factory();
		//关联处理器与Hello服务的实现
		TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
		TServer.Args tArgs = new TServer.Args(serverTransport);
		tArgs.processor(processor);
		tArgs.protocolFactory(proFactory);
		//使用TSimpleServer
		TServer server = new TSimpleServer(tArgs);
		System.out.println("Start server on port 7911....");
		server.serve();
		}catch(TTransportException e){
			e.printStackTrace();
		}		
		/*
		try{
			//设置服务器端口为7911
			TServerSocket serverTransport = new TServerSocket(7911);
			//设置协议工厂为TBinaryProtocol.Factory
			Factory proFactory = new TBinaryProtocol.Factory();
			//关联处理器与Hello服务的实现
			TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
			Args tArgs = new Args(serverTransport);
			tArgs.processor(processor);
			tArgs.protocolFactory(proFactory);
			TServer server = new TThreadPoolServer(tArgs);
			System.out.println("Start server on port 7911....");
			server.serve();
			TServerTransport s = new TServerSocket(11);			
			}catch(TTransportException e){
				e.printStackTrace();
			}
			*/
	}

}
7、编写客户端

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 HelloServiceClient {
	/**
	 * 调用Hello服务
	 * @param args
	 */
	public static void main(String[] args) {				
		try {
			//设置调用的服务器为本地,端口为7911
			TTransport transport = new TSocket("localhost", 7911);
			transport.open();
			//设置传输协议为TBinaryProtocol
			TProtocol protocol = new TBinaryProtocol(transport);
			Hello.Client client = new Hello.Client(protocol);
			client.helloVoid();
			transport.close();
			
		} catch (TTransportException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
8、运行

先运行服务器端,再运行客户端。

得到:

Start server on port 7911....
Hello World!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Thrift 是一个高效的跨语言 RPC 框架,它支持多种编程语言,包括 C++, Java, Python, Ruby, PHP 等。下面是一个简单的 Thrift 实例,展示了如何使用 Thrift 进行跨语言通信: 1. 编写 Thrift IDL 文件 定义一个 Thrift 接口,使用 Thrift IDL 语言编写一个服务接口文件,例如 Example.thrift: ``` namespace java com.example namespace py example service ExampleService { string ping(1: string message) } ``` 2. 生成代码 使用 Thrift 编译器将 Thrift IDL 文件编译成代码,例如: ``` thrift --gen java Example.thrift thrift --gen py Example.thrift ``` 将会生成 Java 和 Python 的代码文件,可以根据需要进行使用。 3. 实现服务端 使用生成的代码实现服务端,例如 Java 语言: ```java public class ExampleHandler implements ExampleService.Iface { @Override public String ping(String message) throws TException { return "Pong: " + message; } } public static void main(String[] args) throws Exception { TServerTransport serverTransport = new TServerSocket(9090); ExampleService.Processor<ExampleHandler> processor = new ExampleService.Processor<>(new ExampleHandler()); TServer server = new TSimpleServer(new TServer.Args(serverTransport).processor(processor)); System.out.println("Starting the server..."); server.serve(); } ``` 4. 实现客户端 使用生成的代码实现客户端,例如 Python 语言: ```python transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = ExampleService.Client(protocol) transport.open() response = client.ping("Hello, world!") print(response) transport.close() ``` 以上就是一个简单的 Thrift 实例,展示了如何使用 Thrift 进行跨语言通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值