Preface:

Remote Procedure Call (RPC) means Remote Procedure Call

Two problems RPC addresses:

  1. To solve the invocation problem between services in distributed system.
  2. The remote call should be as convenient as the local call, so that the caller is not aware of the logic of the remote call

If you mimic the B/S architecture, each time you call, you need to write a string of code that initiates an HTTP request. Such as new Curl ()… And so on, can like local call, to initiate a remote call, so that users do not perceive the process of remote call.

In practice, RPC rarely uses HTTP protocol for data transmission. After all, I just want to transmit data, so why use an application layer protocol for text transmission? Why not just use binary transfer.

Installation:

$ pecl install yar
$ pecl install msgpack
Copy the code

Server code:

<? php require_once 'Email.php'; require_once 'Sms.php'; class Service { private $sms; private $email; public function __construct() { $this->sms = new Sms(); $this->email = new Email(); } /** * @param $mobile * @return string */ public function sendSms($mobile, $this-> SMS ->send($mobile, $content); $this-> SMS ->send($mobile, $content); } /** * @param $email * @return string */ public function sendEmail($email) { return $this->email->send($email); } } $service = new Yar_Server(new Service()); $service->handle();Copy the code

Client code:

<? PHP / / a, direct call / / $client = new Yar_Client (" http://192.168.2.138/api/ "); //$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, 1000); $result = $client->sendSms('18234065200', 'I am the content of the SMS '); //echo $result; Public static call (string $uri, string $method [, array $parameters [, callable $callback [,); Callable $error_callback [, array $options]]]]) // Callback is called by Yar when the return from the remote service arrives. If not, call callback // error_callback // $options in the loop Yar_Concurrent_Client: : call (" http://192.168.2.138/api/ ", "sendSms", [' 18234065200 ', 'welcome to register']); Yar_Concurrent_Client: : call (" http://192.168.2.138/api/ ", "sendEmail." [' 12580 @qq.com '], "callback", "callError." [YAR_OPT_TIMEOUT => 10]); Public static loop ([callable $callback [, callable $error_callback ]] ) Yar_Concurrent_Client::loop("loopCallback", "loopError"); Function callback($retval, $callinfo) {if ($retval, $callinfo) {if ($retval, $callinfo) {if ($retval, $callinfo) {if ($retval, $callinfo); $callback ($retval, $callback, $callback, $callback, $callback, $callback, $callback, $callback, $callback, $callback, $callback, $callback, $callback); $callinfo) {if (is_null($callinfo)) {echo 'PHP_EOL; } else {echo 'PHP_EOL; var_dump($retval); var_dump($callinfo); }} function callError() {echo 'sending RPC error'. PHP_EOL; } function loopError() {echo 'sending RPC error'. PHP_EOL; }Copy the code

! [](https://pic4.zhimg.com/80/v2-0c7069872a2217ecfdb5c5aa4fb59277_720w.jpg)

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be free to share with you