Files
kiri-rpc/RpcManager.php
T

171 lines
3.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;
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 = [];
/**
* @param $serviceName
* @return void
2022-01-13 18:25:29 +08:00
* @throws Exception
2022-01-09 14:00:32 +08:00
*/
public function async($serviceName): void
{
2022-01-13 18:25:29 +08:00
$this->reRegister($serviceName);
2022-01-09 14:00:32 +08:00
$lists = Kiri::getDi()->get(Health::class)->setQuery('passing=true')->service($serviceName);
if ($lists->getStatusCode() != 200) {
return;
}
$body = json_decode($lists->getBody(), true);
$file = storage('.rpc.clients.' . md5($serviceName), 'rpc');
if (!empty($body) && is_array($body)) {
file_put_contents($file, json_encode(array_column($body, 'Service')), LOCK_EX);
} else {
file_put_contents($file, json_encode([]), LOCK_EX);
}
}
2022-01-13 18:25:29 +08:00
/**
* @param string $serviceName
* @return void
* @throws Kiri\Exception\ConfigException
* @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']);
2022-01-13 18:58:40 +08:00
var_dump($info->getBody());
2022-01-13 18:25:29 +08:00
if ($info->getStatusCode() == 200) {
return;
}
$data = $service->service->register($config['config']);
2022-01-13 18:47:34 +08:00
$this->logger()->info($data->getBody());
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) {
$this->async($name);
}
} catch (\Throwable $throwable) {
$this->logger()->error(error_trigger_format($throwable));
2022-01-09 14:00:32 +08:00
}
}
/**
* @param $serviceName
* @return array
2022-01-13 18:25:29 +08:00
* @throws Exception
2022-01-09 14:00:32 +08:00
*/
public function getServices($serviceName): array
{
$file = storage('.rpc.clients.' . md5($serviceName), 'rpc');
if (!file_exists($file) || filesize($file) < 10) {
$this->async($serviceName);
}
$content = json_decode(file_get_contents($file), true);
if (empty($content) || !is_array($content)) {
return [];
}
return $content;
}
/**
* @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];
}
}