使用 Apache Thrift 实现具有 HTTP 协议的服务器/客户端

     2023-02-16     215

关键词:

【中文标题】使用 Apache Thrift 实现具有 HTTP 协议的服务器/客户端【英文标题】:Implementing a Server/Client with HTTP protocol with Apache Thrift 【发布时间】:2016-08-01 21:30:36 【问题描述】:

我是 apache thrift 的新手。我想用它来帮助我实现一个服务器,它接受来自客户端的输入,解析它并在此基础上将它引导到适当的函数,该函数反过来会返回对该特定请求的响应。

例如:

    客户端发送:/get_list/1(这应该从服务器返回列表#1 中的值) 服务器将输入解析为以下内容:get_list && 1 服务器将此映射到 get_list_req,这是一个只有一个参数的结构:i32_t id(在本例中为 1) 服务器将此发送到 getListReq 函数,该函数应返回具有 1 个参数的 get_list_resp 结构:list list_content。

现在我已经在 mylist.thrift 中定义了必要的结构:

struct TGetListRequest 
    1: i32_t id,

struct TGetListResponse 
    1: string response_code, //FAIL or SUCCESS
    2: list<string> nodes, 


service TmyListService 

    TGetListResponse getListReq( 1:TGetListRequest arg ),

现在这会生成必要的文件:

TmyListService_server.skeleton.cpp 
TmyListService.cpp
TmyListService.h
mylistconstants.cpp
mylistconstants.h
mylisttypes.cpp
mylisttypes.h

第一个文件的内容被复制到 TmyListService_server.cpp 中,该类使用其方法实现:getListReq,如下所示:

class TGetListRequestHandler: public TGetListRequestIf 
public:
    TGetListResponse getListReq(TGetListRequest arg)
        ...
    
;

我在其中提供了以下代码以在 main() 函数中启动服务器:

int main(int argc, char **argv)  
  int port = 9090;
  shared_ptr<TmyListServiceHandler> handler(new TmyListServiceHandler());
  shared_ptr<TProcessor> processor(new TmyListServiceProcessor(handler));
  shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  shared_ptr<TTransportFactory> transportFactory(new  TBufferedTransportFactory());
  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

  TSimpleServer server(processor, serverTransport, transportFactory,     protocolFactory);
  server.serve();
  return 0;

现在对我来说棘手的部分来了:

    在哪里实现对来自客户端到服务器的输入的解析?有人告诉我,这是使用 thrift 的简单实现,无需编写您的解析器或使用解析器生成器?我似乎无法弄清楚这一点?我在哪里告诉我的服务器如何处理输入,即要将其定向到哪个 Req 函数?

    在实现客户端方面,我有以下几点:

    int main(int argc, char **argv) 
     boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
     boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
     boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    
     TmyListServiceClient client(protocol);
     TGetListRequest arg;
     arg.id = 1;
     TGetListResponse argz;
     transport->open();
     client.send_getListReq( arg );
     printf("DONE SEDNING");
     client.recv_getListReq( argz );
     transport->close();
    
    return 0;
    
    

除了打开客户端连接之外,这基本上没有任何作用。它根本不接受任何输入。我确定这也与服务器中处理逻辑的实现有关,但是我应该在这里做什么才能让客户端准备好针对服务器进行测试?

【问题讨论】:

【参考方案1】:

你快完成了!

在您的服务器上显示此代码:

shared_ptr&lt;TProcessor&gt; processor(new TmyListServiceProcessor(handler));

处理程序在哪里?!这是实现您的服务的类。

类似:

class TmyListServiceHandler: public TmyListServiceIf public: virtual TGetListResponse getListReq(TGetListRequest arg) override ;

您的服务 (TmyListServiceIf) 的基础在:TmyListService.h 中生成

>

查看您对问题所做的修改,我认为您仍然有 处理程序问题。 Handler 类被声明:TGetListRequestHandler 在您添加的新代码中。

然而你正在构建 TmyListServiceHandler 的 main 函数 服务器。当人们在 IDL 中更改内容时,我已经看到了这种情况 并在不删除旧输出的情况下重新生成代码。你最终有两个 事物的不同定义。您需要使用 TmyListServiceHandler 到处。

根据您的 IDL,TGetListRequest 是一个结构,不应有处理程序。

【讨论】:

我已经在 .skeleton.cpp 文件中实现了处理程序类。我有一个类,其中实现了函数

使用 Apache Thrift 的 Java 编译错误

】使用ApacheThrift的Java编译错误【英文标题】:JavacompilationerrorsusingApacheThrift【发布时间】:2020-03-2418:06:08【问题描述】:我正在尝试为使用ApacheThrift的服务器和客户端使用不同的语言。我用Python编写了服务器,它运行起来没有任... 查看详情

go语言使用thrift协议实现客户端和服务端报notenoughargumentsincalltooprot.writemessagebegin错误解决方案

...ift.git/lib/go/thrift下载下来的库版本是不同导致的。可以先使用Thrift的IDL编译工具生成协议,然后进入git.apache.org/thrift.git目录,执行gitcheckout0.10.0然后重新编译即可。0.10.0就是对于IDL编译工具生成协议。 查看详情

go语言使用thrift协议实现客户端和服务端报notenoughargumentsincalltooprot.writemessagebegin错误解决方案

...ift.git/lib/go/thrift下载下来的库版本是不同导致的。可以先使用Thrift的IDL编译工具生成协议,然后进入git.apache.org/thrift.git目录,执行gitcheckout0.10.0然后重新编译即可。0.10.0就是对于IDL编译工具生成协议。 查看详情

Python 中使用 HTTPS 的示例 Apache Thrift 服务

】Python中使用HTTPS的示例ApacheThrift服务【英文标题】:ExampleApacheThriftservicewhichusesHTTPS,inPython【发布时间】:2013-04-3012:31:09【问题描述】:我已经能够找到examples,了解如何使用ApacheThrift来实现服务,该服务使用SSL作为Java中的传输... 查看详情

go语言使用thrift协议实现客户端和服务端报notenoughargumentsincalltooprot.writemessagebegin错误解决方案

正常步骤:安装golang的Thrift包:gogetgit.apache.org/thrift.git/lib/go/thrift安装Thrift的IDL编译工具:http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.exe编译客户端的时候会报错, 查看详情

go语言使用thrift协议实现客户端和服务端报notenoughargumentsincalltooprot.writemessagebegin错误解决方案

正常步骤:安装golang的Thrift包:gogetgit.apache.org/thrift.git/lib/go/thrift安装Thrift的IDL编译工具:http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.exe编译客户端的时候会报错, 查看详情

go语言使用thrift协议实现客户端和服务端报notenoughargumentsincalltooprot.writemessagebegin错误解决方案

正常步骤:安装golang的Thrift包:gogetgit.apache.org/thrift.git/lib/go/thrift安装Thrift的IDL编译工具:http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.exe编译客户端的时候会报错, 查看详情

openresty使用thrift安装步骤

最新想用Golang与Openresty相互通讯调用,使用RPC协议来实现,后来研究最终选择了Thrift;主要还是FB实现了支持Lua和Go模块,直接编译就可以成功嵌套使用,非常方便;研究了两天最后编译成功,于是便把使用步骤做下记录。1、Mac... 查看详情

Apache thrift 如何与 Apache hive 配合使用?

】Apachethrift如何与Apachehive配合使用?【英文标题】:HowdoesApachethriftfitswithApachehive?【发布时间】:2016-02-2508:52:33【问题描述】:为什么ApacheHive需要ApacheThrift?在Thrift的网站上,它说它可以编译成多种语言,但我不明白它适合哪... 查看详情

使用 Apache Thrift 的服务多路复用

】使用ApacheThrift的服务多路复用【英文标题】:ServiceMultiplexingusingApacheThrift【发布时间】:2013-11-0600:51:53【问题描述】:服务器代码:TMultiplexedProcessorprocessor=newTMultiplexedProcessor();processor.registerProcessor("AddService",newAddService.Proc 查看详情

如何使用 apache thrift 生成数组

】如何使用apachethrift生成数组【英文标题】:HowcanIgenerateaarraywithapachethrift【发布时间】:2014-02-1106:47:55【问题描述】:我需要用apachethriftidl生成一个java数组。谁能告诉我该怎么做?我已经尝试过使用List。这里是代码structsubscript... 查看详情

java.lang.IncompatibleClassChangeError:类 'org.apache.http.message.BasicHeader' 未实现接口 'org.apache.htt

】java.lang.IncompatibleClassChangeError:类\\\'org.apache.http.message.BasicHeader\\\'未实现接口\\\'org.apache.http.NameValuePair\\\'【英文标题】:java.lang.IncompatibleClassChangeError:Class\'org.apache.http.message.BasicHeader\'does 查看详情

在 Apache Thrift 中使用 seqid 的目的是啥?

】在ApacheThrift中使用seqid的目的是啥?【英文标题】:ForwhichpurposesusedseqidinApacheThrift?在ApacheThrift中使用seqid的目的是什么?【发布时间】:2015-03-0415:57:01【问题描述】:我正在研究如何使用ApacheThrift库,发现任何发送/接收操作都... 查看详情

使用 Apache Thrift 时如何检测协议不匹配?

】使用ApacheThrift时如何检测协议不匹配?【英文标题】:HowtodetectprotocolmismatchwhileusingApacheThrift?【发布时间】:2015-08-0709:23:03【问题描述】:我正在我的Mac上运行一对使用ApacheThrift进行通信的客户端和服务器程序。在我们的生产... 查看详情

是否可以在常规 Web 服务器上使用 Apache Thrift?

】是否可以在常规Web服务器上使用ApacheThrift?【英文标题】:IsitpossibletouseApacheThriftonaregularwebserver?【发布时间】:2012-02-1021:54:44【问题描述】:我已经有一个付费的网络服务器,我想使用Thrift和PHP在上面公开一些服务。我的问... 查看详情

将您自己的通信协议与 apache thrift 一起使用?

】将您自己的通信协议与apachethrift一起使用?【英文标题】:Useyourowncommunicationprotocolwithapachethrift?【发布时间】:2012-08-1522:36:30【问题描述】:我想知道是否可以只使用apachethrift的序列化部分。我基本上已经创建了一个自定义通... 查看详情

我可以使用 Apache Thrift 获得可发现的服务吗?

】我可以使用ApacheThrift获得可发现的服务吗?【英文标题】:CanIhavediscoverableservicesusingApacheThrift?【发布时间】:2014-09-0500:16:29【问题描述】:目标是拥有可以计算事物的可发现的thrift服务器,特别是我想知道它们有哪些服务以... 查看详情

thrift框架-安装

1.前言今天接触了使用PRC【远程过程调用协议】的Thrift框架 ,留下随笔心得,这是安装篇2.下载去apache官网下载Thrift脚本编译程序,window则下载一个exe文件,然后将文件的路径设置在系统环境变量path即可Thrift下载地址: &... 查看详情