Files
kiri-rpc/RpcManager.php
T

148 lines
2.9 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-01-11 14:15:51 +08:00
use Kiri\Message\Handler\Handler;
2022-01-09 14:00:32 +08:00
use ReflectionException;
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
* @throws ReflectionException
*/
public function add(string $name, string $class, array $serviceConfig): bool
{
$methods = Kiri::getDi()->getReflect($class);
$lists = $methods->getMethods(\ReflectionMethod::IS_PUBLIC);
if (!isset($this->_rpc[$name])) {
$this->_rpc[$name] = ['methods' => [], 'id' => $serviceConfig['ID'], 'config' => $serviceConfig];
}
foreach ($lists as $reflection) {
if ($reflection->getDeclaringClass() != $class) {
continue;
}
$methodName = $reflection->getName();
$this->_rpc[$name]['methods'][$methodName] = [new Handler('/', [$class, $methodName]), null];
}
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];
}
}