thrift的多路服务复用实现

作为构建业务服务的发布者,一般情况下我们发布thrift的服务,会是这样

public class Server {
 
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private void start(int port) {
        try {

            TServerSocket serverTransport = new TServerSocket(port);
 
            ArithmeticService.Processor processor = new ArithmeticService.Processor(new ArithmeticServiceImpl());
 
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
            server.serve();

        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        new Server().start(8082);
    }
 
}

其中,ArithmeticService是我们在接口中定义的业务A接口,在SERVER这边进行发布。不过,当我们又有一个业务B,想跟这个A在一起部署。这样,就得用上TMultiplexedProcessor,相当于可以在一个端口下面发布多个业务服务。具体代码如下

public class Main {
    public static void main(String[] args) throws Exception {

        ApplicationContext app =  new GenericXmlApplicationContext("classpath:/applicationContext-server.xml");
        Main.start(app);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    private static void start(ApplicationContext app) {
        Properties pro = (Properties) app.getBean("config");
        int thriftPort = NumberUtils.toInt(pro.getProperty("thrift.port"));
        try {

            TServerSocket serverTransport = new TServerSocket(thriftPort);
            //sms
            SmsService.Iface smsIface = app.getBean( SmsService.Iface.class);
            SmsService.Processor smsProcessor = new SmsService.Processor(smsIface);
            //email
            EmailService.Iface emailIface = app.getBean( EmailService.Iface.class);
            EmailService.Processor emailProcessor = new EmailService.Processor(emailIface);

            //注册多个TMultiplexedProcessor
            TMultiplexedProcessor tMultiplexedProcessor = new TMultiplexedProcessor();
            tMultiplexedProcessor.registerProcessor(SmsService.class.getName()+"$Client",smsProcessor);
            tMultiplexedProcessor.registerProcessor(EmailService.class.getName()+"$Client", emailProcessor);
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(tMultiplexedProcessor));

            server.serve();

        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }
}

注意看TMultiplexedProcessor ,利用它可以注册多个业务服务。上面这段代码块,主要注册了短信发送服务,邮件发送服务。服务启动成功后,可以利用我之前改写的thrift-pool-client
调用示例如下

public class ThriftMain {
     public static void main(String[] args) throws TException {
            // customize pool config
            GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
            // ... customize pool config here
            // customize transport, while if you expect pooling the connection, you should use TFrameTransport.
            Function<ThriftServerInfo, TTransport> transportProvider = new Function<ThriftServerInfo, TTransport>() {

                public TTransport apply(ThriftServerInfo info) {
                    // TODO Auto-generated method stub
                    TSocket socket = new TSocket(info.getHost(), info.getPort());
                    return socket;
                }
                
            };

            Function<List<ThriftServerInfo>, List<ThriftServerInfo>> addElementFunction =
                    new Function<List<ThriftServerInfo>, List<ThriftServerInfo>>() {

                      public List<ThriftServerInfo> apply(List<ThriftServerInfo> list) {
                          return Arrays.asList(//
                                    ThriftServerInfo.of("localhost", 8082)//
                                    // or you can return a dynamic result.
                                    );
                      }
                    };
                    
            Supplier<List<ThriftServerInfo>> list  = Suppliers.compose(addElementFunction, new Supplier<List<ThriftServerInfo>>(){

                public List<ThriftServerInfo> get() {
                    // TODO Auto-generated method stub
                    return Arrays.asList(//
                            ThriftServerInfo.of("localhost", 8082)//
                            // or you can return a dynamic result.
                            );
                }});
          

         ThriftClient thriftClient = new ThriftClientImpl( list,new DefaultThriftConnectionPoolImpl(poolConfig, transportProvider));
         thriftClient.iface(SmsService.Client.class).sendSms("*******","尊敬的用户:欢迎使用****。您的登录名是:" + "**********" + ",密码为02145,如非本人操作,请不要理会.");
     }
}
本篇没有涉及到thrift的接口定义等相关技术点,再启篇幅吧。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,964评论 19 139
  • 转自:http://blog.csdn.net/kesonyk/article/details/50924489 ...
    晴天哥_王志阅读 25,243评论 2 38
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,034评论 25 709
  • 也许是过于活在梦幻中,仿佛现实才出现在梦里。 梦里稀奇古怪,却又那么真实,有时分不清,是梦给了我生活,还是现实给了...
    京之村猫阅读 1,792评论 0 0
  • 2016年经历了俩大事,升学,搬迁。磕磕绊绊总算圆满。这个年,头一次在外过。2017,依旧有两件大事,无论什么过程...
    艳儿吖阅读 2,144评论 0 0

友情链接更多精彩内容