An explanation of the popular RPC framework in PHP

Source: Internet
Author: User
Tags php server php source code phpinfo zts
What is the RPC framework? If you summarize RPC in a nutshell: Remote Call Framework (Procedure call) What is remote invocation? Usually we call a method in PHP, such as a function method: Localadd (Ten, a), the implementation of the Localadd method is either the user's own definition, or PHP library functions in the self-contained, it is said in the Localadd method of code implementation locally, It is a local call! The remote invocation means that the specific implementation of the called method is not local to the program, but to some other remote location.

Remote invocation principle

For example, a (client) invokes the Remoteadd method provided by B (server):

    1. First, a TCP connection is established between a and B;

    2. Then a will call the method name (here is Remoteadd) as well as the method parameters (10, 20) serialized into a stream of bytes sent out;

    3. B accepts a stream of bytes sent over a, then deserializes the target method name, method parameters, then executes the corresponding method call (possibly Localadd) and returns the result 30;

    4. A accepts the remote call result, output 30.

The RPC framework is to encapsulate the details that I just said, to expose users to simple and friendly API usage.

Benefits of remote Invocation

Decoupling: When the server needs to implement modifications within a method, the client is completely unaware and does not have to make any changes, which are often used in cross-sectoral, cross-company collaboration, and the provider of the method we commonly call: Service exposure.

What is the difference between RPC and socket?

Through the above simple elaboration, like RPC and socket like AH. are called remote methods, are Client/server mode, I have also written an article: What is the difference between the sockets?

RPC (Remote procedure Call) uses client/server mode to communicate with each other between two processes. Socket is one of the most common means of communication RPC, RPC is implemented on the basis of socket, it needs more network and system resources than socket. In addition to SOCKET,RPC there are other communication methods, such as HTTP, the operating system comes with the pipeline and other technologies to implement the remote program calls. In Microsoft's Windows system, RPC is using named pipes for communication.

What is the difference between RPC and rest?

By understanding RPC, we know that RPC is Client/server mode, call the remote method, rest is also familiar with a set of API call protocol method, it is based on the Client/server mode, call the remote method, then what difference do they have?

Both the rest API and RPC are exposed on the server side to encapsulate a function as an interface for client invocation, but the rest API is based on the HTTP protocol, and Rest is committed to using methods such as Post/get/put/delete in the HTTP protocol and a A highly readable URL to provide an HTTP request. RPC may not be based on the HTTP protocol
Therefore, if the backend two languages call each other, RPC can get better performance (eliminating a series of things such as the HTTP header), it should be easier to configure. If the frontend is called by the front end through AJAX, then it is better to use the REST API (because HTTP is not a good way to avoid it anyway).

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 listed below: Phprpc,yar, Thrift, Grpc, Swoole, Hprose

Because time and energy is limited, it is impossible to study and use one by one, I choose a few of the most used in the world. Because the RPC principle is the same, are client/server mode, but each framework is used differently.

The main explanation is that Phprpc and Yar are the most I have heard and contacted at the moment.

Phprpc

Download the latest stable version of PHPRPC from the official website: Download link decompression.

Installation

We will find a lot of files and folders inside, the structure is as follows:

  • dhparams/

  • pecl/

  • bigint.php

  • compat.php

  • phprpc_date.php

  • xxtea.php

  • dhparams.php

  • phprpc_server.php

  • phprpc_client.php

There are dhparams and Pecl is a folder, PECL is the xxtea extension of PHP, according to the description of the official website, can be installed or not installed, do not install PHPRPC can also be run. But if you need faster encryption processing power, you can install it.

I'll just install it. After all, the ability to encrypt faster is good:

Install the steps below, first copy the Xxtea folder under Pecl to the ETX directory of the PHP source code:/lamp/php-5.4.11/ext. The extension is then recompiled with Phpize.

[Root@localhost/]# cd/lamp/php-5.4.11/ext/xxtea[root@localhost xxtea]#/usr/local/php/bin/phpize[root@localhost xxtea]#./configure--enable-xxtea=shared--with-php-config=/usr/local/php/bin/php-configmake && make Install

OK, compile complete, prompt us xxtea.so already under/usr/local/php/lib/php/extensions/no-debug-zts-20100525/xxtea.so.

Below, we need to add this xxtea.so at the end of the php.ini:

[Root@localhost/]# Vi/usr/local/php/etc/php.ini [xxtea]extension=xxtea.so

Good. After the addition, we need to restart Apache or PHP-FPM.

Restart Apache[root@localhost/]#/usr/local/apache/bin/apachectl Restart smooth restart PHP-FPMKILL-USR2 ' Cat/usr/local/php/var/run /php-fpm.pid '

After the restart is complete, open the Phpinfo () page, search, you should be able to see the Xxtea.

Start using

First, a simple example, PHPRPC is also divided into server-side and client. So the folders correspond to phprpc_server.php and phprpc_client.php.

We refer to the official website for a few examples, practice:

server.php server: This completes the simplest Helloword interface.

<?phpinclude ("phprpc/phprpc_server.php"); function HelloWorld () {   return ' Hello world! ';} $server = new Phprpc_server (); $server->add (' HelloWorld '); $server->start ();

Run under server.php, I rub, incredibly error!!!

PHP Strict Standards:  non-static method Phprpc_server::initsession () .... Cannot redeclare gzdecode () .....

Google, said to be phprpc_server.php 413 lines of initsession () to the static function

static function Initsession () {    * *}

PS. I have a wipe, such a big mistake, how PHPRPC is released!!!

In the 71st line of the compat.php Gzdecode () function, php5.4 has implemented this function. This function is rewritten, the error, so add a judgment:

if (!function_exists (' Gzdecode ')) {    //include the Gzdecode function in}

Good. Change, save. Then run the next server.php. It's OK. Not an error. Output:

Phprpc_functions= "ytoxontpoja7czo5oijozwxsb3dvcmqio30=";

We then write the client client.php, see how it is written?

<?phpinclude ("phprpc/phprpc_client.php"); $client = new Phprpc_client (' http://127.0.0.1/server.php '); echo $ Client->helloworld ();? >

We are performing the following client.php, the output of the wish:

Hello word!

The delivery of such a simple server/clent is done. Although there are some mistakes in the middle, but the overall is quite simple and understandable!

Other more advanced usage can refer to the official website.

Yar

Yar is home to the famous PHP big God Bird Xinchen's masterpiece, has already begun to use in the microblogging product. It is also an RPC framework. It is good because it uses a pure C for PHP extension, so the efficiency should be quite high, and support asynchronous parallel, this is still praise.

Download installation

Official website Download: Http://pecl.php.net/package/yar Latest Version Yar-1.2.4.tgz

Then extract the copy to the PHP source ETX directory:/lamp/php-5.4.11/ext. The extension is then recompiled with Phpize.

[Root@localhost yar-1.2.4]#/usr/local/php/bin/phpize[root@localhost yar-1.2.4]#./configure--with-php-config=/usr /local/php/bin/php-config

But there's something wrong: tip, Curl has a problem:

Configure:error:Please Reinstall the Libcurl distribution-easy.h should be in <curl-dir>/include/curl/

I guess I have a problem with this machine curl, then install it with Yum:

Yum-y Install Curl-devel

After the installation has finished curl continue to compile the installation, there is no problem:

[Root@localhost yar-1.2.4]#/usr/local/php/bin/phpize[root@localhost yar-1.2.4]#./configure--with-php-config=/usr /local/php/bin/php-config[root@localhost yar-1.2.4]# make && make install

After success, we are prompted to yar.so extension in the/usr/local/php/lib/php/extensions/no-debug-zts-20100525/already under.

Let's edit the php.ini, add the yar.so extension, and then reboot Apache or PHP-PFM.

[Root@localhost/]# Vi/usr/local/php/etc/php.ini [yar]extension=yar.so

Good. After the addition, we need to restart Apache or PHP-FPM.

Restart Apache[root@localhost/]#/usr/local/apache/bin/apachectl Restart smooth restart PHP-FPMKILL-USR2 ' Cat/usr/local/php/var/run /php-fpm.pid '

After the restart is complete, open the Phpinfo () page, search, you should be able to see the Yar.

Start using

Like the other RPC frameworks, Yar is also server/client mode, so we also start writing a simple example of how to invoke it.

Yar_server.php indicates that the server-side

<?phpclass API {Public   function API ($parameter, $option = "foo") {       return $parameter;   }   protected function Client_can_not_see () {   }} $service = new Yar_server (new API ()); $service->handle ();

Well, we'll run it in the browser and we'll see the output as shown. Very high-end AH!!! Brother Bird said that the use of this can be at a glance to know how much I RPC provides the interface, the API documentation 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 ');

Good, like other swoole,hprose and so on basic is this principle, just see who's function more, use up more handy just.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.