introduction

The Apache Thrift software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.

Thrift is a lightweight, cross-language framework for remote service invocation, originally developed by Facebook and later made its way into the Apache open source project. It generates RPC server/client template code for various major languages through its own IDL intermediate language and with the help of a code generation engine. Thrift supports many different programming languages, including C++, Java, Python, PHP, and Ruby. This series focuses on how to configure and use Thrift based on the Java language.Copy the code

quick start

Write the IDL file of Hello. thrift

service HelloWorldService {
  string say(1: string username)
}
Copy the code

Using a code generation tool to generate code, execute the following command

thrift -gen java hello.thrift
Copy the code

Since the target directory for code generation is not specified, the generated class file is stored in the gen-java directory by default to generate a helloWorldService.java class file

The Thrift framework only needs to focus on the four core internal interfaces/classes: Iface, AsyncIface, Client and AsyncClient.

  • Iface: The server provides the specific synchronization business logic to the client by implementing the HelloWorldService.Iface interface.

  • AsyncIface: The server provides specific asynchronous business logic to the client by implementing the HelloWorldService.Iface interface.

  • Client: The Client synchronously accesses the service method provided by the server through the instance object of HelloWorldService.Client.

  • AsyncClient: the client through the HelloWorldService AsyncClient instance objects, access to the server in the form of asynchronous service method.

public class HelloWorldService {
    public interface Iface {
        public String say(String username) throws org.apache.thrift.TException;
    }

    public interface AsyncIface {
        public void say(String username, org.apache.thrift.async.AsyncMethodCallback<String> resultHandler) throws org.apache.thrift.TException;
    }

    public static class Client extends org.apache.thrift.TServiceClient implements Iface {
        public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {
            public Factory(a) {}public Client getClient(org.apache.thrift.protocol.TProtocol prot) {
                return new Client(prot);
            }

            public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
                return newClient(iprot, oprot); }}public Client(org.apache.thrift.protocol.TProtocol prot) {
            super(prot, prot);
        }

        public Client(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
            super(iprot, oprot);
        }

        public String say(String username) throws org.apache.thrift.TException {
            send_say(username);
            return recv_say();
        }
        / / to omit...
    }

    public static class AsyncClient extends org.apache.thrift.async.TAsyncClient implements AsyncIface {
        public static class Factory implements org.apache.thrift.async.TAsyncClientFactory<AsyncClient> {
            private org.apache.thrift.async.TAsyncClientManager clientManager;
            private org.apache.thrift.protocol.TProtocolFactory protocolFactory;

            public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) {
                this.clientManager = clientManager;
                this.protocolFactory = protocolFactory;
            }

            public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) {
                return newAsyncClient(protocolFactory, clientManager, transport); }}public AsyncClient(org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.transport.TNonblockingTransport transport) {
            super(protocolFactory, clientManager, transport);
        }

        public void say(String username, org.apache.thrift.async.AsyncMethodCallback<String> resultHandler) throws org.apache.thrift.TException {
            checkReady();
            say_call method_call = new say_call(username, resultHandler, this, ___protocolFactory, ___transport);
            this.___currentMethod = method_call;
            ___manager.call(method_call);
        }
        / / to omit...
    }
    / / to omit...
}

Copy the code

theory

  1. transport

Encapsulates the Socket communication API

The client is TTransport and the server is TServerTransport

Implement categories: TSocket/TServerSocket: for transmission, using block type I/O is the most common mode TFramedTransport/TNonblockingServerTransport: Transfer by block size in a non-blocking manner, similar to NIO in Java

  1. protocol

Specify the message delivery format

The client is TProtocol, which holds the Transport object and encapsulates socket flows in the format specified for message delivery. The Server is TProtocolFactory, which specifies the protocol type

  1. Iface/processor/Client

Define RPC interfaces and interface implementations

Client The client implements the Iface interface and holds the Protocol object for sending and receiving messages. Since the client is automatically generated by thrift, you can directly use the Server processor to implement the Iface interface to encapsulate the general message processing logic. The message is read according to the protocol specified by the user, and the Iface interface method implemented is called back to return the reply in the specified message format

  1. server

The Thrift service model can be unified by holding transport and Processor objects and specifying a protocolFactory

Server only: TSimpleServer: single-threaded server that uses standard blocking I/O TThreadPoolServer: multi-threaded server that uses standard blocking I/O

TNonblockingServer: single-threaded server side, using non-blocking I/O THsHaServer: semi-synchronous and semi-asynchronous server side, based on non-blocking I/O read and write and multi-threaded work task processing TThreadedSelectorServer: Multithreaded selector server side, enhancement of THsHaServer on asynchronous IO model

document

Official documentation Apache Thrift series of tutorial blogs