In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "detailed introduction of the php rpc framework". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is the RPC framework?
If you summarize RPC in one sentence, it is: remote invocation Framework (Remote Procedure Call)
So what is a remote call?
Usually we call a method in php, such as a function method: localAdd (10,20). The specific implementation of the localAdd method is either defined by the user or included in the php library function, that is to say, the code implementation of the localAdd method is local, it is a local call! Remote invocation means that the specific implementation of the called method is not local to the program, but somewhere remote.
Principle of remote call
For example, A (client) calls the remoteAdd method provided by B (server):
First, establish a TCP connection between An and B.
Then A serializes the method name (here is remoteAdd) and the method parameters (10,20) into a byte stream.
B accepts the byte stream sent by A, then deserializes to get the target method name and method parameters, then executes the corresponding method call (possibly localAdd) and returns the result 30
An accepts the result of the remote call and outputs 30.
The RPC framework encapsulates the details I have just mentioned and exposes users to the simple and friendly use of API.
Benefits of remote invocation
Decoupling: when server needs to implement changes within a method, client is completely unaware of it and does not need to make any changes; this approach is often used in cross-departmental, cross-company collaboration, and the provider of the method is often referred to as service exposure.
What's the difference between RPC and Socket?
Through the simple explanation above, it seems that RPC looks like Socket. They all call remote methods, all in client/server mode. I also wrote an article before: what's the difference between them by talking about socket in detail?
RPC (remote procedure call) uses client / server mode to realize the communication between two processes. Socket is one of the communication methods 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, there are other communication methods, such as: http, the pipeline of the operating system and other technologies to achieve the call of remote programs. In Microsoft's Windows system, RPC uses named pipes to communicate.
What's 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 and calls remote methods. What's the difference between them?
Both REST API and RPC encapsulate functions into interfaces on the Server side for Client to call, but REST API is based on the HTTP protocol, and REST is committed to providing a http request through methods such as POST/GET/PUT/DELETE in the http protocol and a readable URL.
However, RPC may not be based on HTTP protocol.
Therefore, if the back-end two languages call each other, you can get better performance with RPC (omitting a series of things such as HTTP headers) and should be easier to configure.
If the front end calls the back end through AJAX, it's better to use REST API (because you can't avoid HTTP 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 list: phprpc,yar, thrift, gRPC, swoole, hprose
Because of the limited time and energy, it is impossible to learn and use one by one. I choose the ones that are most used in the world. Because the principle of RPC is the same, it is all Client/Server mode, but each framework is used differently.
Mainly explain that phprpc and yar are the ones I have heard and contacted the most so far.
Phprpc
First download the latest stable version of phprpc: download link from the official website.
Installation
We will find that there are many files and folders in it, and the structure is as follows:
Dhparams/pecl/bigint.phpcompat.phpphprpc_date.phpxxtea.phpdhparams.phpphprpc_server.phpphprpc_client.php
Among them, dhparams and pecl are folders, and pecl is the xxtea extension of php. According to the description of the official website, it can be installed or not installed, and it can be run without installing phprpc. But if you need faster encryption processing power, 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 for extension recompilation.
[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, the compilation is complete, prompting us that xxtea.so is already under / usr/local/php/lib/php/extensions/no-debug-zts-20100525/xxtea.so.
Next, 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
After adding it, we need to restart apache or php-fpm.
Restart apache
[root@localhost /] # / usr/local/apache/bin/apachectl restart
Smooth restart of php-fpm
Kill-USR2 `cat / usr/local/php/var/run/php- fpm.pid`
After the restart, open the phpinfo () page, search for it, and you should be able to see xxtea.
Start using
Let's start with a simple example. Phprpc is also divided into server side and client side. So the corresponding folders are phprpc_server.php and phprpc_client.php.
Let's refer to a few examples on the official website to practice:
Server.php server: this completes the simplest helloword interface.
We are performing the following client.php, and the desired output has been achieved:
Hello Word!
Such a simple Server/Clent delivery is done. Although there are some mistakes in the middle, but on the whole it is quite easy to understand!
Other more advanced uses can be found on the official website.
Yar
Yar is the masterpiece of the famous php Divine Bird GE Hui Xinchen, which has been used in Weibo products. It is also a rpc framework. Since it is written in pure C for php extensions, it should be quite efficient and supports asynchronous parallelism, which is nice.
Download and install
Download: yar-1.2.4.tgz, the latest version of http://pecl.php.net/package/yar
Then extract and copy it to the etx directory of the php source code: / lamp/php-5.4.11/ext. Then use phpize for extension recompilation.
[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 is a problem: hint, there is a problem with curl:
Configure: error: Please reinstall the libcurl distribution-easy.h should be in / include/curl/
I guess there is something wrong with my native curl, so install it with yum:
Yum-y install curl-devel
After installing curl, continue to compile and install, and there will be 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, remind us that the yar.so extension is already under / usr/local/php/lib/php/extensions/no-debug-zts-20100525/.
Let's vi edit the php.ini, finally add the yar.so extension, and then restart apache or php-pfm.
[root@localhost /] # vi / usr/local/php/etc/php.ini [yar] extension=yar.so
good. After adding it, we need to restart apache or php-fpm.
Restart apache
[root@localhost /] # / usr/local/apache/bin/apachectl restart
Smooth restart of php-fpm
Kill-USR2 `cat / usr/local/php/var/run/php- fpm.pid`
After the restart, open the phpinfo () page, search for it, and you should be able to see yar.
Start using
Like other rpc frameworks, yar is the server/client pattern, so let's start writing a simple example of how to call it.
Yar_server.php represents the server side
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.