Detailed explanation of popular RPC framework in PHP

Time:2020-7-30

What is the RPC framework?

In a word, RPC is: remote procedure call

So what is a remote call?

My official group click here.

Usually we call a method in PHP, such as a function method: localadd (10, 20). The specific implementation of the local add method is either user-defined or self-contained in the PHP library function. In other words, the code implementation of the localadd method is local. It is a local call! Remote call means that the specific implementation of the called method is not local to the program, but in some other remote place.

Principle of remote call

For example, a (client) calls the remoteadd method provided by B (server)

  1. First, a TCP connection is established between a and B;
  2. Then a serializes the method name to be called (here is remoteadd) and method parameters (10, 20) and sends them out in byte stream;
  3. B accepts the byte stream sent by a, and then deserializes to get the target method name and method parameters, then executes the corresponding method call (possibly local add) and returns the result 30;
  4. A accepts the result of the remote call and outputs 30.

The RPC framework encapsulates the details I mentioned just now to expose the user a simple and friendly API to use.

Benefits of remote calls

Decoupling: when the server needs to modify the implementation within the method, the client can not perceive it completely and does not need to make any changes. This method is often used in cross department and cross company cooperation, and the method provider is commonly referred to as service exposure.

What is the difference between RPC and socket?

Through the above simple explanation, it seems that RPC and socket are like ah. They all call remote methods, and they are all client / server mode. I also wrote an article before: what’s the difference between socket and socket?

RPC (remote procedure call) adopts client / server mode to realize the communication between two processes. Socket is one of the communication means often used by RPC. RPC is implemented on the basis of socket. It needs more network and system resources than socket. In addition to socket, RPC has other communication methods, such as HTTP, the pipeline of the operating system and other technologies to realize the call of remote programs. In Microsoft Windows system, RPC uses named pipe to communicate.

What is the difference between RPC and rest?

After understanding RPC, we know that RPC is in client / server mode and calls remote methods. Rest is also a set of API call protocol methods that we are familiar with. It is also based on client / server mode and calls remote methods. What is the difference between them?

Both rest API and RPC encapsulate functions into interfaces on the server side and expose them for the client to call. However, the rest API is based on the HTTP protocol. Rest is committed to providing an HTTP request through the methods of post / get / put / delete and a readable URL in the HTTP protocol. RPC can not be based on HTTP protocol
Therefore, if the back-end two languages call each other, RPC can achieve better performance (omitting a series of things such as HTTP header), and it should be easier to configure. If the front-end calls the back-end through Ajax, it is better to use the rest API form (because HTTP can’t be avoided in any case).

What are the popular RPC frameworks in PHP

Since PHP is the best language in the world, what are the popular RPC frameworks in PHP?

First of all, PHPRPC, Yar, thrift, grpc, swote, Hprose

Because of the limited time and energy, it is impossible to learn and use one by one. I choose the most used ones in the world. Because the principle of RPC is the same, it is client / server mode, but the usage of each framework is different.

PHPRPC and Yar are the most I have heard and contacted at present.

phprpc

First, download the latest stable version of PHPRPC from the official website.

install

We will find that there are many files and folders in it. The structure is as follows:

  • dhparams/
  • pecl/
  • bigint.php
  • compat.php
  • phprpc_date.php
  • xxtea.php
  • dhparams.php
  • phprpc_server.php
  • phprpc_client.php

 

Dhparams and PECL are the folders. In PECL is the xxtea extension of PHP. According to the description on the official website, you can install or not install it, and you can run without installing PHPRPC. But if you need faster encryption, you can install it.

I’d better install it. After all, faster encryption is a good thing:

The installation steps are as follows: first copy the xxtea folder under PECL to the ETX directory of PHP source code / lamp / php-5.4.11/ext. The extension is recompiled with phpize.

[[email protected] /]# cd /lamp/php-5.4.11/ext/xxtea

[[email protected] xxtea]# /usr/local/php/bin/phpize

[[email protected] xxtea]# ./configure --enable-xxtea=shared --with-php-config=/usr/local/php/bin/php-config

make && make install

OK, the compilation is complete, prompt us xxtea.so It has been found in / usr / local / PHP / lib / PHP / extensions / no-debug-zts-20100525/ xxtea.so I’m off.

Next, we need to php.ini At the end of this xxtea.so add:

[[email protected] /]# vi /usr/local/php/etc/php.ini 

[xxtea]

extension=xxtea.so

After that, we need to restart Apache or PHP FPM

Restart Apache

[[email protected] /]# /usr/local/apache/bin/apachectl restart

Restart PHP FPM smoothly

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

After the restart, open the phpinfo () page and search for it. You should be able to see the xxtea.

Getting started

For a simple example, PHPRPC is also divided into server-side and client-side. So the corresponding folder is PHPRPC_ server.php And PHPRPC_ client.php

Let’s refer to a few examples on the official website

server.php Server: this completes the simplest helloword interface.

add('HelloWorld');

$server->start();

Under operation server.php , I wipe, actually reported a mistake!!!

PHP Strict Standards:  Non-static method PHPRPC_Server::initSession()....

Cannot redeclare gzdecode().....

Google, said PHPRPC first_ server.php Change the initsession() of line 413 to static function

static function initSession() {

   ****

}

I made an erasure, such a big mistake, how did PHPRPC release it!!!

In the compat.php The gzdecode() function on line 71 of php5.4 already implements this function. In this way, the function will be rewritten and an error will be reported. Therefore, a judgment is added:

if (!function_exists('gzdecode')) {

    //Include the gzdecode function

}

OK. Change it and save it. Run again server.php 。 OK. No mistake. Output:

phprpc_functions="YToxOntpOjA7czo5OiJoZWxsb3dvcmQiO30=";

Let’s write the client client.php How is it written?

HelloWorld();

?>

We are implementing the following client.php The output is as follows:

Hello Word!

This is a simple server / client delivery. Although there is a bit of error in the middle, it is still quite simple and easy to understand!

Other more advanced usage can refer to the official website.

yar

Yar is a masterpiece of huixinchen, a famous PHP God in China. It has been used in microblog products. It is also an RPC framework. It is written in pure C for PHP extensions, so the efficiency should be quite high, and support asynchronous parallel, this is still good.

Download and install

Download from official website:http://pecl.php.net/package/yarThe latest version yar-1.2.4.tgz

Then unzip and copy it to the ETX directory of PHP source code, / lamp / php-5.4.11/ext. The extension is recompiled with phpize.

[[email protected] yar-1.2.4]# /usr/local/php/bin/phpize

[[email protected] yar-1.2.4]# ./configure --with-php-config=/usr/local/php/bin/php-config

But there is a problem: there is a problem with curl

configure: error: Please reinstall the libcurl distribution - easy.h should be in /include/curl/

It is estimated that there is a problem with curl on my computer. Please use Yum to install it

yum -y install curl-devel

After the installation of curl, continue to compile and install. There is no problem

[[email protected] yar-1.2.4]# /usr/local/php/bin/phpize

[[email protected] yar-1.2.4]# ./configure --with-php-config=/usr/local/php/bin/php-config

[[email protected] yar-1.2.4]# make && make install

After success, remind us yar.so The extension is already under / usr / local / PHP / lib / PHP / extensions / no-debug-zts-20100525 /.

Let’s edit it php.ini , add at the end yar.so Extension, and then restart Apache or PHP PFM.

[[email protected] /]# vi /usr/local/php/etc/php.ini 

[yar]

extension=yar.so

OK. After that, we need to restart Apache or PHP FPM

Restart Apache

[[email protected] /]# /usr/local/apache/bin/apachectl restart

Restart PHP FPM smoothly

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

After the restart, open the phpinfo () page and search it. You should be able to see Yar.

Getting started

Like other RPC frameworks, Yar is also a server / client mode. Therefore, we will start to write a simple example of how to call it.

yar_ server.php Represents the server side

handle();

OK, let’s run it in the browser and the output will be as shown in the figure below. Very high end!!! Bird said that the purpose of this is to know at a glance how many interfaces my RPC provides, so that the API documents can be omitted.

OK, let’s start writing Yar_ client.php This is the client:

$client = new Yar_Client("http://127.0.0.1/yar_server.php");

echo $client->api('helo word');

For example, other spools and Hprose are basically based on this principle. It just depends on who has more functions and is more convenient to use.

The above is the details of the popular RPC framework in PHP

The above contents hope to help you. Many PHPer will encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code, and I don’t know where to start to improve. For this, I have sorted out some materials, including but not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, tp6, laravel, yii2, redis, and Swoo Le, swoft, Kafka, MySQL optimization, shell script, docker, microservice, nginx and other knowledge points. Advanced dry goods that you need can be shared with you free of charge. If you need, you can join my official group. Click here.