1、添加依赖 jar
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
2、编写IDL文件 Hello.thrift
namespace java service.demo
service Hello {
string helloString(1:string para)
i32 helloInt(1:i32 para)
bool helloBoolean(1:bool para)
void helloVoid()
string helloNull()
}
3、生成代码
thrift -o <output directory> -gen java Hello.thrift
生成代码缩略图:
4、编写实现类、实现Hello.Iface:
缩略图:
5、编写服务端,发布(阻塞式IO + 多线程处理)服务。
- /**
- * 阻塞式、多线程处理
- *
- * @param args
- */
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public static void main(String[] args) {
- try {
- //设置传输通道,普通通道
- TServerTransport serverTransport = new TServerSocket(7911);
- //使用高密度二进制协议
- TProtocolFactory proFactory = new TCompactProtocol.Factory();
- //设置处理器HelloImpl
- TProcessor processor = new Hello.Processor(new HelloImpl());
- //创建服务器
- TServer server = new TThreadPoolServer(
- new Args(serverTransport)
- .protocolFactory(proFactory)
- .processor(processor)
- );
- System.out.println("Start server on port 7911...");
- server.serve();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
6、编写客户端,调用(阻塞式IO + 多线程处理)服务:
- public static void main(String[] args) throws Exception {
- // 设置传输通道 - 普通IO流通道
- TTransport transport = new TSocket("localhost", 7911);
- transport.open();
- //使用高密度二进制协议
- TProtocol protocol = new TCompactProtocol(transport);
- //创建Client
- Hello.Client client = new Hello.Client(protocol);
- long start = System.currentTimeMillis();
- for(int i=0; i<10000; i++){
- client.helloBoolean(false);
- client.helloInt(111);
- client.helloNull();
- client.helloString("dongjian");
- client.helloVoid();
- }
- System.out.println("耗时:" + (System.currentTimeMillis() - start));
- //关闭资源
- transport.close();
- }
现在已完成整个开发过程,超级无敌简单。
其中服务端使用的协议需要与客户端保持一致。
-------------------------------------------------------------------------------------------------------------------
上面展示了普通且常用的服务端和客户端,下面请看非阻塞IO,即java中的NIO:
基于非阻塞IO(NIO)的服务端:
- public static void main(String[] args) {
- try {
- //传输通道 - 非阻塞方式
- TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
- //异步IO,需要使用TFramedTransport,它将分块缓存读取。
- TTransportFactory transportFactory = new TFramedTransport.Factory();
- //使用高密度二进制协议
- TProtocolFactory proFactory = new TCompactProtocol.Factory();
- //设置处理器 HelloImpl
- TProcessor processor = new Hello.Processor(new HelloImpl());
- //创建服务器
- TServer server = new TThreadedSelectorServer(
- new Args(serverTransport)
- .protocolFactory(proFactory)
- .transportFactory(transportFactory)
- .processor(processor)
- );
- System.out.println("Start server on port 7911...");
- server.serve();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
调用非阻塞IO(NIO)服务的客户端:
- public static void main(String[] args) throws Exception {
- //设置传输通道,对于非阻塞服务,需要使用TFramedTransport,它将数据分块发送
- TTransport transport = new TFramedTransport(new TSocket("localhost", 7911));
- transport.open();
- //使用高密度二进制协议
- TProtocol protocol = new TCompactProtocol(transport);
- //创建Client
- Hello.Client client = new Hello.Client(protocol);
- long start = System.currentTimeMillis();
- for(int i=0; i<10000; i++){
- client.helloBoolean(false);
- client.helloInt(111);
- client.helloNull();
- client.helloString("360buy");
- client.helloVoid();
- }
- System.out.println("耗时:" + (System.currentTimeMillis() - start));
- //关闭资源
- transport.close();
- }
-----------------------------------------------------------------------------------------------------------------------------------
客户端异步调用:
- /** 调用[非阻塞IO]服务,异步 */
- public static void main(String[] args) {
- try {
- //异步调用管理器
- TAsyncClientManager clientManager = new TAsyncClientManager();
- //设置传输通道,调用非阻塞IO。
- final TNonblockingTransport transport = new TNonblockingSocket("localhost", 7911);
- //设置协议
- TProtocolFactory protocol = new TCompactProtocol.Factory();
- //创建Client
- final Hello.AsyncClient client = new Hello.AsyncClient(protocol, clientManager, transport);
- // 调用服务
- System.out.println("开始:" + System.currentTimeMillis());
- client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
- public void onError(Exception exception) {
- System.out.println("错误1: " + System.currentTimeMillis());
- }
- public void onComplete(helloBoolean_call response) {
- System.out.println("完成1: " + System.currentTimeMillis());
- try {
- client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
- public void onError(Exception exception) {
- System.out.println("错误2: " + System.currentTimeMillis());
- }
- public void onComplete(helloBoolean_call response) {
- System.out.println("完成2: " + System.currentTimeMillis());
- transport.close();
- }
- });
- } catch (TException e) {
- e.printStackTrace();
- }
- }
- });
- System.out.println("结束:" + System.currentTimeMillis());
- Thread.sleep(5000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-----------------------------------------------------------------------------------------------------------------------------------
使用SSL的服务端:
调用基于SSL服务端的客户端:
http://blog.csdn.net/m13321169565/article/details/7836006
相关推荐
thrift文件生成工具thrift-0.9.1.exe和thrift-0.9.2.exe压缩包
thrift,Apache Thrift - 可伸缩的跨语言服务开发框架
maven-thrift-plugin-0.1.11-sources.jar
Be a crackerjack at solving Apache Thrift-related issues. Table of Contents Chapter 1: Introducing Apache Thrift Chapter 2: Installing and Running Apache Thrift Chapter 3: Running Your First Apache ...
thrift开发时,将thrift文件自动生成java文件需要用到thrift-0.9.0.exe
thrift开发时,将thrift文件自动生成java文件需要用到thrift-0.9.0.exe
Apache Thrift is an open source cross language serialization and RPC framework. With support for over 15 programming languages, Apache Thrift can play an important role in a range of distributed ...
NULL 博文链接:https://onlyor.iteye.com/blog/1700777
maven-thrift-plugin-0.1.11.jar
Thrift是什么?什么情况下使用thrift Thrift 基础架构 Thrift脚本的数据类型,协议,传输层 Thrift运用实例
thrift-0.13.0在Windows上exe程序;thrift-0.13.0在Windows上exe程序;thrift-0.13.0在Windows上exe程序
thrift开发时,将thrift文件自动生成java文件需要用到thrift-0.9.3.exe
thrift-0.11.0.tar.gz
http://mikecvet.wordpress.com/2010/05/13/apache-thrift-tutorial-the-sequel/ http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/ 原教程使用的是c++ server和php client. 我用c++和c#分别实现了...
thrift-0.9.1.exe和thrift-0.9.2.exe 含使用说明
NULL 博文链接:https://kingxss.iteye.com/blog/1413696
thrift-0.9.0.tar.gz 类似google protocol buffer的东东的源代码
Thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器。thrift允许你定义一个简单的定义文件中的数据类型和服务接口。以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝...
thrift-0.13.0 已编译的Java版本 包含thrift和其依赖包 thrift-0.13.0 已编译的Java版本 包含thrift和其依赖包