hyperf/docs/en/rpc-multiplex.md

128 lines
3.5 KiB
Markdown
Raw Normal View History

2022-10-31 01:47:12 +08:00
# Multiplexed based RPC components
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
This component is based on the `TCP` protocol, and the multiplexing design is borrowed from the `AMQP` component.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
## Install
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
````
2022-02-16 02:13:55 +08:00
composer require hyperf/rpc-multiplex
2022-10-31 01:47:12 +08:00
````
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
## Server configuration
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
Modify the `config/autoload/server.php` configuration file, the following configuration deletes irrelevant configuration.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
In the `settings` setting, the subcontracting rules are not allowed to be modified, only `package_max_length` can be modified, this configuration needs to be consistent between `Server` and `Client`.
2022-02-16 02:13:55 +08:00
```php
<?php
declare(strict_types=1);
use Hyperf\Server\Event;
use Hyperf\Server\Server;
return [
'servers' => [
[
'name' => 'rpc',
'type' => Server::SERVER_BASE,
'host' => '0.0.0.0',
'port' => 9502,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
Event::ON_RECEIVE => [Hyperf\RpcMultiplex\TcpServer::class, 'onReceive'],
],
'settings' => [
'open_length_check' => true,
'package_length_type' => 'N',
'package_length_offset' => 0,
'package_body_offset' => 4,
'package_max_length' => 1024 * 1024 * 2,
],
],
],
];
```
2022-10-31 01:47:12 +08:00
Create `RpcService`
2022-02-16 02:13:55 +08:00
```php
<?php
namespace App\RPC;
use App\JsonRpc\CalculatorServiceInterface;
use Hyperf\RpcMultiplex\Constant;
use Hyperf\RpcServer\Annotation\RpcService;
/**
* @RpcService(name="CalculatorService", server="rpc", protocol=Constant::PROTOCOL_DEFAULT)
*/
class CalculatorService implements CalculatorServiceInterface
{
}
```
2022-10-31 01:47:12 +08:00
## client configuration
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
Modify the `config/autoload/services.php` configuration file
2022-02-16 02:13:55 +08:00
```php
<?php
declare(strict_types=1);
return [
'consumers' => [
[
'name' => 'CalculatorService',
'service' => App\JsonRpc\CalculatorServiceInterface::class,
'id' => App\JsonRpc\CalculatorServiceInterface::class,
'protocol' => Hyperf\RpcMultiplex\Constant::PROTOCOL_DEFAULT,
'load_balancer' => 'random',
2022-10-31 01:47:12 +08:00
// Which service center does the consumer want to obtain node information from, if not configured, the node information will not be obtained from the service center
2022-02-16 02:13:55 +08:00
'registry' => [
'protocol' => 'consul',
'address' => 'http://127.0.0.1:8500',
],
'nodes' => [
['host' => '127.0.0.1', 'port' => 9502],
],
'options' => [
'connect_timeout' => 5.0,
'recv_timeout' => 5.0,
'settings' => [
2022-10-31 01:47:12 +08:00
// The maximum value of the package body. If it is less than the data size returned by the Server, an exception will be thrown, so try to control the package body size as much as possible.
2022-02-16 02:13:55 +08:00
'package_max_length' => 1024 * 1024 * 2,
],
2022-10-31 01:47:12 +08:00
// number of retries, default is 2
2022-02-16 02:13:55 +08:00
'retry_count' => 2,
2022-10-31 01:47:12 +08:00
// retry interval, milliseconds
2022-02-16 02:13:55 +08:00
'retry_interval' => 100,
2022-10-31 01:47:12 +08:00
// Number of multiplexed clients
2022-02-16 02:13:55 +08:00
'client_count' => 4,
2022-10-31 01:47:12 +08:00
// Heartbeat interval non-numeric means no heartbeat
2022-02-16 02:13:55 +08:00
'heartbeat' => 30,
],
],
],
];
```
2022-10-31 01:47:12 +08:00
### Registration Center
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
If you need to use the registry, you need to manually add the following listeners
2022-02-16 02:13:55 +08:00
```php
<?php
return [
Hyperf\RpcMultiplex\Listener\RegisterServiceListener::class,
];
```