Files
kiri-rpc/Etcd.php
T

90 lines
1.8 KiB
PHP
Raw Normal View History

2023-05-16 15:21:50 +08:00
<?php
2023-05-20 23:05:38 +08:00
use Etcd\Client;
use GuzzleHttp\Exception\BadResponseException;
use Kiri\Abstracts\Component;
use Kiri\Core\Str;
/**
*
*/
class Etcd extends Component
2023-05-16 15:21:50 +08:00
{
2023-05-20 23:05:38 +08:00
private Client $client;
private bool $isEnd = false;
private array $config = [];
private array|BadResponseException $grant;
/**
* @return void
* @throws Exception
*/
public function init(): void
{
$this->client = new Client('47.92.194.207:' . 2379, 'v3');
$this->grant = $this->client->grant(60);
if ($this->grant instanceof BadResponseException) {
throw new Exception($this->grant->getMessage());
}
$key = 'center.service.' . gethostbyname(gethostname());
2024-04-24 14:31:06 +08:00
\pcntl_signal(SIGINT, function () use ($key) {
2023-05-20 23:05:38 +08:00
$this->isEnd = true;
$this->client->del($key);
});
$this->client->put($key, json_encode([
'address' => gethostbyname(gethostname()) . ':10240',
'nodeId' => Str::rand(32)
]), ['lease' => (int)$this->grant["ID"]]);
}
/**
* @param string $key
* @return mixed
*/
public function get(string $key): mixed
{
return $this->config[$key] ?? null;
}
/**
* @param string $key
* @param mixed $value
* @return void
* @throws Exception
*/
public function put(string $key, mixed $value): void
{
$result = $this->client->put($key, $value);
if ($result instanceof BadResponseException) {
throw new Exception($result->getMessage());
}
$this->config[$key] = $value;
}
/**
* @return void
*/
public function waite(): void
{
while ($this->isEnd == false) {
$this->client->keepAlive((int)$this->grant["ID"]);
sleep(1);
}
}
2023-05-16 15:21:50 +08:00
}