1. What is RPC
RPC is a way of communication between processes, the full name is "remote call procedure".
When a client sends a request to the server, it is not sent directly to the target server, but to the RPC Server, which schedules it.
RPC provides remote calling methods that are no different from calling local methods.
2. Introduction to Hprose
Hprose is a lightweight high-performance cross language RPC service framework, which provides synchronous, asynchronous, write and other call modes.
The simple architecture of Hprose is shown in the figure below
3. Specific examples of Hprose
Hprose supports a variety of protocols. The following shows the demo in various protocols
1) Http protocol
Client:
<?php require_once "vendor/autoload.php"; use Hprose\Swoole\Client; $client = Client::create('http://hprose.com/example/', false);
The server:
<?php require_once "vendor/autoload.php"; use Hprose\Swoole\Server; function hello($name) { return "Hello $name!"; } $server = new Server(); $server->addFunction('hello'); $server->start();
2) TCP protocol
Client:
<?php require_once "vendor/autoload.php"; use Hprose\Swoole\Client; $client = Client::create('tcp://127.0.0.1:1234', false);
The server:
<?php require_once "vendor/autoload.php"; use Hprose\Swoole\Server; function hello($name) { return "Hello $name!"; } $server = new Server("tcp://0.0.0.0:1234"); $server->addFunction('hello'); $server->start();
3) create UNIX Socket server
Client:
<?php require_once "vendor/autoload.php"; use Hprose\Swoole\Client; $client = Client::create('unix:/tmp/my.sock');
The server:
<?php require_once "vendor/autoload.php"; use Hprose\Swoole\Server; function hello($name) { return "Hello $name!"; } $server = new Server("unix:/tmp/my.sock"); $server->addFunction('hello'); $server->start();
4) asynchronous WebSocket client
Client:
<?php require_once "vendor/autoload.php"; use Hprose\Swoole\Client; $client = Client::create('ws://127.0.0.1:8080/');
The server:
<?php require_once "vendor/autoload.php"; use Hprose\Swoole\Server; function hello($name) { return "Hello $name!"; } $server = new Server("ws://0.0.0.0:8088"); $server->addFunction('hello'); $server->start();
4.Hprose-Swoole
Hprose swoole supports the on, set, listen methods of swoole
The operation steps are as follows:
1) create Hprose service object
2) set server parameters and callback functions
3) initialize the framework in the onStart callback
4) initialize PDO and other connections in the onWorkerStart callback
5) register the service interface and start the service