当我们使用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<AImpl> aProcessor = new AService.Processor<>(aService);
multiplexedProcessor.registerProcessor("aService", aProcessor);
BService.Processor<BImpl> 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<AImpl> 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的方式。