Files
kiri-rpc/src/Note/JsonRpc.php
T

84 lines
2.2 KiB
PHP
Raw Normal View History

2021-10-26 18:58:09 +08:00
<?php
2021-11-30 15:10:01 +08:00
namespace Kiri\Rpc\Note;
2021-10-26 18:58:09 +08:00
2021-10-29 18:06:04 +08:00
use Kiri\Abstracts\Config;
2021-12-02 14:06:57 +08:00
use Kiri\Core\Network;
2021-10-29 18:06:04 +08:00
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
2021-10-28 15:14:21 +08:00
use Kiri\Rpc\RpcManager;
2021-12-02 14:06:57 +08:00
use Note\Attribute;
2021-10-28 15:14:21 +08:00
use ReflectionException;
2021-10-28 10:52:09 +08:00
#[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc extends Attribute
2021-10-26 18:58:09 +08:00
{
2021-12-02 14:06:57 +08:00
private string $uniqueId = '';
2021-10-26 18:58:09 +08:00
/**
2021-11-29 15:57:36 +08:00
* @param string $service
2021-10-29 18:06:04 +08:00
* @param string $driver
2021-12-02 14:06:57 +08:00
* @param array $tags
* @param array $meta
2021-10-29 18:06:04 +08:00
* @param array $checkOptions
2021-10-26 18:58:09 +08:00
*/
2021-12-02 14:06:57 +08:00
public function __construct(public string $service, public string $driver, public array $tags = [], public array $meta = [], public array $checkOptions = [])
2021-10-26 18:58:09 +08:00
{
2021-12-02 14:06:57 +08:00
$this->uniqueId = preg_replace('/(\w{11})(\w{4})(\w{3})(\w{8})(\w{6})/', '$1-$2-$3-$4-$5', md5(__DIR__ . '.' . md5(Network::local())));
2021-10-26 18:58:09 +08:00
}
2021-10-28 10:52:09 +08:00
/**
* @param mixed $class
* @param mixed|string $method
* @return mixed
2021-10-28 15:14:21 +08:00
* @throws ReflectionException
2021-10-29 18:06:04 +08:00
* @throws ConfigException
2021-10-28 10:52:09 +08:00
*/
2021-10-28 15:14:21 +08:00
public function execute(mixed $class, mixed $method = ''): bool
2021-10-28 10:52:09 +08:00
{
2021-12-02 14:06:57 +08:00
return Kiri::getDi()->get(RpcManager::class)->add($this->service, $class, $this->create());
2021-10-29 18:06:04 +08:00
}
/**
* @throws ConfigException
*/
protected function create(): array
{
2021-12-02 14:06:57 +08:00
$rpcPort = Config::get('rpc.port');
2021-10-29 18:06:04 +08:00
return [
2021-12-02 14:06:57 +08:00
"ID" => "rpc.json.{$this->service}." . $this->uniqueId,
2021-12-02 14:24:26 +08:00
"Name" => $this->service,
2021-12-02 14:25:50 +08:00
"EnableTagOverride" => false,
2021-12-02 14:06:57 +08:00
"TaggedAddresses" => [
"lan" => [
"address" => "127.0.0.1",
"port" => $rpcPort
],
"wan" => [
"address" => Network::local(),
"port" => $rpcPort
]
],
"Meta" => $this->meta,
2021-12-02 14:25:50 +08:00
"Address" => Network::local(),
2021-12-02 14:06:57 +08:00
"Port" => $rpcPort,
"Check" => [
2021-12-02 14:22:23 +08:00
"CheckId" => "service:rpc.json.{$this->service}." . $this->uniqueId,
"Name" => "service " . $this->service . ' health check',
"Notes" => "Script based health check",
"ServiceID" => $this->service,
"TCP" => Network::local() . ":" . Config::get('rpc.port'),
"Interval" => "5s",
"Timeout" => "1s",
"DeregisterCriticalServiceAfter" => "30s"
2021-12-02 14:06:57 +08:00
],
2021-10-29 18:06:04 +08:00
];
2021-10-28 10:52:09 +08:00
}
2021-10-26 18:58:09 +08:00
}