使用RPC框架Apache Thrift在远程主机运算并返回

47 篇文章 2 订阅
8 篇文章 0 订阅

新建一个count.thrift文件:


#

/**
 * Thrift files can namespace, package, or prefix their output in various
 * target languages.
 */
namespace cpp freebird

/**
 * Defining a removed class named WorkerManager
 */
service WorkerManager {

  /**
   * client calls ping method to make sure service process is active or dead
   */
   void count(),
   i32 add(1:i32 a,2:i32 b),
   i32 dec(1:i32 a,2:i32 b),
   i32 multi(1:i32 a,2:i32 b),
   i32 div(1:i32 a,2:i32 b),
}

然后在count.thrift文件同目录下执行:

thrift -r --gen cpp count.thrift 
cd gen-cpp
vim WorkerManager_server.skeleton.cpp

修改WorkerManager_server.skeleton.cpp如下:

// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.

#include "WorkerManager.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;

using namespace  ::freebird;

class WorkerManagerHandler : virtual public WorkerManagerIf {
 public:
  WorkerManagerHandler() {
    // Your initialization goes here
  }

  /**
   * client calls ping method to make sure service process is active or dead
   */
  void count() {
    // Your implementation goes here
    printf("count\n");
  }

  int32_t add(const int32_t a, const int32_t b) {
    // Your implementation goes here
    printf("add\n");
    return a + b;
  }

  int32_t dec(const int32_t a, const int32_t b) {
    // Your implementation goes here
    printf("dec\n");
    return a - b;
  }

  int32_t multi(const int32_t a, const int32_t b) {
    // Your implementation goes here
    printf("multi\n");
    return a*b;
  }

  int32_t div(const int32_t a, const int32_t b) {
    // Your implementation goes here
    printf("div\n");
    return a/b;
  }

};

int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr<WorkerManagerHandler> handler(new WorkerManagerHandler());
  shared_ptr<TProcessor> processor(new WorkerManagerProcessor(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;
}

然后编译执行:

g++ -g -I/usr/local/include/thrif -L /usr/local/lib/*.so -lthrift WorkerManager.cpp count_types.cpp count_constants.cpp WorkerManager_server.skeleton.cpp -o serve

./serve

至此,服务端配置完成,监听也已经开启。
然后在另一台主机装好Apache Thrift,并用上面同样的方式生成客户端代码。
进入到客户端机器的gen-cpp目录,执行:

touch Client.cpp
gedit Client.cpp

键入以下代码:

#include "WorkerManager.h"  // Your .h File 
#include <iostream> 
#include <thrift/transport/TSocket.h>  
#include <thrift/transport/TBufferTransports.h>  
#include <thrift/protocol/TBinaryProtocol.h>  
using namespace std;
using namespace apache::thrift;  
using namespace apache::thrift::protocol;  
using namespace apache::thrift::transport;  
using boost::shared_ptr;
using namespace std;
using namespace  ::freebird;


int main(int argc, char **argv) {  
    boost::shared_ptr<TSocket> socket(new TSocket("server_ip", 9090));//替换ip
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));  
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));  
    WorkerManagerClient client(protocol);

    transport->open();  

    // Your Codes 
    client.count();
    cout << "1 + 1 = " << client.add(1, 1) << endl;
    cout << "2 * 5 = " << client.multi(1, 1) << endl;
    cout << "1 -10 = " << client.dec(1, 10) << endl;
    cout << "6 / 2 = " << client.div(1, 1) << endl;
    transport->close();
    return 0;  
}  

然后:

g++ -g -I/usr/local/include/thrif -L /usr/local/lib -lthrift testping_constants.cpp Client.cpp WorkerManager.cpp testping_types.cpp -o client
#-I 链接include目录,-L链接lib文件
./client

就可以本机看到远程主机计算的结果了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值