标签:
上篇文章讲了单服务接口的实现,如果有多个接口怎么办?
还好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://my.oschina.net/qiangzigege/blog/517366