Thrift 多接口服务支持:server 绑定多个 processor

原创 thrift

在实际项目需求中,往往会有大量的服务接口。而 Processor 很明显无法满足代码上的组织优化,如果将所有的“接口方法”写在一个 service 接口实现文件中,那么最终导致的问题就是代码文件庞大混乱,无法维护。

幸好,Thrift 提供了 TMultiplexedProcessorTMultiplexedProtocol 类来帮助开发者拆分和组装业务接口服务。

核心分析

多接口服务器代码与单一接口的服务器代码最大的不同之处是使用了 TMultiplexedProcessor 类,通过该类,可以注册多个接口的服务实现类:

TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor();
multiplexedProcessor.registerProcessor("TopicService", newTopicService.Processor<TopicService.Iface>(new TopicImpl()));
multiplexedProcessor.registerProcessor("UserService", new UserService.Processor<UserService.Iface>(new UserImpl()));

其他部分代码跟以前的服务器代码一样。

客户端代码的不同之处是引入了 TMultiplexedProtocol 类,它来帮助客户端区别调用哪个接口:

TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol,"TopicService");
TopicService.Client topicService = new TopicService.Client(multiplexedProtocol);

多接口服务实例

本实例在《精选实例:Thrift 实例教程(Java)》一文中的实例基础上更改,使原来单一接口服务能够支持多接口服务。方便大家对照参考。

服务器端 ThriftServerTest.java

package com.gemantic.analyse.thrift.index;

import java.net.InetSocketAddress;

import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportFactory;

/**
 * 服务端
 * 
 * @author liuqianfei
 *
 */
public class ThriftServerTest
{
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args)
    {
        // TMultiplexedProcessor
        TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor();
        // index processor,其他的processor
        IndexNewsOperatorServices.Processor indexProcessor
        = new IndexNewsOperatorServices.Processor(new IndexNewsOperatorServicesImpl());
        // other processor ...

        // 注册所有的processor到multiplexedProcessor中
        multiplexedProcessor.registerProcessor("indexService", indexProcessor);

        try
        {
            TServerTransport serverTransport = new TServerSocket(new InetSocketAddress("127.0.0.1", 9981));
            Args trArgs = new Args(serverTransport);
            //trArgs.processor(processor);
            trArgs.processor(multiplexedProcessor);
            // 使用二进制来编码应用层的数据
            trArgs.protocolFactory(new TBinaryProtocol.Factory(true, true));
            // 使用普通的socket来传输数据
            trArgs.transportFactory(new TTransportFactory());
            TServer server = new TThreadPoolServer(trArgs);
            System.out.println("server begin ......................");
            server.serve();
            System.out.println("---------------------------------------");
            server.stop();
        }
        catch (Exception e)
        {
            throw new RuntimeException("index thrift server start failed!!" + "/n" + e.getMessage());
        }
    }
}

客户端 ThriftClientTest.java

package com.gemantic.analyse.thrift.index;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
/**
 * 客户端
 * @author liuqianfei
 *
 */
public class ThriftClientTest
{
    public static void main(String[] args) throws TException
    {
        TTransport transport = new TSocket("127.0.0.1", 9981);
        long start = System.currentTimeMillis();
        TProtocol protocol = new TBinaryProtocol(transport);

        // TMultiplexedProtocol
        TMultiplexedProtocol multiplexedProtocol = new  TMultiplexedProtocol(protocol,"indexService");
        // 按名称获取服务端注册的service
        IndexNewsOperatorServices.Client indexService = new IndexNewsOperatorServices.Client(multiplexedProtocol);

        transport.open();

        // deleteArtificiallyNews
        indexService.deleteArtificiallyNews(123456);

        // indexNews
        NewsModel newsModel = new NewsModel();
        newsModel.setId(789456);
        newsModel.setTitle("好诗");
        newsModel.setContent("锄禾日当午,清明上河图");
        newsModel.setAuthor("ddc");
        newsModel.setMedia_from("新华08");
        String callback = indexService.indexNews(newsModel);
        System.out.println("==>" + callback);

        transport.close();

        System.out.println("耗时:" + (System.currentTimeMillis() - start));
        System.out.println("client sucess!");
    }
}

环境搭建和其他代码细节请参考《精选实例:Thrift 实例教程(Java)》

如果觉得这对你有用,请随意赞赏,给与作者支持
评论 0
最新评论