zoukankan      html  css  js  c++  java
  • Thrift入门

    从官网介绍看应该是个RPC框架,通过thrift定义接口,根据thrift文件生成各种语言的代码,c++, python, java....这里工作主要用到java。从晚上抄了个乘法的例子

    1. pom依赖

    <dependency>
                <groupId>org.apache.thrift</groupId>
                <artifactId>libthrift</artifactId>
                <version>0.10.0</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.0.13</version>
            </dependency>

    这里加了个logback日志,方便查看框架日志输出

    2. 定义接口

    multiple.thrift

    namespace java tutorial
    namespace py tutorial
    
    /*
     C like comments are supported
    */
    // This is also a valid comment
    
    typedef i32 int // We can use typedef to get pretty names for the types we are using
    service MultiplicationService
    {
            int multiply(1:int n1, 2:int n2),
    }

    这里定义了一个乘法接口,接收两个整形参数,返回他们的乘积

    3. 使用thrift编译器生成java代码

    thrift --gen java multiple.thrift

    4. 服务端代码

    MultiplicationHandler.java
    /**
     * Created by GuanXF on 2017/10/8.
     */
    public class MultiplicationHandler implements MultiplicationService.Iface {
        public int multiply(int n1, int n2) throws TException {
            System.out.println("n1 = " + n1 + ", n2 = " + n2);
            return  n1 * n2;
        }
    }
    MultiplicationServer.java
    /**
     * Created by GuanXF on 2017/10/8.
     */
    public class MultiplicationServer {
        public static MultiplicationHandler handler;
        public static MultiplicationService.Processor processor;
    
        public static void main(String[] args) {
            handler = new MultiplicationHandler();
            processor = new MultiplicationService.Processor(handler);
    
            final Runnable simple = new Runnable() {
                public void run() {
                    simple(processor);
                }
            };
            new Thread(simple).start();
        } //main
    
        public static void simple(MultiplicationService.Processor processor){
            try{
                TServerTransport serverTransport = new TServerSocket(9090);
                TServer server = new TSimpleServer(new TServer.Args(serverTransport).processor(processor));
    
                System.out.println("Starting the simple server...");
                server.serve();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    这里,服务端,应该是使用socket监听9090端口。

    5.客户端代码

    MultiplicationClient.java
    /**
     * Created by GuanXF on 2017/10/8.
     */
    public class MultiplicationClient {
        public static void main(String[] args) {
            try{
                TTransport transport;
                transport = new TSocket("localhost", 9090);
                transport.open();
                TProtocol protocol = new TBinaryProtocol(transport);
                MultiplicationService.Client client = new MultiplicationService.Client(protocol);
                perform(client);
                transport.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        } //main
    
        private static void perform(MultiplicationService.Client client) throws TException {
            int product = client.multiply(3, 5);
            System.out.println("3 * 5 = " + product);
        }
    }
  • 相关阅读:
    C#中 栈,堆你真的懂吗?不理解引用类型和值类型区别的程序员将会给代码引入诡异的bug和性能问题
    c# 可空类型,语法糖,lambda,命名规则(Pascal 帕斯卡命名,Camel 驼峰命名),注释,封装,继承,多态
    数据库事务,游标,触发器,存储过程,索引,数字,日期转换为字符,字符串操作,查询,分类,内连接,外连接,全连接,模糊查询,范围查询,5种聚合函数,分组查询,主键,外键,标识列
    html,css 知识汇总
    html,css,js,jquery
    数据库文件托管
    final,finally,finalize的区别
    Thread,Threadpool,task的区别
    ABP 一个开源的web开发框架
    redis 40问
  • 原文地址:https://www.cnblogs.com/luckygxf/p/7636961.html
Copyright © 2011-2022 走看看