java Thrift TThreadPoolServer 多个processor 的实现
当我们使用Thrift 通信的时候,服务端有时候需要注册多个类,去实现通信,这时候我们就不能再使用单一Processor的方式,就要使用多个Processor,那么如何去实现呢?
(图片来源网络,侵删)
多个Process
服务端
public static void main(String[] args) {
try {
AImpl aService = new AImpl();
BImpl bService=new BImpl();
TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor();
AService.Processor aProcessor = new AService.Processor(aService);
multiplexedProcessor.registerProcessor("aService", aProcessor);
BService.Processor bProcessor = new BService.Processor(bService);
multiplexedProcessor.registerProcessor("bService", bProcessor);
TServerSocket serverTransport = new TServerSocket(80000);
TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);
serverArgs.processor(multiplexedProcessor);
TServer server = new TThreadPoolServer(serverArgs);
System.out.println("Starting the multi-processor server...");
server.serve();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
客户端
public static void main(String[] args) throws TException {
TTransport transport = new TSocket("localhost", 80000);
transport.open();
// AService
TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(new TBinaryProtocol(transport), "aService");
AService.Client aClient = new AService.Client(multiplexedProtocol);
aClient.method();
System.out.println("Calling AService method...");
// BService
multiplexedProtocol = new TMultiplexedProtocol(new TBinaryProtocol(transport), "bService");
BService.Client bClient = new BService.Client(multiplexedProtocol);
BClient.method();
System.out.println("Calling SystemLogService method...");
transport.close();
}
这个Demo中,我们要用到两个接口类,那么,A和B,使用TMultiplexedProcessor 去注册两个Service,启动服务。
单个Process
服务端
AImpl aService = new AImpl();
TServerSocket serverSocket = new TServerSocket(90000);
AService.Processor aProcessor
= new AService.Processor(aService);
TThreadPoolServer.Args serverArg = new TThreadPoolServer.Args(serverSocket);
serverArg.processor(aProcessor);
TThreadPoolServer server = new TThreadPoolServer(serverArg);
server.serve();
客户端
TTransport transport = new TSocket("localhost", 90000);
transport.open();
TBinaryProtocol protocol = new TBinaryProtocol(transport);
AService.Client aClient = new AService.Client(protocol);
aclient.method();
附单个process的方式。
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
