• thrift的简单使用


    写在前面

    本文一起看下一种由facebook出品的rpc框架thrift。

    源码

    1:开发步骤

    1:编写thrift idl文件
    2:根据thrift idl文件生成java模板代码
    3:继承模板代码的*.Iface接口给出server的具体服务实现
    4:使用模板的HelloWorldService.Processor编写server端
    5:使用HelloWorldService.Client编写服务端调用程序
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2:实战

    • 准备编译生成器
      这里

    • idea准备插件
      在这里插入图片描述

    无该步骤也可以

    • 编写idl
      hello.thrift:
    service HelloWorldService {
      string say(1: string username)
    }
    
    • 1
    • 2
    • 3
    • 通过生成器生成模板文件
    $ ./thrift-0.19.0.exe -gen java hello.thrift
    
    • 1

    在这里插入图片描述

    生成的模板Java文件很长,主要关注如下几个类即可:

    Iface:服务端通过实现此接口提供同步服务
    AsyncIface:服务端通过实现此接口提供异步服务
    Client:客户端通过此类的实例对象以同步的方式访问服务端
    AysyncClient:客户端通过此类的是实例以异步的方式访问服务端
    
    • 1
    • 2
    • 3
    • 4

    将生成的代码拷贝到项目,备用。

    • pom
    <dependency>
        <groupId>org.apache.thriftgroupId>
        <artifactId>libthriftartifactId>
        <version>0.19.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • service实现类
    public class HelloWorldServiceImpl implements HelloWorldService.Iface {
        @Override
        public String say(String username) throws TException {
            return "Hello " + username;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • server类
    public class SimpleServer {
        public static void main(String[] args) throws Exception {
            ServerSocket serverSocket = new ServerSocket(ServerConfig.SERVER_PORT);
            TServerSocket serverTransport = new TServerSocket(serverSocket);
            HelloWorldService.Processor processor =
                    new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldServiceImpl());
    
            TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();
            TSimpleServer.Args tArgs = new TSimpleServer.Args(serverTransport);
            tArgs.processor(processor);
            tArgs.protocolFactory(protocolFactory);
    
            // 简单的单线程服务模型 一般用于测试
            TServer tServer = new TSimpleServer(tArgs);
            System.out.println("Running Simple Server");
            tServer.serve();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    启动。

    • client类
    public class SimpleClient {
        public static void main(String[] args) {
            TTransport transport = null;
            try {
                transport = new TSocket(ServerConfig.SERVER_IP, ServerConfig.SERVER_PORT, ServerConfig.TIMEOUT);
                TProtocol protocol = new TBinaryProtocol(transport);
                HelloWorldService.Client client = new HelloWorldService.Client(protocol);
                transport.open();
    
                String result = client.say("Leo");
                System.out.println("Result =: " + result);
            } catch (TException e) {
                e.printStackTrace();
            } finally {
                if (null != transport) {
                    transport.close();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    运行:

    Result =: Hello Leo
    
    Process finished with exit code 0
    
    • 1
    • 2
    • 3

    酱!!!

    写在后面

    参考文章列表

    Apache Thrift系列详解(一) - 概述与入门

  • 相关阅读:
    NLP主流大模型如GPT3/chatGPT/T5/PaLM/LLaMA/GLM的原理和差异有哪些-详细解读
    基于eXosip2实现的客户端和服务端
    数据结构--栈和队列
    Redis客户端-引入jedis
    涡流搜索算法(Matlab代码实现)
    Java#20(包和final)
    最佳联盟营销软件解决方案:简化你的联盟管理
    Android -- 每日一问:如何理解 Android 中的 Context,它有什么用?
    迭代器模式简介
    《心理学报》的《大学生学习适应量表》能用吗?
  • 原文地址:https://blog.csdn.net/wang0907/article/details/132985223