rpc配置
rpc服务:
- 仿照app\rpc 文件夹分别创建服务对应的Service/ *.Services文件和Lib/*.Interface文件
- 配置rpc服务app\bean.php的pool连接池和服务的参数
- 复制出RPC Interface文件到http服务机
'trade' => [
'class' => ServiceClient::class,
'host' => '127.0.0.1',
'port' => 18307,
'setting' => [
'timeout' => 0.5,
'connect_timeout' => 1.0,
'write_timeout' => 10.0,
'read_timeout' => 0.5,
],
'packet' => bean('rpcClientPacket'),
'provider' => bean(\App\Common\RpcProvider::class),
],
'trade.pool' => [
'class' => ServicePool::class,
'client' => bean('trade'),
],
'rpcServer' => [
'class' => ServiceServer::class,
'port' => 9801, // trade服务rpc端口为9801
'listener' => [
// 'http' => bean('httpServer'),
]
],
运行 php bin/swoft rpc:start
user服务:
trade服务:
http服务:
- 把Rpc服务机中的App\Rpc\Lib\*.Interface文件拷贝到http服务机的App\Rpc\Lib中
- 修改http服务配置 app\bean.php, 添加对应的服务到配置中
'httpServer' => [ 'class' => HttpServer::class, 'port' => 18307, // 这里要和rpc对应 'listener' => [ // 'rpc' => bean('rpcServer'), // 'tcp' => bean('tcpServer'), ], 'process' => [ // 'monitor' => bean(\App\Process\MonitorProcess::class) // 'crontab' => bean(CrontabProcess::class) ], 'on' => [ // SwooleEvent::TASK => bean(SyncTaskListener::class), // Enable sync task SwooleEvent::TASK => bean(TaskListener::class), // Enable task must task and finish event SwooleEvent::FINISH => bean(FinishListener::class) ], /* @see HttpServer::$setting */ 'setting' => [ 'task_worker_num' => 12, 'task_enable_coroutine' => true, 'worker_num' => 6, // static handle // 'enable_static_handler' => true, // 'document_root' => dirname(__DIR__) . '/public', ] ], 'user' => [ 'class' => ServiceClient::class, 'host' => '39.105.156.191', 'port' => 9803, 'setting' => [ 'timeout' => 0.5, 'connect_timeout' => 1.0, 'write_timeout' => 10.0, 'read_timeout' => 0.5, ], 'packet' => bean('rpcClientPacket') ], 'user.pool' => [ 'class' => ServicePool::class, 'client' => bean('user'), ], 'trade' => [ 'class' => ServiceClient::class, 'host' => '39.105.156.191', 'port' => 9801, 'setting' => [ 'timeout' => 0.5, 'connect_timeout' => 1.0, 'write_timeout' => 10.0, 'read_timeout' => 0.5, ], 'packet' => bean('rpcClientPacket') ], 'trade.pool' => [ 'class' => ServicePool::class, 'client' => bean('trade'), ], 'rpcServer' => [ 'class' => ServiceServer::class, 'listener' => [ 'http' => bean('httpServer'), ] ],
- 控制器中调用rpc
<?php declare(strict_types=1); /** * This file is part of Swoft. * * @link https://swoft.org * @document https://swoft.org/docs * @contact group@swoft.org * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE */ namespace App\Http\Controller; use App\Rpc\Lib\UserInterface; use App\Rpc\Lib\TradeInterface; use Exception; use Swoft\Co; use Swoft\Http\Server\Annotation\Mapping\Controller; use Swoft\Http\Server\Annotation\Mapping\RequestMapping; use Swoft\Rpc\Client\Annotation\Mapping\Reference; /** * Class RpcController * * @since 2.0 * * @Controller() */ class RpcController { /** * @Reference(pool="user.pool") * * @var UserInterface */ private $userService; /** * @Reference(pool="trade.pool") * * @var TradeInterface */ private $tradeService; /** * @RequestMapping("getList") * * @return array */ public function getList(): array { $result = $this->userService->getList(12, 'type'); $result2 = $this->tradeService->getList(12, 'type'); return [$result, $result2]; } }
启动命令
Commands:
reload Reload worker processes
restart Restart the http server
start Start the http server
stop Stop the currently running server
Example:
bin/swoft rpc:start Start the rpc server
bin/swoft rpc:stop Stop the rpc server
前台运行
$ php bin/swoft rpc:start
后台运行
$ php bin/swoft rpc:start -d
跨主机调用rpc注意事项
- 接口约定服务方法和参数(在app\rpc\lib中定义interface接口)
- 实现interface接口 (在app\rpc\Service中实现方法)
- 使用时客户端需要配置连接(在app\rpc\lib中定义)
案例Demo:
代码1 rpc 9801 trade ,代码2 rpc 9803 user, 代码3 client
1,2 运行php bin/swoft rpc:start
3 运行php bin/swoft http:start
下载链接: swoft2框架使用rpc调用其他主机方法-PHP文档类资源-CSDN下载
注册RPC到consul
app/bean.php 文件中配置:
return [
// ...
'consul' => [
'host' => '192.168.4.11'
]
// ...
];
详细配置
host
consul 地址IPport
consul 端口号timeout
请求超时时间
app\bean.php 服务配置中添加 provider
'user' => [
'class' => ServiceClient::class,
'host' => '127.0.0.1',
'port' => 18307,
'setting' => [
'timeout' => 0.5,
'connect_timeout' => 1.0,
'write_timeout' => 10.0,
'read_timeout' => 0.5,
],
'packet' => bean('rpcClientPacket'),
'provider' => bean(\App\Common\RpcProvider::class) // 添加的这句
],
'user.pool' => [
'class' => ServicePool::class,
'client' => bean('user'),
],
/app/Listener/RegisterServiceListener.php中最下面的注册服务取消注释 , 然后试一下吧
public function handle(EventInterface $event): void
{
/** @var HttpServer $httpServer */
$httpServer = $event->getTarget();
$service = [
'ID' => 'swofts', //
'Name' => 'swofts', //'swofts'
'Tags' => [
'http'
],
'Address' => '127.0.0.1',
'Port' => $httpServer->getPort(),
'Meta' => [
'version' => '1.0'
],
'EnableTagOverride' => false,
'Weights' => [
'Passing' => 10,
'Warning' => 1
],
"Check" => [
"name" => "swoft.swofts.server",
// 192.168.31.22 这是 swoft 的服务宿主机地址
"tcp" => "39.105.156.191:9803", //服务器ip地址
"interval" => '10s', //检查时间间隔
"timeout" => '2s' //检查超时时间
],
];
// Register
$this->agent->registerService($service);
\Swoft\Log\Helper\CLog::info('Swoft http register service success by consul!');
}