Thrift框架简介

功能:实现各个服务模块之间的跨语言、跨平台的通信,是RPC框架的一种,与dubbo类似。

Thrift的应用原理:

  Thrift的部分功能相当于代码生成引擎,使用Thrift定义的语言编写*.Thrift文件,文件中主要定义了数据结构和数据接口!使用Thrift命令生成对应语言的代码,然后用对应的语言实现定义的数据结构和接口的对象。

  Thrift的另一部分功能是基于Thrift封装的库实现客户端与服务端之间的通信!

Thrift的安装:

  Thrift支持多种语言,建议使用哪种语言配置、编译、安装哪种语言的功能。否则可能安装失败!

Thrift的使用:

  工程下面有test文件夹,里面有各种语言的demon,以java为例,说明;

  使用:

thrift --gen java JavaBeansTest.thrift 

  生成java文件,生成的文件在 /gen-java目录下!

  一共生成两个文件,其中一个对应的是结构体,另一个对应的是接口!

  可以看到生成的两个文件代码量很大,暂时不必因此感觉艰难,继续看下去!

  将这两个文件拷贝到自己项目中,并引入thrift-0.5.0-fix-backlog.jar, slf4j-api 和 slf4j-simple 这三个JAR包!

  然后将生成的文件加入到自己的项目中,并编写接口中的实现代码!

  好了,下面看一个简单的demo:

  服务端:

package Test;


import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;


public class Server {

    private void start() {

        try {

            TServerSocket serverTransport = new TServerSocket(7911);
            Something.Processor processor = new Something.Processor(new SomethingImpl());
            Factory protFactory = new TBinaryProtocol.Factory(true, true);
            TServer server = new TThreadPoolServer(processor, serverTransport, protFactory);
            System.out.println("Starting server on port 7911 ...");
            server.serve();

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

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


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

    }


} 

 

  客户端:

  

package Test;


import org.apache.thrift.TException;
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 Client {


    public static void main(String[] args) {


        try {
            TTransport transport = new TSocket("localhost", 7911);
            TProtocol protocol = new TBinaryProtocol(transport);
            Something.Client client = new Something.Client(protocol);
            transport.open();
            System.out.println("Client calls ping()");
            client.ping();
            transport.close();

        } catch (TException x) {


            x.printStackTrace();


        }


    }


}

  整个项目结构:

  

  其中main 和 sql 不需要,可以删去!Something是由Thrift生成的代码,

namespace java Test
service Something{
    i32 ping()
}
test.thrift

SomethingImpl是接口的实现:

  

package Test;

import org.apache.thrift.TException;

class SomethingImpl implements Something.Iface {
    public SomethingImpl() {
    }

    /**
     * 方法的实现
     *
     * @return
     * @throws TException
     */
    public int ping() throws TException {
        System.out.println("Recieve ping from client...");
        return 0;
    }
}

   在这个demo中,server和client在一个项目下,实际上可以部署到多个项目,或者是不同机器上,代码稍作改动即可!

   

原文地址:https://www.cnblogs.com/tengpan-cn/p/6026750.html