当前位置:网站首页>Thrift getting started

Thrift getting started

2022-06-26 00:07:00 Keyboard singer

One 、 brief introduction

Thrift By the first FaceBook Research and development , It is mainly used for communication between various services RPC signal communication , Support for cross language , Common languages such as C++,Java,Python,PHP,Ruby,Erlang,Perl,Haskell,C# wait .
Thrift It's a typical CS( client / Server side ) structure , Client and server can be developed in different languages , Then there must be an intermediate language to associate the languages of the client and the server , This language is IDL(Interface Description Language)

Two 、 Front end environment configuration

I am using mac System , Need to be installed on your computer homebrew, In use homebrew To install git and thrift.git Your environment is installation thrift The basis of !
1.homebrew Installation :

xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"

2.git Installation

brew install git

3.thrift Installation :

brew install thrift

4. The identification of successful installation is as follows :
 Please add a picture description
as for windows Installation , Details please see Official website

3、 ... and 、Thrift type

1. data type

Thrift Unsigned types are not supported

byte: Signed byte
i16:16 Bit signed integer
i32:32 Bit signed integer
i64:64 Bit signed integer
double:64 Bit floating point
string: character string

2. Container type

Elements in a collection can be anything other than service Any type other than , Include exception

list: A series of T An ordered list of types of data , Elements can be repeated
set: A series of T An unordered list of types of data , Element is not repeatable
map: A dictionary structure ,key by K type ,value by V type , amount to Java Medium HashMap

Four 、Thrift working principle

1、 How to realize the communication between multiple languages ?
Data transmission uses socket( Many languages support ), Data in a specific format (String etc. ) send out , The language of the receiver shall be parsed

2、 Definition thrift The file of , from thrift file (IDL) Generate interfaces for both languages 、model, In the generated model And there will be decoding code in the interface .

5、 ... and 、Demo Small cases

1. introduce maven rely on

		<dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.14.2</version>
        </dependency>

2. To write thrift file

namespace java thrift.generated

typedef i16 short
typedef i32 int
typedef i64 long
typedef bool boolean
typedef string String

struct Person {
    1: optional String username,
    2: optional int age,
    3: optional boolean married
}

exception DataException {
    1: optional String message,
    2: optional String callStack,
    3: optional String date
}

service PersonService {
    Person getPersonByUsername(1: required String username) throws (1: DataException dataException),

    void savePerson(1: required Person person) throws (1: DataException dataException)
}

3. Generate for by command gen-java file

 Please add a picture description  Please add a picture description
If the class file is red ! Please add package sentence :
 Insert picture description here
4. To write Server End code

package com.maoyan.luzelong.thrift.test;

import com.maoyan.luzelong.thrift.generated.PersonService;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.layered.TFramedTransport;

public class ThriftServer {
    
    public static void main(String[] args) throws Exception{
    
        TNonblockingServerSocket socket = new TNonblockingServerSocket(8899);
        THsHaServer.Args arg = new THsHaServer.Args(socket).minWorkerThreads(2).maxWorkerThreads(4);
        PersonService.Processor<PersonServiceImpl> processor = new PersonService.Processor<>(new PersonServiceImpl());

        arg.protocolFactory(new TCompactProtocol.Factory());
        arg.transportFactory(new TFramedTransport.Factory());
        arg.processorFactory(new TProcessorFactory(processor));

        TServer server = new THsHaServer(arg);

        System.out.println("Thrift Server start");
        server.serve();
    }
}

5. To write Client End

package com.maoyan.luzelong.thrift.test;

import com.maoyan.luzelong.thrift.generated.Person;
import com.maoyan.luzelong.thrift.generated.PersonService;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFramedTransport;

public class ThriftClient {
    
    public static void main(String[] args) throws TTransportException {
    
        TTransport transport = new TFramedTransport(new TSocket("localhost",8899),600);
        TProtocol protocol = new TCompactProtocol(transport);
        PersonService.Client client = new PersonService.Client(protocol);
        try{
    
            transport.open();
            Person person = client.getPersonByUsername(" Zhang San ");
            System.out.println(person.getUsername());
            System.out.println(person.getAge());
            System.out.println(person.isMarried());

            System.out.println("---------");

            Person person2 = new Person();
            person2.setUsername(" Li Si ");
            person2.setAge(30);
            person2.setMarried(true);
            client.savePerson(person2);
        }catch (Exception e){
    
            throw new RuntimeException(e.getMessage(),e);
        }finally {
    
            transport.close();
        }
    }
}

6. test ( First run Server, Run again Client)
 Please add a picture description
 Please add a picture description

原网站

版权声明
本文为[Keyboard singer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252115219000.html