微服务之PHP swoft 框架Hello world

博客介绍了基于Docker的微服务RPC服务配置过程。需准备两台互通电脑,在其中一台安装docker consul集群,接着修改多个PHP文件方法,在两台主机进行相关配置,加入特定文件,最后启动RPC服务和http服务,访问指定地址查看配置结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、准备两台互通的电脑(可以是虚拟机),假设系统IP分别为192.168.2.2和192.168.2.3

2、在192.168.1.2 系统上安装docker consul集群,步骤https://blog.youkuaiyun.com/guoguicheng1314/article/details/109815257

3、修改app\common\RpcProvider.php 的getList方法

public function getList(Client $client): array
{
        // Get health service from consul
        $services = $this->health->service('swoft');
        $servicesArr = json_decode($services->getBody(), true);
        #$members = $this->agent->members();
        $list = [];
        #$body = json_decode($members->getBody(), true) ?: [];
        #print_r($body);

        foreach ($servicesArr as $item) {
            $service = $item['Service'];
            $list[] = "{$service['Address']}:{$service['Port']}";
        }
        print_r($list);
        echo "========================+++++++++++++++++======================\n";
        return $list;
}

4、修改app/listener/RegisterServiceListener.php 的handle方法

public function handle(EventInterface $event): void
    {
        /** @var HttpServer $httpServer */
        $httpServer = $event->getTarget();

        // Register
        if (env('CONSUL_ON') && $httpServer->getServerType() === 'RPC') {
            $service = [
                'ID'                => 'swoft',
                'Name'              => 'swoft',
                'Tags'              => [
                    'http'
                ],
                'Address'           => $httpServer->getHost(),
                'Port'              => $httpServer->getPort(),
                'Meta'              => [
                    'version' => '1.0'
                ],
                'EnableTagOverride' => false,
                'Weights'           => [
                    'Passing' => 10,
                    'Warning' => 1
                ]
            ];
            $this->agent->registerService($service);
            CLog::info('Swoft http register service success by consul!');
        }
    }

5、修改app/listener/DeregisterServiceListener.php的handle方法

    /**
     * @param EventInterface $event
     */
    public function handle(EventInterface $event): void
    {
        /** @var HttpServer $httpServer */
        $httpServer = $event->getTarget();

        if (env('CONSUL_ON') && $httpServer->getServerType() === 'RPC') {
            $this->agent->deregisterService('swoft');
        }
    }

6、在两台主机配置app/bean.php中分别加入配置

// 192.168.2.2上的bean.php配置加上
 $bean['consul'] = [
        'host' => env('CONSUL_HOST', '192.168.2.2'),
        'port' => env('CONSUL_PORT',8500),
        'timeout' => 2
    ];

// 192.168.2.3的bean.php配置加上
 $bean['consul'] = [
        'host' => env('CONSUL_HOST', '192.168.2.3'),
        'port' => env('CONSUL_PORT',8510),
        'timeout' => 2
    ];

7、在两台主机分别加入app/Rpc/Lib/TestInterface.php

# 在两台主机分别加入app/Rpc/Lib/TestInterface.php

<?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\Rpc\Lib;

/**
 * Class TestInterface
 *
 * @since 2.0
 */
interface TestInterface
{
    /**
     * @return string
     */
    public function say(): string;
}

8、在192.168.2.3 app/Rpc/Service/TestService.php加入

<?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\Rpc\Service;

use App\Rpc\Lib\UserInterface;
use Exception;
use RuntimeException;
use Swoft\Co;
use Swoft\Rpc\Server\Annotation\Mapping\Service;

/**
 * Class TestService
 *
 * @since 2.0
 *
 * @Service()
 */
class TestService implements UserInterface
{
    /**
     * @param int   $id
     * @param mixed $type
     * @param int   $count
     *
     * @return array
     */
    public function say(): string
    {
        return 'hello world';
    }
}

9、在192.168.2.2 app/Http/Controller/RpcController.php 控制器中掉用

   /**
     * @Reference(pool="user.pool")
     *
     * @var TestInterface
     */
    private $testService;

    /**
     * @RequestMapping("say")
     *
     * @return array
     */
    public function say(): array
    {
        $result  = $this->testService->say();

        return [$result];
    }

10、启动两台主机的RPC服务和192.168.2.2主机的http服务

# 192.168.2.2
php bin/swoft rpc:start
php bin/swoft http:start

#192.168.2.3
php bin/swoft rpc:start

11、访问http://192.168.2.2/rpc/say 查看,配置成功的话应该输出[hello world]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_________MAN

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

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

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

打赏作者

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

抵扣说明:

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

余额充值