java Thrift TThreadPoolServer 多个processor 的实现

java Thrift TThreadPoolServer 多个processor 的实现

码农世界 2024-06-16 后端 94 次浏览 0个评论

当我们使用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的方式。

转载请注明来自码农世界,本文标题:《java Thrift TThreadPoolServer 多个processor 的实现》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,94人围观)参与讨论

还没有评论,来说两句吧...

Top