Thrift客户端服务器示例

前言

这里我采用的是Java作为服务端,C++作为客户端

官方文档

Thrift文件

namespace java com.tubai.thrift

enum Action{
    Hello = 1,
    Query = 2
}

struct Request{
    1: required Action type,
    2: required string name,
    3: optional i32    age,
}

service ActionService{
    void doAction(1:Request req)
}

服务器

首先根据官网文档生成代码,然后我们只需要编写ActionService的实现类以及一个程序的入口(也就是main函数)
如下所示

public class ActionServiceImpl implements ActionService.Iface{
    public void doAction(Request req) throws org.apache.thrift.TException{
        System.out.println("===>" + req);
    }
}
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;

import java.io.IOException;
import java.net.ServerSocket;

public class JavaServer{
    public static void main(String[] args){
        ServerSocket socket = null;
        try {
            socket = new ServerSocket(9090);
            TServerSocket serverTransport = new TServerSocket(socket);
            ActionService.Processor processor = new ActionService.Processor(new ActionServiceImpl());
            TServer server = new TSimpleServer(new TServer.Args(serverTransport).processor(processor));
            System.out.println("Running server...");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

然后用maven打成jar包上传至Linux服务器即可;

可能的坑点

java.lang.NoClassDefFoundError找不到对应的类

打开jar包中的MANIFEST.MF文件,将其中显示的依赖jar包放在我们执行jar包的同个文件夹即可

比如我的是这样的
在这里插入图片描述

那么Linux服务器文件图如下

在这里插入图片描述

客户端

同样先根据thrift文件生成代码,这里因为是客户端,所以我们删除名字为xxx_server.skeleton.cpp文件,这个是服务器用的,客户端不需要;

然后编写客户端代码如下;

#include <iostream>                                                                                                                                                    
                                                                                                                                                                       
#include <thrift/protocol/TBinaryProtocol.h>                                                                                                                           
#include <thrift/transport/TSocket.h>                                                                                                                                  
#include <thrift/transport/TTransportUtils.h>                                                                                                                          
//路径自己改一下                                                                                                                                                                       
#include "../gen-cpp/ActionService.h"                                                                                                                                  
#include "../gen-cpp/hello_types.h" 

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

int main() {
  std::shared_ptr<TTransport> socket(new TSocket("你的服务器地址", 9090));
  std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
  std::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
  ActionServiceClient client(protocol);
  try {
    transport->open();
    //do something
    Request r;
    r.type = Action::Hello;
    r.name = "xxx";
    r.age  = 21;
    client.doAction(r);
    transport->close();
  } catch (TException& tx) {
    cout << "ERROR: " << tx.what() << endl;
  }
}

然后编译,链接,运行即可;

命令如下

g++ -c client.cpp
g++ -c ../gen-cpp/*.cpp
g++ *.o -o client -lthrift
./client
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值