mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-05 13:18:22 +08:00
e1544beec5
Co-authored-by: zmy <my.zhu@knowyourself.cc> Co-authored-by: 沈唁 <52o@qq52o.cn> Co-authored-by: 李铭昕 <l@hyperf.io>
3.2 KiB
3.2 KiB
基于多路复用的 RPC 组件
本组件基于 TCP
协议,多路复用的设计借鉴于 AMQP
组件。
安装
composer require hyperf/rpc-multiplex
Server 配置
修改 config/autoload/server.php
配置文件,以下配置删除了不相干的配置。
settings
设置中,分包规则不允许修改,只可以修改 package_max_length
,此配置需要 Server
和 Client
保持一致。
<?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',
// 这个消费者要从哪个服务中心获取节点信息,如不配置则不会从服务中心获取节点信息
'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' => [
// 包体最大值,若小于 Server 返回的数据大小,则会抛出异常,故尽量控制包体大小
'package_max_length' => 1024 * 1024 * 2,
],
// 重试次数,默认值为 2
'retry_count' => 2,
// 重试间隔,毫秒
'retry_interval' => 100,
// 多路复用客户端数量
'client_count' => 4,
// 心跳间隔 非 numeric 表示不开启心跳
'heartbeat' => 30,
],
],
],
];
注册中心
如果需要使用注册中心,则需要手动添加以下监听器
<?php
return [
Hyperf\RpcMultiplex\Listener\RegisterServiceListener::class,
];