【发布时间】:2015-08-07 09:23:03
【问题描述】:
我正在我的 Mac 上运行一对使用 Apache Thrift 进行通信的客户端和服务器程序。在我们的生产系统中,我们最终可能会遇到客户端使用TJSONProtocol而服务器使用TBinaryProtocol进行序列化和反序列化的情况。
我知道这是一件可怕的事情。但我不确定如何事先检测到这一点。我尝试了一个具有不同协议的示例程序。我原以为会收到异常,但客户端卡在 RPC 调用上并且从未返回。
使用TBinaryProtocol的服务器代码:
shared_ptr<SomethingHandler> handler(new SomethingHandler());
shared_ptr<TProcessor> processor(new SomethingProcessor(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();
使用TJSONProtocol的客户端代码:
boost::shared_ptr < TSocket > socket(new TSocket(argv[1], 9090));
boost::shared_ptr < TTransport > transport(new TBufferedTransport(socket));
boost::shared_ptr < TProtocol > protocol(new TJSONProtocol(transport));
transport->open();
std::cout<<"Transport open success"<<std::endl;
SomethingClient client(protocol);
std::cout<<"Create client success"<<std::endl;
try{
std::cout<<"About to ping"<<std::endl;
client.ping(argv[2]);
std::cout<<"Ping success"<<std::endl;
}catch(TException e){
std::cout<<"Exception occurred:"<<e.what()<<std::endl;
}
transport->close();
在这个例子中,我从来没有得到Ping success 来打印。
有什么方法可以检测到这种不兼容性而不会卡住?
【问题讨论】: