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]