Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0b3310b82e | |||
| e8cf7f29f0 | |||
| 62e3b001f0 | |||
| f655cebdf3 | |||
| ee492d97ad | |||
| a16d841d98 | |||
| f015662fd6 | |||
| 021dcae59b | |||
| c6dc34c821 | |||
| 88c3958aba | |||
| 24ebfac360 | |||
| b3c200b443 | |||
| 3e2b50bf60 | |||
| 89c142930e | |||
| 1515d9695c | |||
| f1f7b1081d | |||
| 07533e29f9 | |||
| 4d60a24fac | |||
| 4dc6bc661a | |||
| 0d7fd5e356 | |||
| da16b0685d | |||
| febbdea8c8 | |||
| a45c3d875c | |||
| 64d01c0a80 | |||
| bf5fe594e4 | |||
| 5cfe3c6d6d | |||
| 773e3c0f57 | |||
| b1c91343ef | |||
| 3f821ca9d0 | |||
| d296b3107e | |||
| f606c58204 |
@@ -5,7 +5,7 @@ namespace Kiri\Rpc\Annotation;
|
|||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Core\Network;
|
use Kiri\Core\Network;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use Kiri\Rpc\RpcManager;
|
use Kiri\Rpc\RpcManager;
|
||||||
use Kiri\Annotation\Attribute;
|
use Kiri\Annotation\Attribute;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
@@ -70,7 +70,7 @@ use ReflectionException;
|
|||||||
"Check" => [
|
"Check" => [
|
||||||
"CheckId" => "service:rpc.json.{$this->service}." . $this->uniqueId,
|
"CheckId" => "service:rpc.json.{$this->service}." . $this->uniqueId,
|
||||||
"Name" => "service " . $this->service . ' health check',
|
"Name" => "service " . $this->service . ' health check',
|
||||||
"Annotations" => "Script based health check",
|
"Notes" => "Script based health check",
|
||||||
"ServiceID" => $this->service,
|
"ServiceID" => $this->service,
|
||||||
"TCP" => $this->checkUrl,
|
"TCP" => $this->checkUrl,
|
||||||
"Interval" => "5s",
|
"Interval" => "5s",
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@ use Exception;
|
|||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Context;
|
use Kiri\Context;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use Kiri\Pool\Alias;
|
use Kiri\Pool\Alias;
|
||||||
use Kiri\Pool\Pool;
|
use Kiri\Pool\Pool;
|
||||||
use Swoole\Client;
|
use Swoole\Client;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use Exception;
|
|||||||
use Kiri\Message\ServerRequest;
|
use Kiri\Message\ServerRequest;
|
||||||
use Kiri\Message\Stream;
|
use Kiri\Message\Stream;
|
||||||
use Kiri\Core\Number;
|
use Kiri\Core\Number;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use Kiri\Pool\Pool;
|
use Kiri\Pool\Pool;
|
||||||
use Psr\Http\Client\ClientExceptionInterface;
|
use Psr\Http\Client\ClientExceptionInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|||||||
+28
-20
@@ -2,30 +2,29 @@
|
|||||||
|
|
||||||
namespace Kiri\Rpc;
|
namespace Kiri\Rpc;
|
||||||
|
|
||||||
|
use Kiri;
|
||||||
|
use Kiri\Abstracts\Component;
|
||||||
|
use Kiri\Abstracts\Config;
|
||||||
|
use Kiri\Annotation\Annotation;
|
||||||
|
use Kiri\Annotation\Inject;
|
||||||
|
use Kiri\Consul\Agent;
|
||||||
|
use Kiri\Context;
|
||||||
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Message\Constrict\RequestInterface;
|
use Kiri\Message\Constrict\RequestInterface;
|
||||||
use Kiri\Message\Handler\Handler;
|
use Kiri\Message\Handler\Handler;
|
||||||
use Kiri\Message\Handler\Router;
|
use Kiri\Message\Handler\Router;
|
||||||
use Kiri\Message\ServerRequest;
|
use Kiri\Message\ServerRequest;
|
||||||
use Kiri\Abstracts\Component;
|
|
||||||
use Kiri\Abstracts\Config;
|
|
||||||
use Kiri\Consul\Agent;
|
|
||||||
use Kiri\Context;
|
|
||||||
use Kiri\Events\EventProvider;
|
|
||||||
use Kiri\Exception\ConfigException;
|
|
||||||
use Kiri\Kiri;
|
|
||||||
use Kiri\Annotation\Inject;
|
|
||||||
use Kiri\Annotation\Annotation;
|
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
|
||||||
use ReflectionException;
|
|
||||||
use Kiri\Server\Contract\OnCloseInterface;
|
use Kiri\Server\Contract\OnCloseInterface;
|
||||||
use Kiri\Server\Contract\OnConnectInterface;
|
use Kiri\Server\Contract\OnConnectInterface;
|
||||||
use Kiri\Server\Contract\OnReceiveInterface;
|
use Kiri\Server\Contract\OnReceiveInterface;
|
||||||
use Kiri\Server\Events\OnBeforeShutdown;
|
use Kiri\Server\Events\OnBeforeShutdown;
|
||||||
use Kiri\Server\Events\OnServerBeforeStart;
|
use Kiri\Server\Events\OnServerBeforeStart;
|
||||||
use Kiri\Server\Events\OnTaskerStart;
|
use Kiri\Server\Events\OnTaskerStart;
|
||||||
|
use Kiri\Server\Events\OnWorkerExit;
|
||||||
use Kiri\Server\Events\OnWorkerStart;
|
use Kiri\Server\Events\OnWorkerStart;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
use Swoole\Coroutine\Channel;
|
use Swoole\Coroutine\Channel;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
@@ -47,9 +46,11 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
|||||||
public Annotation $annotation;
|
public Annotation $annotation;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private RpcManager $manager;
|
private RpcManager $manager;
|
||||||
|
|
||||||
|
|
||||||
|
private int $timerId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
@@ -58,9 +59,10 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
|||||||
{
|
{
|
||||||
$this->eventProvider->on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']);
|
$this->eventProvider->on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']);
|
||||||
|
|
||||||
scan_directory(APP_PATH . 'rpc', 'Rpc');
|
scan_directory(APP_PATH . 'rpc', 'app\Rpc');
|
||||||
|
|
||||||
$this->eventProvider->on(OnWorkerStart::class, [$this, 'consulWatches']);
|
$this->eventProvider->on(OnWorkerStart::class, [$this, 'consulWatches']);
|
||||||
|
$this->eventProvider->on(OnWorkerExit::class, [$this, 'onWorkerExit']);
|
||||||
$this->eventProvider->on(OnServerBeforeStart::class, [$this, 'register']);
|
$this->eventProvider->on(OnServerBeforeStart::class, [$this, 'register']);
|
||||||
|
|
||||||
$this->manager = Kiri::getDi()->get(RpcManager::class);
|
$this->manager = Kiri::getDi()->get(RpcManager::class);
|
||||||
@@ -94,16 +96,22 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$async_time = (int)Config::get('consul.async_time', 1000);
|
$async_time = (int)Config::get('consul.async_time', 1000);
|
||||||
Timer::tick($async_time, static function ($timeId) {
|
$this->timerId = Timer::tick($async_time, static function () {
|
||||||
if (env('state', 'start') == 'exit') {
|
|
||||||
Timer::clear($timeId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Kiri::getDi()->get(RpcManager::class)->tick();
|
Kiri::getDi()->get(RpcManager::class)->tick();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param OnWorkerExit $exit
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function onWorkerExit(OnWorkerExit $exit)
|
||||||
|
{
|
||||||
|
Timer::clear($this->timerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param OnServerBeforeStart $server
|
* @param OnServerBeforeStart $server
|
||||||
*/
|
*/
|
||||||
|
|||||||
+36
-8
@@ -2,11 +2,12 @@
|
|||||||
|
|
||||||
namespace Kiri\Rpc;
|
namespace Kiri\Rpc;
|
||||||
|
|
||||||
use Kiri\Message\Handler\Handler;
|
use Exception;
|
||||||
|
use Kiri;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Consul\Agent;
|
use Kiri\Consul\Agent;
|
||||||
use Kiri\Consul\Health;
|
use Kiri\Consul\Health;
|
||||||
use Kiri\Kiri;
|
use Kiri\Message\Handler\Handler;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
|
|
||||||
class RpcManager extends Component
|
class RpcManager extends Component
|
||||||
@@ -22,11 +23,12 @@ class RpcManager extends Component
|
|||||||
/**
|
/**
|
||||||
* @param $serviceName
|
* @param $serviceName
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ReflectionException
|
* @throws Exception
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
public function async($serviceName): void
|
public function async($serviceName): void
|
||||||
{
|
{
|
||||||
|
$this->reRegister($serviceName);
|
||||||
|
|
||||||
$lists = Kiri::getDi()->get(Health::class)->setQuery('passing=true')->service($serviceName);
|
$lists = Kiri::getDi()->get(Health::class)->setQuery('passing=true')->service($serviceName);
|
||||||
if ($lists->getStatusCode() != 200) {
|
if ($lists->getStatusCode() != 200) {
|
||||||
return;
|
return;
|
||||||
@@ -42,12 +44,37 @@ class RpcManager extends Component
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ReflectionException
|
* @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;
|
||||||
|
}
|
||||||
|
$service->service->register($config['config']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function tick(): void
|
public function tick(): void
|
||||||
{
|
{
|
||||||
foreach ($this->_rpc as $name => $list) {
|
try {
|
||||||
$this->async($name);
|
foreach ($this->_rpc as $name => $list) {
|
||||||
|
$this->async($name);
|
||||||
|
}
|
||||||
|
} catch (\Throwable $throwable) {
|
||||||
|
$this->logger()->error(error_trigger_format($throwable));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +82,7 @@ class RpcManager extends Component
|
|||||||
/**
|
/**
|
||||||
* @param $serviceName
|
* @param $serviceName
|
||||||
* @return array
|
* @return array
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getServices($serviceName): array
|
public function getServices($serviceName): array
|
||||||
{
|
{
|
||||||
@@ -117,6 +144,7 @@ class RpcManager extends Component
|
|||||||
{
|
{
|
||||||
$agent = Kiri::getDi()->get(Agent::class);
|
$agent = Kiri::getDi()->get(Agent::class);
|
||||||
foreach ($this->_rpc as $list) {
|
foreach ($this->_rpc as $list) {
|
||||||
|
$agent->service->deregister($list['config']['ID']);
|
||||||
$data = $agent->service->register($list['config']);
|
$data = $agent->service->register($list['config']);
|
||||||
if ($data->getStatusCode() != 200) {
|
if ($data->getStatusCode() != 200) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user