Make writing a habit together! This is the second day of my participation in the “Gold Digging Day New Plan · April More text challenge”. Click here for more details.

preface

Generally, the program development mode is the separation of the front and back ends and the microservice mode. The front-end (such as NodeJS) processes the data and then transfers it to the back end. In the microservice mode, multiple modules need to interact with each other. There are two main ways of data interaction, one is HTTP REST and the other is RPC. Thrift is a form of RPC communication.

  • REST is data interaction based on HTTP. Data is usually in JSON format
  • Thrift implements data exchange based on sockets. Data can be transmitted in binary mode, serialization and deserialization, or in JSON format

The installation

This document is installed on Ubuntu20.04. For other operating systems, see the thrift.apache.org/

Install dependencies
sudo apt-get install automake bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg-config

# This article uses CPP as the server and Python as the client, so you need to install the corresponding package
sudo apt install g++
sudo apt install python3
sudo apt install python-all python-all-dev python-all-dbg

# download thriftWget HTTP: / / https://dlcdn.apache.org/thrift/0.16.0/thrift-0.16.0.tar.gz# decompressionThe tar - xf thrift - 0.16.0. Tar. GzEnter the directory and wait
cdThrift 0.16.0/./configure sudo make sudo make installVerify that the installation is successful
thrift -version
Copy the code

An introduction to

  • Write test interface, similar to CPP struct, file nametest.thrift, defines User and two methods:
namespace cpp match_service

struct User {
  1: i32 id,
  2: string name,
  3: i32 score
}

service Match {
  i32 add_user(1: User user, 2: string info),
  i32 remove_user(1: User user, 2: string info),}Copy the code
  • CPP code is then generated through the interface:
# CPP can be changed to other programming languages
thrift -r --gen cpp test.thrift

The generated code is in the gen-cpp directory \
Copy the code
  • Modified to start the server
cd gen-cpp

# Change the file name to main
mv Match_server.skeleton.cpp main.cpp

Add an output to the main function for testing
vim main.cpp

# Compile, connect, run
g++ -c *.cpp
g++ *.o -o main -lthrift
./main
Copy the code

  • Tomorrow based on this server update a match system for the actual combat 😎

An error to solve

CPP could not find dynamic link library:

sudo vim /etc/ld.so.conf
Add /usr/local/lib at the end of the line

Make the change take effect
sudo /sbin/ldconfig
Copy the code

Python could not find the thrift module:

sudo apt install python3-pip
sudo pip install thrift
Copy the code