您的位置:首页 > 运维架构 > Apache

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

2017-09-01 16:53 567 查看
新建一个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


就可以本机看到远程主机计算的结果了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  apache thrift RPC