Thrift-0.9.3的多服务接口实现
2015-10-15 00:00
363 查看
上篇文章讲了单服务接口的实现,如果有多个接口怎么办?
还好thrift的后续版本提供了,下面就来说下怎么实现,
这里参考了文章: http://blog.csdn.net/hivon/article/details/11681977
服务端
package service.server;
import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TTransportException;
import service.demo.Hello;
import service.demo.HelloServiceImpl;
public class MultiHelloServer {
public static void main(String[] args) {
try {
// TServerSocket serverTransport = new TServerSocket(7911);
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
TFramedTransport.Factory transportFactory = new TFramedTransport.Factory();
// TNonblockingTransport.
// 设置协议工厂为 TBinaryProtocol.Factory
TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();
// 关联处理器与 Hello 服务的实现
// TProcessor processor = new Hello.Processor(new
// HelloServiceImpl());
TMultiplexedProcessor processor1 = new TMultiplexedProcessor();
processor1.registerProcessor("Service1", new Hello.Processor(new HelloServiceImpl()));
// newTopicService.Processor<TopicService.Iface>(new TopicImpl()));
TThreadedSelectorServer.Args tArgs = new TThreadedSelectorServer.Args(serverTransport);
tArgs.transportFactory(transportFactory);
tArgs.protocolFactory(protocolFactory);
tArgs.processor(processor1);
// TServer server = new TThreadPoolServer(tArgs);
TServer server = new TThreadedSelectorServer(tArgs);
System.out.println("Start server on port 7911...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
客户端
package service.client;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import service.demo.Hello;
public class MultiHelloClient {
public static void main(String[] args) throws Exception {
// 设置传输通道,对于非阻塞服务,需要使用TFramedTransport,它将数据分块发送
TTransport transport = new TFramedTransport(new TSocket("127.0.0.1", 7911));
transport.open();
// 使用二进制协议
TProtocol protocol = new TBinaryProtocol(transport);
// 创建Client
TMultiplexedProtocol tmp = new TMultiplexedProtocol(protocol,"Service1");
Hello.Client client = new Hello.Client(tmp);
long start = System.currentTimeMillis();
for (int i = 0; i < 1; i++) {
System.out.println("client.helloBoolean(false)---"+client.helloBoolean(false));
//System.out.println("client.helloInt(111)---"+client.helloInt(111));
//client.helloNull();
System.out.println("client.helloString(\"360buy\")---"+client.helloString("360buy"));
client.helloVoid();
}
System.out.println("耗时:" + (System.currentTimeMillis() - start));
// 关闭资源
transport.close();
}
}
演示结果
还好thrift的后续版本提供了,下面就来说下怎么实现,
这里参考了文章: http://blog.csdn.net/hivon/article/details/11681977
服务端
package service.server;
import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TTransportException;
import service.demo.Hello;
import service.demo.HelloServiceImpl;
public class MultiHelloServer {
public static void main(String[] args) {
try {
// TServerSocket serverTransport = new TServerSocket(7911);
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
TFramedTransport.Factory transportFactory = new TFramedTransport.Factory();
// TNonblockingTransport.
// 设置协议工厂为 TBinaryProtocol.Factory
TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();
// 关联处理器与 Hello 服务的实现
// TProcessor processor = new Hello.Processor(new
// HelloServiceImpl());
TMultiplexedProcessor processor1 = new TMultiplexedProcessor();
processor1.registerProcessor("Service1", new Hello.Processor(new HelloServiceImpl()));
// newTopicService.Processor<TopicService.Iface>(new TopicImpl()));
TThreadedSelectorServer.Args tArgs = new TThreadedSelectorServer.Args(serverTransport);
tArgs.transportFactory(transportFactory);
tArgs.protocolFactory(protocolFactory);
tArgs.processor(processor1);
// TServer server = new TThreadPoolServer(tArgs);
TServer server = new TThreadedSelectorServer(tArgs);
System.out.println("Start server on port 7911...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
客户端
package service.client;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import service.demo.Hello;
public class MultiHelloClient {
public static void main(String[] args) throws Exception {
// 设置传输通道,对于非阻塞服务,需要使用TFramedTransport,它将数据分块发送
TTransport transport = new TFramedTransport(new TSocket("127.0.0.1", 7911));
transport.open();
// 使用二进制协议
TProtocol protocol = new TBinaryProtocol(transport);
// 创建Client
TMultiplexedProtocol tmp = new TMultiplexedProtocol(protocol,"Service1");
Hello.Client client = new Hello.Client(tmp);
long start = System.currentTimeMillis();
for (int i = 0; i < 1; i++) {
System.out.println("client.helloBoolean(false)---"+client.helloBoolean(false));
//System.out.println("client.helloInt(111)---"+client.helloInt(111));
//client.helloNull();
System.out.println("client.helloString(\"360buy\")---"+client.helloString("360buy"));
client.helloVoid();
}
System.out.println("耗时:" + (System.currentTimeMillis() - start));
// 关闭资源
transport.close();
}
}
演示结果
![](http://static.oschina.net/uploads/space/2015/1015/092809_0VDi_1382024.png)
相关文章推荐
- 基于HBase Thrift接口的一些使用问题及相关注意事项的详解
- sparksql与hive整合
- 用thrift实现多语言相互调用
- Thrift的required和optional源码分析
- Python 通过thrift接口连接Hbase读取存储数据
- Apache Thrift原理及windows使用
- 运行apache thrift的错误解决
- PHP调用python
- ajax应用
- Thrift学习
- Thrift 实例 Helloworld
- Thirft框架介绍
- Ubuntu安装Thrift连接Hive
- Java版的各种Thrift server实现的比较
- ubuntu12.0 安装thrift 0.9 时 configure: error: "Error: libcrypto required."
- thrift non-blocking server在多线程下的处理流程
- thrift文件里头使用异常
- Intel Hadoop Distribution 2.5.1 HBase Thrift启动异常
- 30天了解30种技术系列---(9)可伸缩的跨语言服务开发框架Thrift
- thrift连接池问题