精华内容
下载资源
问答
  • Thrift

    2020-07-29 16:41:58
    Thrift入门

    Example

    1).thrift

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

    2)自动生成文件

    thrift -r --gen cpp student.thrift
    

    3)修改.skeleton.cpp

    class ServerServiceHandler : virtual public ServerServiceIf {
     public:
      ServerServiceHandler() {
        // Your initialization goes here
        //这里写初始化
        //构造函数
        printf("Initialize\n");
      }
    
      void show(const Student& student) {
        // Your implementation goes here
        //这里写需要实现的服务
        //用于处理客户端传送来的参数
        printf("show\n");
    	//用printf写一个简单的数据处理服务
        printf("ID = %d Name = %s Sex = %d Age = %d/n", s.ID, s.Name.c_str(), s.Sex, s.Age);
      }
    };
    

    4)使用thrift包自动生成的client类来实现客户端

    就是用来调用client类就可以了,真正的client功能其实已经实现了

    #include "ServerService.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 student;
        s.ID = 2016220102031;
        s.Name = "WYZ";
        s.Sex = 1;
        s.sage = 22;
    
        ServerServiceClient client(protocol);
    
        client.put(student);
    
    	transport->close();
    
    	return 0;
    }
    

    Reference

    由浅入深了解Thrift(一)——Thrift介绍与用法

    thrift一个例子

    Thrift入门

    Thrift基础-linux环境搭建+cmake编译

    展开全文
  • thrift

    2019-10-02 08:24:09
    1.下载window版本的thrift编译器 2.下载idea的thirft插件 3.设置thrift编译工具为:步骤1下载的编译器 4.编写thrift文件 namespace java org.chenhao.test.thrift service Hello{ void helloVoid() } ...

    环境准备:

    1.下载window版本的thrift编译器

    2.下载idea的thirft插件

    3.设置thrift编译工具为:步骤1下载的编译器

    4.编写thrift文件

    namespace java org.chenhao.test.thrift
        service  Hello{
            void helloVoid()
        }
    

    5.编译改文件,生成Hello.java

    6.编写HelloServiceImpl,实现了Iface接口

    package org.chenhao.test.thrift;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class HelloServiceImpl implements Hello.Iface {
        private static final Logger logger= LoggerFactory.getLogger(HelloServiceImpl.class);
        @Override
        public void helloVoid(){
            logger.info("hello world");
        }
    }

    7.编写HelloServer.java

    package org.chenhao.test.thrift;
    
    import org.apache.thrift.TProcessor;
    import org.apache.thrift.protocol.TBinaryProtocol;
    import org.apache.thrift.server.TServer;
    import org.apache.thrift.server.TThreadPoolServer;
    import org.apache.thrift.transport.TServerSocket;
    
    public class HelloServer {
        public static void main(String[] args) {
            try {
                TServerSocket tansport = new TServerSocket(7091);
                TProcessor processor = new Hello.Processor(new HelloServiceImpl());
                TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory();
                TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(tansport).processor(processor).protocolFactory(factory));
                server.serve();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    8.编写HelloClient.java

    package org.chenhao.test.thrift;
    
    import org.apache.thrift.protocol.TBinaryProtocol;
    import org.apache.thrift.protocol.TProtocol;
    import org.apache.thrift.transport.TSocket;
    import org.apache.thrift.transport.TTransport;
    
    public class HelloClient {
        public static void main(String[] args) {
            try {
                TTransport transport = new TSocket("localhost", 7091);
                transport.open();
                TProtocol protocol = new TBinaryProtocol(transport);
                Hello.Client client = new Hello.Client(protocol);
                client.helloVoid();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    9.运行HelloServer.java

    10.运行HelloClient.java,可以观察到命令行打印如下信息:

     

    转载于:https://www.cnblogs.com/swaggyC/p/6039908.html

    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 14,139
精华内容 5,655
关键字:

thrift