【问题标题】:Can one thrift server/transport implement multiple services?一个节俭服务器/传输可以实现多种服务吗?
【发布时间】:2023-06-11 22:14:01
【问题描述】:

所有教程和文档都建议每个 Thrift 服务器可以提供一个服务(1 个处理程序 1 个处理器 1 个服务器,所有这些都在构造函数中给出等)。

从我的观点(设计优雅)来看,如果许多或所有服务定义可以是独立的会更好。

  • 如果可以运行多个服务,代码的语法/api是什么?
  • 或者我需要使用一个更大、更丑的服务吗?

【问题讨论】:

    标签: thrift thrift-protocol


    【解决方案1】:

    所有教程和文档都建议 [...]

    不,事实并非如此。文档和教程的存在是为了解释一些东西,因此它们自然会专注于简单的案例,只是为了简单。

    但确实以前版本的 Thrift 并没有提供这样的功能。从 0.9.2 版本开始,我们在整个代码库中广泛实现了 Support for Multiplexing Services on any Transport, Protocol and Server

    用法很简单。细节取决于语言。例如,这是一个 C# 客户端:

    TTransport trans;
    trans = new TSocket("localhost", 9090);
    trans = new TFramedTransport(trans);
    trans.Open();
    
    TProtocol Protocol = new TBinaryProtocol(trans, true, true);
    
    TMultiplexedProtocol multiplex;
    
    multiplex = new TMultiplexedProtocol( Protocol, Constants.NAME_BENCHMARKSERVICE);
    BenchmarkService.Iface bench = new BenchmarkService.Client( multiplex);
    
    multiplex = new TMultiplexedProtocol( Protocol, Constants.NAME_AGGR);
    Aggr.Iface aggr = new Aggr.Client( multiplex);
    

    在这种情况下,我们有一个服务器通过同一个套接字提供两个服务,BenchmarkServiceAggr 服务。服务器部分以类似的方式设置。整个例子可以在in the code base under /lib/csharp/test/Multiplex找到。

    【讨论】:

    • 谢谢,我正在阅读。大概就是我想要的答案吧。 顺便说一句,如果您来自开发团队,我对节俭(运行时)速度感到非常惊讶