`
haoningabc
  • 浏览: 1456505 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

thrift的测试

阅读更多
尝试失败的版本
0.9.1 要求autoconf是2.56以上,默认的centos6.5是2.53
0.9.0
0.8.0 gcc版本不能是4.4,make写得太严格了,生成的代码,在gcc4.4下不可用
都不行
查到的解决方法http://blog.csdn.net/hoho568/article/details/7321611 尝试也不都能通过

最终确认
0.7.0

参考:
http://blog.csdn.net/hbuxiaoshe/article/details/6558391
http://jinghong.iteye.com/blog/1222713
下载
http://archive.apache.org/dist/thrift/0.7.0/thrift-0.7.0.tar.gz

yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel openssl-devel
yum remove qt
./configure --prefix=/usr/local/thrift --with-java --with-cpp --with-python
make
make install
设置环境变量
export PATH=$HADOOP_HOME/bin:$HIVE_HOME/bin:/usr/local/java/jdk1.6.0_45/bin:$HBASE_HOME/bin:$PATH:/usr/local/thrift/bin:/usr/local/git/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/thrift/lib


student.thrift:
struct Student{
 1: i32 sno,
 2: string sname,
 3: bool ssex,
 4: i16 sage,
}
service Serv{
 void put(1: Student s),
}

thrift -r --gen cpp student.thrift
vim  client.cpp
----------------
include "Serv.h"
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.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) {
    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();

    // 我们的代码写在这里
    Student s;
    s.sno = 123;
    s.sname = "xiaoshe";
    s.ssex = 1;
    s.sage = 30; 
    ServClient client(protocol);
    client.put(s);
    //---
    transport->close();

    return 0;
}

----------------


---------------------------
[root@localhost gen-cpp]# cat Serv_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 "Serv.h"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <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;

class ServHandler : virtual public ServIf {
 public:
  ServHandler() {
    // Your initialization goes here
  }

  void put(const Student& s) {
    // Your implementation goes here
    printf("sno=%d sname=%s ssex=%d sage=%d/n", s.sno, s.sname.c_str(), s.ssex, s.sage);//这里是新加的
    printf("put\n");
  }

};

int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr<ServHandler> handler(new ServHandler());
  shared_ptr<TProcessor> processor(new ServProcessor(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/thrift/include/thrift -L/usr/local/thrift/lib/ -lthrift Serv.cpp student_types.cpp student_constants.cpp Serv_server.skeleton.cpp -o server
g++ -g -I/usr/local/thrift/include/thrift -L/usr/local/thrift/lib/ -lthrift -lm -pthread -lz -lrt -lssl Serv.cpp student_types.cpp student_constants.cpp client.cpp -o client


root@localhost gen-cpp]# ./server &
[1] 8553
[root@localhost gen-cpp]# ./client
sno=123 sname=xiaoshe ssex=1 sage=30/nput  (这里是server返回的)
[root@localhost gen-cpp]#

-------------------java客户端----------
--------code begin----
import org.apache.thrift.TException;                                                                                                                                    
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

//import UserService.Client;

public class UserClient {
    private void start() {
        try {
            TTransport socket = new TSocket("localhost", 9090);
            //TTransport transport = new TFramedTransport(socket);
            TProtocol protocol = new TCompactProtocol(socket);

            UserService.Client client = new UserService.Client(protocol);
            socket.open();
            System.out.println(client.get("lll"));

            User u = new User();
            u.uid="leojava";
            u.uname="yueyue";
            u.usex=true;
            u.uage=3;
            client.add(u);
            socket.close();

        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        }   
    }   

    public static void main(String[] args) {
        UserClient c = new UserClient();
        c.start();

    }   
}

--------code end----

编译
javac -classpath /usr/local/lib/libthrift-0.7.0.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/commons-lang-2.5.jar:/usr/local/lib/commons-codec-1.3.jar:/usr/local/lib/httpclient-4.0.1.jar:/usr/local/lib/httpcore-4.0.1.jar:/usr/local/lib/servlet-api-2.5.jar UserClient.java ./gen-java/*.java
java -classpath .:/usr/local/lib/libthrift-0.7.0.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/commons-lang-2.5.jar:/usr/local/lib/commons-codec-1.3.jar:/usr/local/lib/httpclient-4.0.1.jar:/usr/local/lib/httpcore-4.0.1.jar:/usr/local/lib/servlet-api-2.5.jar:/usr/local/lib/slf4j-log4j12-1.6.1.jar:/usr/local/lib/log4j-1.2.17.jar:./gen-java/ UserClient

结果:
客户端
[root@localhost acsuser]# java -classpath .:/usr/local/lib/libthrift-0.7.0.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/commons-lang-2.5.jar:/usr/local/lib/commons-codec-1.3.jar:/usr/local/lib/httpclient-4.0.1.jar:/usr/local/lib/httpcore-4.0.1.jar:/usr/local/lib/servlet-api-2.5.jar:/usr/local/lib/slf4j-log4j12-1.6.1.jar:/usr/local/lib/log4j-1.2.17.jar:./gen-java/ UserClient
SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
User(uid:leo1, uname:yueyue, usex:true, uage:3)
[root@localhost acsuser]#
服务端
[root@localhost acsuser]# ./UserServer
start user server...
uid=leo1 uname=yueyue usex=1 uage=3
uid=leojava uname=yueyue usex=1 uage=3


------------python---客户端
#!/usr/bin/env python
import sys
sys.path.append('./gen-py')
from acsuser import UserService
from acsuser.ttypes import *
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TCompactProtocol

# Make socket
transport = TSocket.TSocket('localhost', 9090)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
protocol = TCompactProtocol.TCompactProtocol(transport)
# Create a client to use the protocol encoder
client = UserService.Client(protocol)
# Connect!
transport.open()
# Call Server services  
u = client.get('lll')
print 'uid=%s uname=%s usex=%d u.uage=%d' %(u.uid,u.uname,u.usex,u.uage)

u1 = User()
u1.uid='leo'
u1.uname='yueyue'
u1.usex=1
u1.uage=3
client.add(u1)



结果
python PythonClient.py
uid=leo1 uname=yueyue usex=1 u.uage=3

-----------------传list的例子--------------
struct User{
 1: string uid,
 2: string uname,
 3: bool usex,
 4: i16 uage,
 5: list<i32> subNodeList,
 6: map<i32,string> subNodeMap,
 7: set<i32> subNodeSet
}
service UserService{
 void add(1: User u),
 void addlist(1: list<User> u),
 User get(1: string uid),
}


UserServer.cpp :
[root@haoning listuser]# cat UserServer.cpp 
#include "UserService.h"
#include <vector>
#include <config.h>
//#include <protocol/TBinaryProtocol.h>
#include <protocol/TCompactProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>
#include <concurrency/ThreadManager.h>
#include <concurrency/PosixThreadFactory.h>
#include <server/TThreadPoolServer.h>
#include <server/TThreadedServer.h>
using namespace std;
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace ::apache::thrift::concurrency; 

using boost::shared_ptr;

class UserServiceHandler : virtual public UserServiceIf {
 public:
  UserServiceHandler() {
    // Your initialization goes here
  }

  void add(const User& u) {
    // Your implementation goes here
    printf("uid=%s uname=%s usex=%d uage=%d\n", u.uid.c_str(), u.uname.c_str(), u.usex, u.uage);
    printf("add\n");
  }
  //void addlist(const std::vector<User, std::allocator<User> > & u) {
  void addlist(const std::vector<User> & u) {
    std::vector<User>::const_iterator iter;
    for(iter=u.begin();iter!=u.end();iter++) {
      printf("u size: %d\n",u.size());
      printf("send 1: %s, %s\n",(*iter).uid.c_str(),(*iter).uname.c_str());
    }    
    //User rect;
    //rect.uid="ning";
    //rect.uname="haohao";
    //rect.usex=0;
    //rect.uage=3;
    //u.push_back(rect);
    //for(int x = 0; x < 5; x++)
    //{
      //  u.push_back(new User());
    //}
    //std::vector<User, std::allocator<User>>::iterator it=u.begin();
    //for(it=u.begin();it!=u.end();it++){
    //    std::cout<<"test"<<std::endl;
       // cout<<(*it).id<<endl;
     //   printf("addlist---\n");
   // }   
    printf("addlist---\n");
  }
 // void addlist(const std::Vector<User>& vec) {
 //   std::vector<User>::iterator it=vec.begin();
 //   for(it=vec.begin();it!=vec.end();it++){
 //      // cout<<"test"<<endl;
 //     //  std::cout<<"addlist:"<<(*it).uid<<std::endl;
 //     printf("addlist id --%s\n",(*it).uid,c_str()); 
 //   } 
 //   printf("addlist ---------\n");
 // }

  void get(User& _return, const std::string& uid) {
// Your implementation goes here  
    _return.uid = "tinghaode";  
    _return.uname = "ningge";  
    _return.usex = 1;  
    _return.uage = 3;  
    printf("uid=%s uname=%s usex=%d uage=%d\n", _return.uid.c_str(), _return.uname.c_str(), _return.usex, _return.uage);  
    printf("get\n");
  }

};

int main(int argc, char **argv) {
  shared_ptr<UserServiceHandler> handler(new UserServiceHandler());
  shared_ptr<TProcessor> processor(new UserServiceProcessor(handler));
  shared_ptr<TProtocolFactory> protocolFactory(new TCompactProtocolFactory());
  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090));

  shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(10);
  shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
  threadManager->threadFactory(threadFactory);
  threadManager->start();
  printf("start user server...\n");

  TThreadPoolServer server(processor, serverTransport, transportFactory, protocolFactory, threadManager);
  server.serve();
  return 0;
}

[root@haoning listuser]# 


UserClient.cpp :
[root@haoning listuser]# cat UserClient.cpp 
#include "UserService.h"
#include <config.h>
#include <vector>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TCompactProtocol.h>

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

using boost::shared_ptr;

int main(int argc, char **argv) {
        boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
        boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
        boost::shared_ptr<TProtocol> protocol(new TCompactProtocol(transport));

        transport->open();

        User u;
        u.uid = "leo";
        u.uname = "yueyue";
        u.usex = 1;
        u.uage = 3;

        UserServiceClient client(protocol);
        client.add(u);
        
        //-----list begin--
        vector<User> vec;
        User rect;
        rect.uid="ning";
        rect.uname="haohao";
        rect.usex=0;
        rect.uage=3;
        vec.push_back(rect); 
        client.addlist(vec);        
        //-----list end----        
    
        User u1; 
        client.get(u1,"lll");

        transport->close();
        printf("uid=%s uname=%s usex=%d uage=%d\n", u1.uid.c_str(), u1.uname.c_str(), u1.usex, u1.uage);
        return 0;
}
[root@haoning listuser]# 

注意
std::vector<User>::const_iterator iter
用const_iterator
分享到:
评论

相关推荐

    thrift测试程序

    标题“thrift测试程序”表明这是一个使用Thrift实现的测试程序,可能包含了服务端和客户端的实现,用于验证Thrift框架在实际应用中的功能和性能。Thrift的测试程序通常会包括以下组成部分: 1. IDL 文件:这是...

    C# Thrift测试

    thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Go,Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, ...

    thriftTest java案例代码

    为了运行这个案例,你需要编译Java代码,然后分别启动服务器和客户端,测试它们之间的通信。 总之,ThriftTest Java案例代码展示了如何在Windows环境下利用Thrift框架实现Java服务的开发。通过Thrift,你可以轻松地...

    php_thrift_python安装测试记录

    本篇文章将围绕“php_thrift_python安装测试记录”这一主题,深入探讨如何在PHP中使用Thrift客户端调用Python服务端,并分享相关的安装步骤和代码示例。 首先,我们来看Thrift的基本工作原理。Thrift通过一种中间...

    gen-cpp.rar_CppServer.cpp_gencpp_thrift_thrift gen_thrift gen-cp

    本例是一个简单的thrift测试程序,包括客户端和服务端。thrift允许你定义一个简单的定义文件中的数据类型和服务接口。以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝跨编程语言。

    thrift实现http协议案例

    这个案例中的“MyThriftWebTest”可能是一个包含整个实现的测试工程,包括Thrift服务的定义、生成的Java代码以及Servlet的实现。通过运行这个测试工程,你可以了解如何将Thrift服务部署到支持HTTP的服务器上,如...

    使用wireshark抓取thrift协议接口调用

    - 调试:在开发和测试环境中,使用Wireshark抓取Thrift流量可以帮助定位接口调用的问题,如序列化错误、网络通信问题等。 - 监控:在生产环境中,定期抓取Thrift流量可监控服务的健康状况,及时发现潜在的性能瓶颈...

    VS2017_Hbase thrift C++接口测试

    **VS2017_Hbase thrift C++接口测试** 在这个项目中,我们主要探讨如何在Visual Studio 2017(VS2017)环境下,利用C++语言通过Thrift库来与HBase数据库进行交互。Thrift是一种跨语言的服务开发框架,它允许我们...

    maven-thrift-server

    - "thrift-server 最简单的测试案例"表明,项目可能包含一个简单的服务器实现和对应的测试用例,用来验证Thrift服务的正确性。 - 测试通常使用JUnit或类似框架,模拟客户端调用,检查服务器的响应是否符合预期。 ...

    Thrift-java学习小结

    除了编译器,Thrift还提供了其他工具,如thrift-gen用于生成代码,thrift-test用于测试服务,以及thrift-server作为简单的HTTP服务器,便于快速调试服务。 九、Thrift与微服务 在微服务架构中,Thrift因其轻量级和...

    php使用thrift客户端访问服务器测试

    php 使用thrift客户端访问服务器测试 下载后 放到站点根目录,设置 thrift_test.php里的 $socket = new TSocket('10.200.28.43', 10001); 修改为默认服务器和端口 后 直接访问 http://localhost/thrift_test.php

    thrift阻塞与非阻塞模式下的测试

    通过对Thrift阻塞和非阻塞模式的测试,我们可以得出适合特定业务场景的最佳实践,优化服务性能,提高系统的整体效率。在实际应用中,可能需要结合具体情况,选择单线程阻塞、多线程阻塞、异步IO、NIO或AIO等模式的...

    Windows下QT使用Thrift的样例

    运行服务端,然后启动客户端,测试它们之间的通信是否正常。 在压缩包中,"QT使用Thrift样例"应该包含了完整的代码示例,包括QT工程文件、Thrift服务定义文件、以及如何集成Boost的示例。通过学习和分析这个样例,...

    zk+thrift demo

    在【压缩包子文件的文件名称列表】"testThrift2" 中,我们可以推测这是一个测试 Thrift 的第二个版本。通常,这样的文件夹或项目会包含以下组成部分: 1. **Thrift IDL 文件**(.thrift):这是 Thrift 服务的核心...

    linux下安装和测试thrift

    在Linux环境下安装和测试Thrift是一项技术性较强的工作,Thrift是一种开源的跨语言服务开发框架,它允许程序员定义服务接口和服务数据类型,然后自动生成客户端和服务器端的代码,以便于不同编程语言之间进行高效、...

    protobuf/thrift/avro-序列化性能测试工程

    这个名为"protobuf/thrift/avro-序列化性能测试工程"的项目专注于对比这三种技术的性能。 protobuf是Google开发的一种高效的数据序列化协议,它提供了简洁、快速和跨平台的序列化方法。protobuf通过定义.proto文件...

    ice-dubbo-thrift-grpc性能测试对比

    ice-dubbo-thrift-grpc性能测试对比 ,ice-dubbo-thrift-grpc性能测试对比,ice-dubbo-thrift-grpc性能测试对比,ice-dubbo-thrift-grpc性能测试对比

    thrift编译的小test程序

    总之,“thrift编译的小test程序”是一个使用Thrift进行服务开发的示例,涵盖了从定义服务接口到编译、运行和测试的完整过程。通过这个程序,你可以深入理解Thrift的工作原理,并掌握如何在实际项目中应用Thrift来...

    Java Thrift demo例子

    5. 测试与调试: 运行服务端程序,然后启动客户端,可以看到客户端成功调用了服务端的方法,并接收到返回结果。 6. 扩展与优化: Thrift支持多种传输层协议(如HTTP、TCP、ZMQ等),可以根据需求选择合适的协议。...

    thrift0.9.3附带jar包

    - `libthrift-0.9.3-test.jar`:Thrift的测试库,可能包含了一些示例代码和测试用例。 使用这些jar包,开发者可以快速地在Java环境中搭建Thrift服务,实现高效、跨平台的RPC通信。首先,需要将这些jar包添加到项目...

Global site tag (gtag.js) - Google Analytics