Thrift 使用

原创
2014/10/23 17:43
阅读数 1.2K
AI总结

      Thrift是一种可伸缩的跨语言服务的发展软件框架。它结合了功能强大的软件堆栈的代码生成引擎,以建设服务,工作效率和无缝地与C + +,C#,Java,Python和PHP和Ruby结合。
     Thrift通过接口定义语言 (interface definition language,IDL) 来定义数据类型和服务,Thrift接口定义文件由Thrift代码编译器生成thrift目标语言的代码(目前支持C++,Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk和OCaml),并由生成的代码负责RPC协议层和传输层的实现。 

下载地址:http://thrift.apache.org/   这里我们下载:Thrift compiler for Windows (thrift-0.9.1.exe)
maven dependency:

<dependency>
	<groupId>org.apache.thrift</groupId>
	<artifactId>libthrift</artifactId>
	<version>0.9.1</version>
</dependency>
接口定义语言:
namespace java thrift.clazz
struct  NewsModel{  
	1:i32 id ;  
	2:string title;  
	3:string content;  
	4:string media_from;  
	5:string author;  
}  
service IndexNewsOperatorServices {  
	bool indexNews(1:NewsModel indexNews),  
	bool deleteArtificiallyNews(1:i32 id )  
}
更多关于接口定义语言: http://www.cnblogs.com/tianhuilove/archive/2011/09/05/2167669.html

由接口定义语言生成相应语言的类
thrift --gen <language> <Thrift filename>
这里我们写一个简单的bat文件
thrift-0.9.1.exe --gen java idl/*.thrift
pause
将idl文件夹下面的接口定义文件生成java类,文件自动生成在  gen-java  文件夹下面。

下面我们用java了测试:
1.首先实现定义的IndexNewsOperatorServices的是实现类:
import org.apache.thrift.TException;

import thrift.clazz.IndexNewsOperatorServices;
import thrift.clazz.NewsModel;

public class IndexNewsOperatorServicesImpl implements
		IndexNewsOperatorServices.Iface {

	@Override
	public boolean indexNews(NewsModel indexNews) throws TException {
		System.out.println("method success !!  data  is :" + indexNews);
		return false;
	}

	@Override
	public boolean deleteArtificiallyNews(int id) throws TException {
		System.out.println("method success !!  id is :" + id);
		return false;
	}
}
2.实现服务器端:
import java.net.InetSocketAddress;

import org.apache.thrift.protocol.TBinaryProtocol;
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.TServerTransport;
import org.apache.thrift.transport.TTransportFactory;

import thrift.clazz.IndexNewsOperatorServices;

public class ThriftServerTest {
	public static void main(String[] args) {
		@SuppressWarnings({ "rawtypes", "unchecked" })
		IndexNewsOperatorServices.Processor processor = new IndexNewsOperatorServices.
                           Processor(new IndexNewsOperatorServicesImpl());
		try {
			TServerTransport serverTransport = new TServerSocket(
					new InetSocketAddress("127.0.0.1", 8081));
			Args trArgs = new Args(serverTransport);
			trArgs.processor(processor);

                         trArgs.protocolFactory(new TBinaryProtocol.Factory(true, true));//使用二进制来编码应用层的数据  
			trArgs.transportFactory(new TTransportFactory());//使用普通的socket来传输数据  

			TServer server = new TThreadPoolServer(trArgs);
			System.out.println("server启动监听客户端连接......");
			server.serve();
		} catch (Exception e) {
			throw new RuntimeException("index thrift server start failed!!"
					+ "/n" + e.getMessage());
		}
	}
}

3.实现客户端:

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 thrift.clazz.IndexNewsOperatorServices;
import thrift.clazz.NewsModel;

public class ThriftClientTest {

	public static void main(String[] args) throws TException {
		TTransport transport = new TSocket("127.0.0.1", 8081);
		TProtocol protocol = new TBinaryProtocol(transport);
		IndexNewsOperatorServices.Client client = new IndexNewsOperatorServices                .Client(protocol);
		transport.open();

		client.deleteArtificiallyNews(123456);
		NewsModel newsModel = new NewsModel();
		newsModel.setId(1);
		newsModel.setTitle("编程语言");
		newsModel.setContent("java,c,c++,c#,php,js......");
		newsModel.setAuthor("ksfzhaohui");
		newsModel.setMedia_from("火星");
		client.indexNews(newsModel);
		transport.close();
		System.out.println("客户端发送数据.....");
	}
}

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
8 收藏
0
分享
AI总结
返回顶部
顶部