thrift编写服务端和客户端程序

2023-05-16

文章目录

        • Thrift的学习
        • Thrift的安装
        • Thrift的代码包目录介绍
        • Thrift文件书写
        • 服务端的更改
        • 客户端的编写
        • 编译所遇到的问题

Thrift的学习

参考师兄wiki thrift基础

Thrift的安装

参考社区wiki linux下配置thrift

Thrift的代码包目录介绍

在server和client中都使用到了其中的文件
Thrift的类介绍
Thrift的代码包目录介绍

  1. concurrrncy:并发和时钟管理方面的库
  2. processor:Processor相关类
  3. transport:transport相关类,负责数据传输。
  4. server:server相关类,服务器类型和相关的请求
  5. protocal:protocal相关类,负责数据编码

Thrift文件书写

书写原则在Thrift学习中了解到

namespace cpp TTG 
namespace go TTG 

enum ResponseState {
    StateOK = 0,
    StateError = 1,
    StateEmpty = 2 
}

struct Request {
    1:i32 studentID = 0 
}

struct Response {
    1:i32 studentID = 0,
    2:string name,
    3:list<string> infos,
    4:ResponseState state
}

service TTGService {
    Response getStudentInfo(1:Request request);
}

这就是一个简单的获取学生信息的服务,使用CPP作为服务端和客户端。

服务端的更改

生成的gen-cpp文件目录如下

.
├── TTG_constants.cpp
├── TTG_constants.h
├── TTGService.cpp
├── TTGService.h
├── TTGService_server.skeleton.cpp//主要修改的文件
├── TTG_types.cpp
└── TTG_types.h
#include "TTGService.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  ::TTG;

class TTGServiceHandler : virtual public TTGServiceIf {
 public:
  TTGServiceHandler() {
    // Your initialization goes here
  }

/*
客户端调用此接口就相当于调用本地的函数一样
*/
  void getStudentInfo(Response& _return, const Request& request) {
  //这是我们修改的位置,也就是接口的位置
    // Your implementation goes here
    printf("getStudentInfo\n");

    printf("request.studentID:%d",request.studentID);
    _return.studentID = request.studentID;
    _return.name = "hello";
    _return.infos.push_back("测试1");
    _return.infos.push_back("测试2");
    _return.state = ResponseState::StateOK;
  }

};

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

客户端的编写

客户端需要自己重新编写一个程序

#include "TTGService.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TCompactProtocol.h>

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

using boost::shared_ptr;

int main(int argc, char **argv) {
/*
前面的都是套路,都属于必须要做且大致相同。首先要知道thrift生成的文件是干什么的。
*/
    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));

    transport->open();
    TTG::Request r;
    r.studentID = 1;
    TTG::Response resp;
    TTG::TTGServiceClient client(protocol);
	//调用服务端的方法且成功。
    client.getStudentInfo(resp,r);
    transport->close();
    printf("ID=%d name=%s state=%d\n\n",resp.studentID,resp.name.c_str(),resp.state);
    return 0;
}

编译所遇到的问题

i. 编译的时候直接进行编译并不可行,需要对thrift库进行链接,主要包括了以下三个方面:

  • /data7/fengwei/wenwen_proj/trunk/common_thrift0.9/include/(thrift头文件的路径)
  • /data7/fengwei/wenwen_proj/trunk/common_thrift0.9/lib/(指定当前lib文件路径)
  • lthrift

ii. 运行找不到动态库

PingShan_fengwei@10.241.136.159:~/fw/gen-cpp> ./xxx_server 
./xxx_server: error while loading shared libraries: libthrift-0.9.1.so: cannot open shared object file: No such file or directory

首先ldd查询动态库链接情况

找到动态库的位置进行链接。

PingShan_fengwei@10.241.136.159:~/fw/gen-cpp> find ~/wenwen_proj/ -name libthrift-0.9.1.so
/data7/fengwei/wenwen_proj/trunk/common_thrift0.9/lib/libthrift-0.9.1.so

将lib位置临时进行export

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data7/fengwei/wenwen_proj/trunk/common_thrift0.9/lib
source ~/bash.rc

此时再运行server和client即可

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

thrift编写服务端和客户端程序 的相关文章

随机推荐