Files
kiri-rpc/RpcManager.php
T

138 lines
2.6 KiB
PHP
Raw Normal View History

2022-01-09 14:00:32 +08:00
<?php
namespace Kiri\Rpc;
2022-01-13 18:25:29 +08:00
use Exception;
use Kiri;
2022-01-09 14:00:32 +08:00
use Kiri\Abstracts\Component;
2022-03-02 15:15:36 +08:00
use Kiri\Annotation\Inject;
2022-01-09 14:00:32 +08:00
use Kiri\Consul\Agent;
use Kiri\Consul\Health;
2022-03-02 16:27:15 +08:00
use Kiri\Message\Handler\Router;
2022-01-09 14:00:32 +08:00
class RpcManager extends Component
{
/**
* @var array
*/
private array $_rpc = [];
2022-03-02 15:15:36 +08:00
#[Inject(Health::class)]
public Health $health;
2022-01-09 14:00:32 +08:00
2022-01-13 18:25:29 +08:00
/**
* @param string $serviceName
* @return void
* @throws Exception
*/
public function reRegister(string $serviceName)
{
$config = $this->_rpc[$serviceName] ?? [];
if (empty($config)) {
return;
}
$service = Kiri::getDi()->get(Agent::class);
$info = $service->service->service_health($config['config']['ID']);
if ($info->getStatusCode() == 200) {
return;
}
2022-01-13 19:02:07 +08:00
$service->service->register($config['config']);
2022-01-13 18:25:29 +08:00
}
2022-01-09 14:00:32 +08:00
/**
2022-01-13 18:25:29 +08:00
* @throws Exception
2022-01-09 14:00:32 +08:00
*/
public function tick(): void
{
2022-01-13 18:42:42 +08:00
try {
foreach ($this->_rpc as $name => $list) {
2022-03-02 15:15:36 +08:00
$this->reRegister($name);
2022-01-13 18:42:42 +08:00
}
} catch (\Throwable $throwable) {
2022-02-23 16:32:08 +08:00
$this->logger->error(error_trigger_format($throwable));
2022-01-09 14:00:32 +08:00
}
}
/**
* @param $serviceName
2022-03-02 15:15:36 +08:00
* @return array|null
2022-01-09 14:00:32 +08:00
*/
2022-03-02 15:15:36 +08:00
public function getServices($serviceName): ?array
2022-01-09 14:00:32 +08:00
{
2022-03-02 15:15:36 +08:00
$lists = $this->health->setQuery('passing=true')->service($serviceName);
if ($lists->getStatusCode() != 200) {
return null;
2022-01-09 14:00:32 +08:00
}
2022-03-02 15:15:36 +08:00
$body = json_decode($lists->getBody(), true);
if (empty($body)) {
return null;
2022-01-09 14:00:32 +08:00
}
2022-03-02 15:15:36 +08:00
return array_column($body, 'Service');
2022-01-09 14:00:32 +08:00
}
/**
* @param string $name
* @param string $class
* @param array $serviceConfig
* @return bool
*/
public function add(string $name, string $class, array $serviceConfig): bool
{
if (!isset($this->_rpc[$name])) {
$this->_rpc[$name] = ['methods' => [], 'id' => $serviceConfig['ID'], 'config' => $serviceConfig];
}
2022-03-02 16:27:15 +08:00
Router::addServer('rpc', static function () use ($name, $class) {
Router::get($name, $class);
});
2022-01-09 14:00:32 +08:00
return true;
}
/**
* @return array
*/
public function doneList(): array
{
$array = [];
foreach ($this->_rpc as $list) {
$array[] = $list;
}
return $array;
}
/**
*/
public function register()
{
2022-01-11 14:41:06 +08:00
$agent = Kiri::getDi()->get(Agent::class);
foreach ($this->_rpc as $list) {
2022-01-11 17:56:16 +08:00
$agent->service->deregister($list['config']['ID']);
2022-01-11 14:41:06 +08:00
$data = $agent->service->register($list['config']);
if ($data->getStatusCode() != 200) {
return;
2022-01-09 14:00:32 +08:00
}
}
}
/**
* @param string $name
* @param string $method
* @return mixed
*/
public function get(string $name, string $method): array
{
return $this->_rpc[$name]['methods'][$method] ?? [null, null];
}
}