Apache thrift RPC 双向通信
在上一篇介绍Apache thrift 安装和使用,写了一个简单的demo,讲解thrift服务的发布和客户端调用,但只是单向的客户端发送消息,服务端接收消息。而客户端却得不到服务器的响应。
在不涉及语言平台的制约,WebService可胜任做这些服务端的处理。
基于大部分业务需求,更需要服务端能够响应处理数据。下面我通过一个demo案例,介绍下Apache thrift 双向通信的使用。
一.首先我们还是需要安装好Apache thrift。这里不再赘述,戳这里查看我上篇文章的介绍:http://www.cnblogs.com/sumingk/articles/6073105.html
二.其次准备好thrift 所需的jar包:

三.新建一个Java web项目,编写thrift脚本,命名为student.thrift 如下:
namespace java com.zhj.student typedef i32 int
typedef i16 short
typedef i64 long //Student Entity
struct Student {
: string name
} service Zthrift {
oneway void send(:Student msg)
}
四.执行student.thrift 文件,thrift --gen java student.thrift (该文件我还是放在c盘根目录下执行),随后生产gen-java文件,如下:

五.将新生成的两文件拷入项目中,其中Student.java 是实体类,Zthrift.java是生成的类。
六.编写thrift服务端类。
package com.zhj.server; import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException; import com.zhj.student.Student;
import com.zhj.student.Zthrift;
import com.zhj.student.Zthrift.Iface; public class ZServer { public static void main(String[] args){
try {
TServerSocket tServerSocket=new TServerSocket(9999);
TThreadPoolServer.Args targs=new TThreadPoolServer.Args(tServerSocket);
TBinaryProtocol.Factory factory=new TBinaryProtocol.Factory();
//获取processFactory
TProcessorFactory tProcessorFactory= getProcessorFactory();
targs.protocolFactory(factory);
targs.processorFactory(tProcessorFactory);
TThreadPoolServer tThreadPoolServer=new TThreadPoolServer(targs);
System.out.println("start server...");
tThreadPoolServer.serve(); } catch (TTransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } /**
* 内部类获取 getProcessorFactory
* @return
*/
public static int tt= 0;
public static TProcessorFactory getProcessorFactory(){ TProcessorFactory tProcessorFactory=new TProcessorFactory(null){
public TProcessor getProcessor(final TTransport tTransport){
Thread thread = new Thread(new Runnable() { @Override
public void run() {
try { System.out.println("服务端休眠5秒后,执行响应......");
//延时五秒回复(延迟执行给客户端发送消息)
Thread.sleep(5000);
tt +=100;
System.out.println("延时五秒回复时,tt = " +tt);
//这里可以把client提取作为成员变量来多次使用
Zthrift.Client client = new Zthrift.Client(new TBinaryProtocol(tTransport));
//给客户端响应消息
client.send(new Student("....test")); } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
thread.start(); return new Zthrift.Processor<Iface>(new Iface() { @Override
public void send(Student msg) throws TException {
// TODO Auto-generated method stub
tt+=10;
System.out.println("接收客户端消息时,tt = " +tt);
//接受客户端消息
System.out.println("....."+msg.toString());
}
}); }
};
return tProcessorFactory;
}
}
此处,内部类使用比较频繁,阅读会有些困难。Zthrift,Processor构造方法需要传入一个Iface 接口,该接口有一个接收客户端的方法send(), msg 是一个Student对象。
七.实现的客户端调用。如下:
package com.zhj.client; import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransportException; import com.zhj.student.Student;
import com.zhj.student.Zthrift.Iface;
import com.zhj.student.Zthrift; public class ZClient { public static void main(String[]args){
final TSocket tSocket=new TSocket("127.0.0.1",9999);
Zthrift.Client client=new Zthrift.Client(new TBinaryProtocol(tSocket));
try {
tSocket.open();
runMethod(tSocket);
//向服务端发送消息
client.send(new Student("小明1")); } catch (TTransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void runMethod(final TSocket tSocket){
Thread thread = new Thread(new Runnable() { @Override
public void run() {
Zthrift.Processor<Iface> mp = new Zthrift.Processor<Zthrift.Iface>(new Iface() { @Override
public void send(Student msg) throws TException {
// TODO Auto-generated method stub
Long start = System.currentTimeMillis();
try {
while(true){
//具体接收时间待定
if((System.currentTimeMillis()-start)>0.1*60*1000){
System.out.println("响应消息超时...");
break;
}
else {
System.out.println("收到服务端响应消息: "+msg);
}
//休眠两秒
Thread.sleep(2000L);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }); try {
while(mp.process(new TBinaryProtocol(tSocket), new TBinaryProtocol(tSocket))){
//阻塞式方法,不需要内容
System.out.println("走阻塞式方法");
//关闭tScoket
// tSocket.close();
}
} catch (TException e) {
System.out.println("连接已断开...");
e.printStackTrace();
}
}
});
thread.start();
}
}
在这里,我加入了一个超时响应的死循环,用于接收服务端返回的消息,控制台可以查看服务端给的响应消息。
八.运行服务端和客户端main方法,控制台打印如下:


代码阅读有些困难,有困难或不合理之处,请小伙伴指出。Thank you!
Apache thrift RPC 双向通信的更多相关文章
- golang高性能RPC:Apache Thrift安装使用完全攻略
在企业应用中RPC的使用可以说是十分的广泛,使用该技术可以方便的与各种程序交互而不用考虑其编写使用的语言. 如果你对RPC的概念还不太清楚,可以点击这里. 现今市面上已经有许多应用广泛的RPC框架,比 ...
- 【Java】分布式RPC通信框架Apache Thrift 使用总结
简介 Apache Thrift是Facebook开源的跨语言的RPC通信框架,目前已经捐献给Apache基金会管理,由于其跨语言特性和出色的性能,在很多互联网公司得到应用,有能力的公司甚至会基于th ...
- RPC框架实践之:Apache Thrift
一.概述 RPC(Remote Procedure Call)即 远程过程调用,说的这么抽象,其实简化理解就是一个节点如何请求另一节点所提供的服务.在文章 微服务调用链追踪中心搭建 一文中模拟出来的调 ...
- Apache thrift - 使用,内部实现及构建一个可扩展的RPC框架
本文首先介绍了什么是Apache Thrift,接着介绍了Thrift的安装部署及如何利用Thrift来实现一个简单的RPC应用,并简单的探究了一下Thrift的内部实现原理,最后给出一个基于Thri ...
- Apache Thrift 跨语言服务开发框架
Apache Thrift 是一种支持多种编程语言的远程服务调用框架,由 Facebook 于 2007 年开发,并于 2008 年进入 Apache 开源项目管理.Apache Thrift 通过 ...
- Apache Thrift 服务开发框架学习记录
Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架. 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Servic ...
- Apache Thrift学习之一(入门及Java实例演示)
目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...
- 使用Thrift RPC编写程序(服务端和客户端)
1. Thrift类介绍 Thrift代码包(位于thrift-0.6.1/lib/cpp/src)有以下几个目录: concurrency:并发和时钟管理方面的库processor:Processo ...
- Apache Thrift入门(安装、测试与java程序编写)
安装Apache Thrift ubuntu linux运行: #!/bin/bash #下载 wget http://mirrors.cnnic.cn/apache/thrift/0.9.1/thr ...
随机推荐
- wepack+sass+vue 入门教程(二)
六.新建webpack配置文件 webpack.config.js 文件整体框架内容如下,后续会详细说明每个配置项的配置 webpack.config.js直接放在项目demo目录下 module.e ...
- [翻译]开发文档:android Bitmap的高效使用
内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...
- 加深一下BlockingQueue的认识
认识BlockingQueue BlockingQueue是一种可以阻塞线程的队列,java中对这种队列提供了方法抽象,BlockingQueue则是抽象的接口. add:添加元素到队列里,添加成功返 ...
- SQLSERVER走起 APP隆重推出
SQLSERVER走起 APP隆重推出 为方便大家查看本微信公众以前推送的文章,QQ群里面的某位SQLSERVER重度爱好者开发了<SQLSERVER走起>的APP 以供大家一起交流 网页 ...
- 关于 devbridge-autocomplete 插件多选操作的实现方法
目前据我所知最好用的 autocomplete 插件就是 jquery-ui 的 autocomplete 以及 devbridge 的 autocomplete 插件. 我最终选择了 devbrid ...
- CSS 3学习——transform 2D转换
首先声明一点,transform属性不为none的元素是它的定位子元素(绝对定位和固定定位)的包含块,而且对内创建一个新的层叠上下文. 注意:可以通过 transform-box 属性指定元素的那个盒 ...
- .NET设计模式访问者模式
一.访问者模式的定义: 表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作. 二.访问者模式的结构和角色: 1.Visitor 抽象访问者角色,为该 ...
- MVC还是MVVM?或许VMVC更适合WinForm客户端
最近开始重构一个稍嫌古老的C/S项目,原先采用的技术栈是『WinForm』+『WCF』+『EF』.相对于现在铺天盖地的B/S架构来说,看上去似乎和Win95一样古老,很多新入行的,可能就没有见过经典的 ...
- css样式之border-image
border-image-source 属性设置边框的图片的路径[none | <image>] div { border: 20px solid #000; border-image-s ...
- Postman - 功能强大的 API 接口请求调试和管理工具
Postman 是一款功能强大的的 Chrome 应用,可以便捷的调试接口.前端开发人员在开发或者调试 Web 程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的 Fi ...