Detailed explanation of thrift

background

Thrift was first developed by facebook and later contributed to apache. thrift mainly solves the problem of cross-language calls.

What is the difference between thrift and protobuf?

Both thrift and protobuf solve a problem that is the definition of language data structures, and json also solves the definition of cross-language data structures, but json is all strings, and the overhead of transmission and parsing may be a bit large. So what if you want to transmit binary data? You can define the data structure yourself. For example, the well-known network protocol is determined in this way. The first byte represents what, the second byte represents what, and then the client Both the server and the server abide by this protocol. A lot of cs programs did this before thrift or profobuf. But it is very tiring to do this, and you have to write documents, and it is difficult to remember and communicate during development. Later, people came up with a way to define the data structure with a file similar to json format, and then automatically generate code to handle the binary conversion, so that people can define the data structure in a readable format and use it efficiently, so Then things like protobuf and thrift were born. This is the end of protobuf. Thrift also adds the server program and client program to automatically generate it, so that the programmer is only responsible for writing business logic, and other transmissions are automatically generated. This is protobuf The difference with thrift. In this way, thrift has a full set of RPC mechanisms,

Why call across languages?

Some people think it is the team needs, different people are good at different languages, I think from the perspective of architecture, there are 2 advantages

  1. Use a suitable language to do suitable things, such as some complex algorithms, C language is more suitable, and less redundant things with high performance are done. PHP may have much slower performance. At this time, you can consider cross-language calls.
  2. For the decoupling between teams of large-scale projects, in a large company, different functions may not be completed by a group, but may be cross-group or even cross-department. At this time, providing a function to other departments in the form of a service is a kind of better way.

Why isn't it more convenient to interact directly with HTTP json?

Yes, you can use json. Many APIs are in the form of json. REST is also popular now, which is actually quite convenient, but there are several disadvantages.

  1. No matter what language you use, you must have an http service. Java and php are fine, but c is a bit laborious.
  2. JSON is very flexible and easy to change, but it needs to have good documentation. It is very easy to use by yourself. Large teams may have some communication problems. At least the document should clearly write the meaning of each field of JSON and what types of values ​​may be, otherwise you can give A url said that this is the api of xxx, and it is basically impossible to connect if there is no documentation.
  3. For authentication and signature verification, the identity of the caller can be ignored for the get request of the website before, because it is for others to see, and whoever sees it differently. But when it is used as an api, especially when it involves additions, deletions, and changes, the identity of the caller has to be considered, and not everyone can adjust it, so generally http apis need to design a signature rule, and the docking person must follow your verification. Sign the rules to write the program, which is usually a time-consuming and problem-prone place for docking. 1. From the size of the transmission, it is not as good as binary space saving, for

Install

The installation of thrift is divided into two parts

  1. is the thrift itself
  2. is a language pack

Install the thrift compiler

In fact, there is no need to compile and install this, just download the Windows version directly. Compiling and installing on linux is also relatively easy to install, but there may be a lack of this and that, and you can use yum to install it.

./configure
make
make install

install language lib

The language packs are all in the lib directory of the source code, so download the linux version only, the windows version is just a compiled thrift compiler

such as java

cd thrift源码/lib/java
ant
会生成libthrift-0.9.3.jar

Another example is python

cd thrift源码/lib/py
python setup.py install

Steps for usage:

  1. Preparation: thrift compiler, lib for server and client languages
  2. write thrift file
service HelloService{
    string sayHello(1:string username)
}

Then compile with thrift:

  • thrift.exe -r -gen java hello.thrift
  • thrift.exe -r -gen php hello.thrift
  • thrift.exe -r -gen py hello.thrift generates whatever language you want
  1. write implementation
import org.apache.thrift.TException;

    public class helloImp implements HelloService.Iface {

        @Override
        public String sayHello(String username) throws TException {
            System.out.println(username);
            return username;
        }

    }

Here this HelloService is the java file generated by thrift 3. Write the server

    import org.apache.thrift.TProcessor;
    import org.apache.thrift.protocol.TBinaryProtocol;
    import org.apache.thrift.server.TServer;
    import org.apache.thrift.server.TSimpleServer;
    import org.apache.thrift.transport.TServerSocket;
    import org.apache.thrift.transport.TTransportException;

    public class Server  {
        
        public void start()
        {
            TProcessor tpProcessor = new HelloService.Processor<HelloService.Iface>(new helloImp());
            TServerSocket serverTransport;
            try {
                serverTransport = new TServerSocket(8889);
                TServer.Args args = new TServer.Args(serverTransport);
                args.processor(tpProcessor);
                args.protocolFactory(new TBinaryProtocol.Factory());
                TServer server = new TSimpleServer(args);
                System.out.println("thrift server start");
                server.serve();
                
            } catch (TTransportException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         
        }
        
        public static void main(String[] args) {
            Server server = new Server();
            server.start();
        }

    }

4. Write the client to python

import sys
import glob

from hello import HelloService

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

# Make socket
transport = TSocket.TSocket('localhost', 8889)

# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)

# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)

# Create a client to use the protocol encoder
client = HelloService.Client(protocol)

# Connect!
transport.open()


rs = client.sayHello('luyu')

print rs

How to write in other languages? ? ? ?

There are examples in various languages ​​in the tutorial directory of the source code

Continuous update to be continued...

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324080842&siteId=291194637