hyperf / rpc-multiplex
Rpc for multiplexing connection
Fund package maintenance!
Open Collective
hyperf.wiki/#/zh-cn/donate
Installs: 50 743
Dependents: 8
Suggesters: 0
Security: 0
Stars: 7
Watchers: 4
Forks: 2
Open Issues: 0
Requires
- php: >=8.1
- hyperf/codec: ~3.1.0
- hyperf/framework: ~3.1.0
- hyperf/http-message: ~3.1.0
- hyperf/load-balancer: ~3.1.0
- hyperf/rpc: ~3.1.0
- hyperf/rpc-client: ~3.1.0
- hyperf/rpc-server: ~3.1.0
- hyperf/server: ~3.1.0
- hyperf/support: ~3.1.0
- hyperf/utils: ~3.1.0
- jetbrains/phpstorm-attributes: ^1.0
- multiplex/socket: ^1.0
- swow/psr7-plus: ^1.0
- 3.1.x-dev
- dev-master / 3.1.x-dev
- v3.1.42
- v3.1.35
- v3.1.27
- v3.1.20
- v3.1.18
- v3.1.15
- v3.1.8
- v3.1.7
- v3.1.0
- v3.1.0-rc.14
- v3.1.0-rc.4
- v3.1.0-beta.13
- v3.1.0-beta.2
- v3.1.0-alpha.2
- v3.1.0-alpha.1
- 3.0.x-dev
- v3.0.45
- v3.0.37
- v3.0.34
- v3.0.19
- v3.0.18
- v3.0.16
- v3.0.15
- v3.0.11
- v3.0.0
- v3.0.0-rc.13
- v3.0.0-rc.4
- v3.0.0-rc.3
- v3.0.0-rc.1
- v3.0.0-beta.10
- v3.0.0-beta.1
- v3.0.0-alpha.6
- v3.0.0-alpha.4
- v3.0.0-alpha.1
- 2.2.x-dev
- v2.2.33
- v2.2.14
- v2.2.4
- v2.2.0
- v2.2.0-rc2
- v2.2.0-rc1
This package is auto-updated.
Last update: 2024-12-21 02:31:00 UTC
README
安装
composer require hyperf/rpc-multiplex
Server 配置
修改 config/autoload/server.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, ], ], ], ];
创建 RpcService
<?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 { }
客户端配置
修改 config/autoload/services.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', 'nodes' => [ ['host' => '127.0.0.1', 'port' => 9502], ], 'options' => [ 'connect_timeout' => 5.0, 'recv_timeout' => 5.0, 'settings' => [ // 包体最大值,若小于 Server 返回的数据大小,则会抛出异常,故尽量控制包体大小 'package_max_length' => 1024 * 1024 * 2, ], // 重试次数,默认值为 2 'retry_count' => 2, // 重试间隔,毫秒 'retry_interval' => 100, // 多路复用客户端数量 'client_count' => 4, // 心跳间隔 非 numeric 表示不开启心跳 'heartbeat' => 30, ], ], ], ];