swoft 2启动rpc

1 篇文章 0 订阅

rpc配置

 

rpc服务:

  1. 仿照app\rpc 文件夹分别创建服务对应的Service/ *.Services文件和Lib/*.Interface文件
  2. 配置rpc服务app\bean.php的pool连接池和服务的参数
  3. 复制出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服务:

  1. 把Rpc服务机中的App\Rpc\Lib\*.Interface文件拷贝到http服务机的App\Rpc\Lib
  2. 修改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'),
            ]
        ],

  3. 控制器中调用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注意事项

  1. 接口约定服务方法和参数(在app\rpc\lib中定义interface接口)
  2. 实现interface接口 (在app\rpc\Service中实现方法)
  3. 使用时客户端需要配置连接(在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 地址IP
  • port 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!');
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苗先生的PHP记录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值