Detailed explanation of popular php rpc framework

藏色散人
Release: 2023-04-08 09:58:01
forward
3024 people have browsed it

Detailed explanation of popular php rpc framework

What is the RPC framework?

If RPC can be summarized in one sentence: Remote Call Framework (Remote Procedure Call)

Then what is remote call?

Usually we call a method in php, such as a function method like this: localAdd(10, 20). The specific implementation of the localAdd method is either defined by the user, or it is a function in the php library. With, that is to say, the code in the localAdd method is implemented locally, it is a local call! Remote calling means: the specific implementation of the called method is not where the program is running, but in some other remote place.

Remote calling principle

For example, A (client) calls the remoteAdd method provided by B (server):

First, establish a connection between A and B TCP connection;

Then A serializes the method name that needs to be called (remoteAdd here) and method parameters (10, 20) into a byte stream and sends it out;

B accepts what A sends The byte stream is then deserialized to obtain the target method name and method parameters, and then the corresponding method call (possibly localAdd) is executed and the result 30 is returned;

A accepts the remote call result and outputs 30.

The RPC framework encapsulates the details I just mentioned and exposes users to simple and friendly API usage.

Benefits of remote calling

Decoupling: When the server needs to modify the method, the client is completely unaware of it and does not need to make any changes; this method It is often used when collaborating across departments and companies, and the provider of the method is usually called: service exposure.

What is the difference between RPC and Socket?

Through the above simple explanation, it seems that RPC and Socket are similar. They all call remote methods and are all in client/server mode. I also wrote an article before: Let’s talk about sockets in detail. What’s the difference between them?

RPC (remote procedure call) uses client/server Mode enables two processes to communicate with each other. Socket is one of the communication methods often used by RPC. RPC is implemented on the basis of Socket, which requires more network and system resources than Socket. In addition to Socket, RPC also has other communication methods, such as http, the operating system's own pipeline and other technologies to implement calls to remote programs. In Microsoft's Windows system, RPC uses named pipes for communication.

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 calling protocol methods that we are familiar with. It is also based on client/server mode. , calls the remote method, so what is the difference between them?

REST API and RPC both encapsulate functions into interfaces on the server side and expose them for client calls. However, the REST API is based on the HTTP protocol. REST is committed to passing POST/ in the http protocol. GET/PUT/DELETE and other methods and a human-readable URL to provide an http request.

And RPC does not need to be based on the HTTP protocol

Therefore, if the two back-end languages ​​call each other, using RPC can get better performance (eliminating a series of things such as HTTP headers) ), should also be easier to configure.

If the front end calls the back end through AJAX, then it is better to use the REST API (because the HTTP hurdle cannot be avoided 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?

Let’s list first: phprpc, yar, thrift, gRPC, swoole, hprose

Because time and energy are limited, it is impossible to learn and use them one by one. I will choose a few that are most commonly used in the world. Because the principle of RPC is the same, both are Client/Server mode, but the usage of each framework is different.

Mainly explain phprpc and yar, which I have heard about and come into contact with the most so far.

phprpc

First download the latest stable version of phprpc from the official website: download link and unzip.

Installation

We will find that there are many 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
Copy after login

There are dhparams and pecl is a folder, and what is in pecl is the xxtea extension of php. According to the description on the official website, it can be installed or not. It can also run without installing phprpc. But if you need faster encryption processing capabilities, 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 the php source code: /lamp/php-5.4.11/ext. Then use phpize to recompile with extensions.

[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-config
make && make install
Copy after login

OK, the compilation is completed, prompting us that xxtea.so has been downloaded in /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xxtea.so.

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

[root@localhost /]# vi /usr/local/php/etc/php.ini 
[xxtea]
extension=xxtea.so
Copy after login

After adding it, we need to restart apache or php-fpm

Restart apache

[root@localhost /]# /usr/local/apache/bin/apachectl restart
Copy after login
Copy after login

Smoothly restart php-fpm

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

重启完毕后,打开phpinfo()页面,搜索一下,应该就能够看到xxtea了。

开始使用

先来个简单的例子,phprpc也是分为服务器端和客户端的。所以文件夹中对应的就是phprpc_server.phpphprpc_client.php

我们参考官网的几个例子,练习下:

server.php 服务端:这样写就完成了一个最简单的helloword的接口。

add('HelloWorld');
$server->start();
Copy after login

运行下server.php,我擦,居然报错了!!!

PHP Strict Standards:  Non-static method PHPRPC_Server::initSession()....
Cannot redeclare gzdecode().....
Copy after login

google了下,说是先把 phprpc_server.php的413行的initSession()改成static function

static function initSession() {
   ****
}
Copy after login

我了个擦,这么大的错误,phprpc是怎么发布的!!!

在把compat.php 的第 71行的 gzdecode()函数,php5.4已经实现了这个函数了。这样函数就被重写了,就报错了,所以加个判断:

if (!function_exists('gzdecode')) {
    //将gzdecode函数包括进来
}
Copy after login

好。改完,保存。再运行下server.php 。ok 了。不报错了。输出:

phprpc_functions="YToxOntpOjA7czo5OiJoZWxsb3dvcmQiO30=";
Copy after login

我们接下来写客户端 client.php, 看是如何写的?

HelloWorld();
?>
Copy after login

我们在执行以下client.php,如愿以偿的输出了:

Hello Word!
Copy after login

这样一个简单的Server/Clent交付就搞定了。虽然中间出了点差错,但是总体来说还是蛮简单易懂的!

其他的更高级的用法可以参考官网的。

yar

yar 是国内著名的php大神鸟哥惠新宸的大作,在微博产品中已经开始使用。它也是一款rpc框架。它由于使用纯C编写的用于php的扩展,所以,效率应该是蛮高的,而且支持异步并行,这点还是赞的。

下载安装

官网下载:http://pecl.php.net/package/yar 最新的版本 yar-1.2.4.tgz

然后解压复制到php源码的etx目录:/lamp/php-5.4.11/ext下。然后用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
Copy after login

但是出现了点问题:提示,curl 有问题:

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

估计是我本机curl 有问题,那用yum 安装一下吧:

yum -y install curl-devel
Copy after login

安装完成curl 后继续编译安装,就没啥问题了:

[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
Copy after login

成功之后,提示我们 yar.so 扩展在已经在/usr/local/php/lib/php/extensions/no-debug-zts-20100525/ 下了。

我们vi编辑一下 php.ini ,最后面加上yar.so扩展,然后重启一下 apache 或者php-pfm就可以了。

[root@localhost /]# vi /usr/local/php/etc/php.ini 
[yar]
extension=yar.so
Copy after login

好。加好了后,我们需要重启下apache或者php-fpm

重启apache

[root@localhost /]# /usr/local/apache/bin/apachectl restart
Copy after login
Copy after login

平滑重启php-fpm

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

重启完毕后,打开phpinfo()页面,搜索一下,应该就能够看到yar了。

开始使用

和其他的rpc框架一样,yar也是server/client模式,所以,我们也一样,开始写一个简单的例子来说下如何调用。

yar_server.php表示服务器端

handle();
Copy after login

好,我们在浏览器里运行一下,就会出现如下图所示的输出。很高端啊!!!鸟哥说这样做的用途是可以一目了然的知道我这个rpc提供了多少接口,把api文档都可以省略了。

好,我们开始写yar_client.php 这个是客户端:

$client = new Yar_Client("http://127.0.0.1/yar_server.php");
echo $client->api('helo word');
Copy after login

像其他的 swoole,hprose等基本都是这个原理,只是看谁的功能更加,用起来更顺手罢了。

更多相关php知识,请访问php教程

The above is the detailed content of Detailed explanation of popular php rpc framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!