36 Commits

Author SHA1 Message Date
as2252258 4d60a24fac Revert "改名"
This reverts commit fdf58326
2022-01-12 14:10:33 +08:00
as2252258 4dc6bc661a Revert "改名"
This reverts commit fdf58326
2022-01-11 17:56:16 +08:00
as2252258 0d7fd5e356 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:53:41 +08:00
as2252258 da16b0685d Revert "改名"
This reverts commit fdf58326
2022-01-11 14:51:50 +08:00
as2252258 febbdea8c8 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:44:02 +08:00
as2252258 a45c3d875c Revert "改名"
This reverts commit fdf58326
2022-01-11 14:42:04 +08:00
as2252258 64d01c0a80 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:41:06 +08:00
as2252258 bf5fe594e4 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:30:58 +08:00
as2252258 5cfe3c6d6d Revert "改名"
This reverts commit fdf58326
2022-01-11 14:30:05 +08:00
as2252258 773e3c0f57 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:26:57 +08:00
as2252258 b1c91343ef Revert "改名"
This reverts commit fdf58326
2022-01-11 14:25:24 +08:00
as2252258 3f821ca9d0 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:23:48 +08:00
as2252258 d296b3107e Revert "改名"
This reverts commit fdf58326
2022-01-11 14:15:51 +08:00
as2252258 f606c58204 Revert "改名"
This reverts commit fdf58326
2022-01-11 14:13:51 +08:00
as2252258 6c1d986814 Revert "改名"
This reverts commit fdf58326
2022-01-10 11:39:56 +08:00
as2252258 da056b09c7 1 2022-01-09 16:07:57 +08:00
as2252258 4eefe54be0 e 2022-01-09 14:00:32 +08:00
as2252258 b0ed7650c5 Revert "改名"
This reverts commit fdf58326
2022-01-08 18:49:07 +08:00
as2252258 3d89cbd4c6 Revert "改名"
This reverts commit fdf58326
2022-01-05 15:22:29 +08:00
as2252258 0f2d19f3c7 Revert "改名"
This reverts commit fdf58326
2022-01-05 15:21:48 +08:00
as2252258 9001dd041e Revert "改名"
This reverts commit fdf58326
2022-01-04 15:56:37 +08:00
as2252258 1bb26b4680 改名 2021-12-24 14:53:59 +08:00
as2252258 a6d825ef9b 改名 2021-12-24 14:16:07 +08:00
as2252258 6cc0334b9b 改名 2021-12-24 11:55:09 +08:00
as2252258 d6cb6e40a8 改名 2021-12-24 11:53:24 +08:00
as2252258 fb10622960 改名 2021-12-24 11:34:29 +08:00
as2252258 2a9c36babb 改名 2021-12-24 11:31:48 +08:00
as2252258 bc4e506ea8 改名 2021-12-23 18:22:26 +08:00
as2252258 59089462e7 改名 2021-12-08 11:32:32 +08:00
as2252258 845b8b36d1 改名 2021-12-06 17:10:45 +08:00
as2252258 07436f7035 改名 2021-12-06 17:08:03 +08:00
as2252258 4d93fcaff2 改名 2021-12-03 15:42:05 +08:00
as2252258 2e01b60e1e 改名 2021-12-03 15:09:05 +08:00
as2252258 8d2ce1d45c 改名 2021-12-03 15:07:21 +08:00
as2252258 8a3ed5aea5 改名 2021-12-03 14:56:13 +08:00
as2252258 d50b6e6ec7 改名 2021-12-03 14:46:38 +08:00
19 changed files with 1485 additions and 1483 deletions
@@ -1,13 +1,13 @@
<?php <?php
namespace Kiri\Rpc\Note; 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 Note\Attribute; use Kiri\Annotation\Attribute;
use ReflectionException; use ReflectionException;
#[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc extends Attribute #[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc extends Attribute
@@ -23,10 +23,11 @@ use ReflectionException;
* @param array $tags * @param array $tags
* @param array $meta * @param array $meta
* @param array $checkOptions * @param array $checkOptions
* @param string $checkUrl
*/ */
public function __construct(public string $service, public string $driver, public array $tags = [], public array $meta = [], public array $checkOptions = []) public function __construct(public string $service, public string $driver, public array $tags = [], public array $meta = [], public array $checkOptions = [], public string $checkUrl = '')
{ {
$this->uniqueId = preg_replace('/(\w{11})(\w{4})(\w{3})(\w{8})(\w{6})/', '$1-$2-$3-$4-$5', md5(__DIR__ . '.' . md5(Network::local()))); $this->uniqueId = preg_replace('/(\w{11})(\w{4})(\w{3})(\w{8})(\w{6})/', '$1-$2-$3-$4-$5', md5(__DIR__ . 'Annotation' . md5(Network::local())));
} }
@@ -49,6 +50,9 @@ use ReflectionException;
protected function create(): array protected function create(): array
{ {
$rpcPort = Config::get('rpc.port'); $rpcPort = Config::get('rpc.port');
if (empty($this->checkUrl)) {
$this->checkUrl = Network::local() . ":" . Config::get('rpc.port');
}
$defaultConfig = [ $defaultConfig = [
"ID" => "rpc.json.{$this->service}." . $this->uniqueId, "ID" => "rpc.json.{$this->service}." . $this->uniqueId,
"Name" => $this->service, "Name" => $this->service,
@@ -68,7 +72,7 @@ use ReflectionException;
"Name" => "service " . $this->service . ' health check', "Name" => "service " . $this->service . ' health check',
"Notes" => "Script based health check", "Notes" => "Script based health check",
"ServiceID" => $this->service, "ServiceID" => $this->service,
"TCP" => Network::local() . ":" . Config::get('rpc.port'), "TCP" => $this->checkUrl,
"Interval" => "5s", "Interval" => "5s",
"Timeout" => "1s", "Timeout" => "1s",
"DeregisterCriticalServiceAfter" => "30s" "DeregisterCriticalServiceAfter" => "30s"
+1 -1
View File
@@ -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;
@@ -4,10 +4,10 @@ namespace Kiri\Rpc;
use Exception; use Exception;
use Http\Message\ServerRequest; use Kiri\Message\ServerRequest;
use Http\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;
@@ -110,6 +110,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
* @param $service * @param $service
* @return array * @return array
* @throws RpcServiceException|\ReflectionException * @throws RpcServiceException|\ReflectionException
* @throws Exception
*/ */
private function get_consul($service): array private function get_consul($service): array
{ {
@@ -2,10 +2,10 @@
namespace Kiri\Rpc; namespace Kiri\Rpc;
use Note\Inject; use Kiri\Annotation\Inject;
use Exception; use Exception;
use Http\Message\Response; use Kiri\Message\Response;
use Http\Message\Stream; use Kiri\Message\Stream;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
@@ -2,8 +2,8 @@
namespace Kiri\Rpc; namespace Kiri\Rpc;
use Http\Message\Response; use Kiri\Message\Response;
use Http\Message\Stream; use Kiri\Message\Stream;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
View File
+23 -29
View File
@@ -2,29 +2,30 @@
namespace Kiri\Rpc; namespace Kiri\Rpc;
use Http\Constrict\RequestInterface; use Kiri\Message\Constrict\RequestInterface;
use Http\Handler\Handler; use Kiri\Message\Handler\Handler;
use Http\Handler\Router; use Kiri\Message\Handler\Router;
use Http\Message\ServerRequest; use Kiri\Message\ServerRequest;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Kiri\Consul\Agent; use Kiri\Consul\Agent;
use Kiri\Context; use Kiri\Context;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use Kiri\Kiri; use Kiri\Exception\ConfigException;
use Note\Inject; use Kiri;
use Note\Note; use Kiri\Annotation\Inject;
use Kiri\Annotation\Annotation;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use ReflectionException; use ReflectionException;
use Server\Contract\OnCloseInterface; use Kiri\Server\Contract\OnCloseInterface;
use Server\Contract\OnConnectInterface; use Kiri\Server\Contract\OnConnectInterface;
use Server\Contract\OnReceiveInterface; use Kiri\Server\Contract\OnReceiveInterface;
use Server\Events\OnBeforeShutdown; use Kiri\Server\Events\OnBeforeShutdown;
use Server\Events\OnServerBeforeStart; use Kiri\Server\Events\OnServerBeforeStart;
use Server\Events\OnTaskerStart; use Kiri\Server\Events\OnTaskerStart;
use Server\Events\OnWorkerStart; use Kiri\Server\Events\OnWorkerStart;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Coroutine\Channel; use Swoole\Coroutine\Channel;
use Swoole\Server; use Swoole\Server;
@@ -42,17 +43,10 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
public Router $router; public Router $router;
#[Inject(Note::class)] #[Inject(Annotation::class)]
public Note $annotation; public Annotation $annotation;
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
#[Inject(ContainerInterface::class)]
public ContainerInterface $container;
private RpcManager $manager; private RpcManager $manager;
@@ -67,7 +61,6 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
scan_directory(APP_PATH . 'rpc', 'Rpc'); scan_directory(APP_PATH . 'rpc', 'Rpc');
$this->eventProvider->on(OnWorkerStart::class, [$this, 'consulWatches']); $this->eventProvider->on(OnWorkerStart::class, [$this, 'consulWatches']);
$this->eventProvider->on(OnTaskerStart::class, [$this, 'consulWatches']);
$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);
@@ -76,6 +69,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/** /**
* @param OnBeforeShutdown $beforeShutdown * @param OnBeforeShutdown $beforeShutdown
* @return void
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
@@ -92,13 +86,15 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/** /**
* @param OnWorkerStart|OnTaskerStart $server * @param OnWorkerStart|OnTaskerStart $server
* @throws ConfigException
*/ */
public function consulWatches(OnWorkerStart|OnTaskerStart $server) public function consulWatches(OnWorkerStart|OnTaskerStart $server)
{ {
if ($server->workerId != 0) { if ($server->workerId != 0) {
return; return;
} }
Timer::tick(1000, static function ($timeId) { $async_time = (int)Config::get('consul.async_time', 1000);
Timer::tick(5000, static function ($timeId) {
if (env('state', 'start') == 'exit') { if (env('state', 'start') == 'exit') {
Timer::clear($timeId); Timer::clear($timeId);
return; return;
@@ -110,7 +106,6 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/** /**
* @param OnServerBeforeStart $server * @param OnServerBeforeStart $server
* @throws ReflectionException
*/ */
public function register(OnServerBeforeStart $server) public function register(OnServerBeforeStart $server)
{ {
@@ -269,10 +264,9 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/** /**
* @param Server $server
* @param int $fd * @param int $fd
*/ */
public function onClose(Server $server, int $fd): void public function onClose(int $fd): void
{ {
// TODO: Implement onClose() method. // TODO: Implement onClose() method.
} }
+9 -6
View File
@@ -2,13 +2,14 @@
namespace Kiri\Rpc; namespace Kiri\Rpc;
use Http\Handler\Handler; 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;
use Kiri\Message\Handler\Handler;
use ReflectionException; use ReflectionException;
class RpcManager class RpcManager extends Component
{ {
@@ -87,8 +88,10 @@ class RpcManager
} }
foreach ($lists as $reflection) { foreach ($lists as $reflection) {
if ($reflection->getDeclaringClass() != $class) {
continue;
}
$methodName = $reflection->getName(); $methodName = $reflection->getName();
$this->_rpc[$name]['methods'][$methodName] = [new Handler('/', [$class, $methodName]), null]; $this->_rpc[$name]['methods'][$methodName] = [new Handler('/', [$class, $methodName]), null];
} }
return true; return true;
@@ -109,15 +112,15 @@ class RpcManager
/** /**
* @throws ReflectionException
*/ */
public function register() public function register()
{ {
$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) {
exit($data->getBody()); return;
} }
} }
} }
+1 -1
View File
@@ -16,7 +16,7 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Kiri\\Rpc\\": "src/" "Kiri\\Rpc\\": "./"
} }
}, },
"require-dev": { "require-dev": {
+2 -2
View File
@@ -3,7 +3,7 @@
use Kiri\Rpc\RpcJsonp; use Kiri\Rpc\RpcJsonp;
use Kiri\Rpc\TestRpcService; use Kiri\Rpc\TestRpcService;
use Server\Constant; use Kiri\Server\Constant;
return [ return [
'rpc' => [ 'rpc' => [
@@ -61,7 +61,7 @@ return [
"node" => "t2.320", "node" => "t2.320",
"checkId" => "service:redis1", "checkId" => "service:redis1",
"name" => "Redis health check", "name" => "Redis health check",
"notes" => "Script based health check", "Annotations" => "Script based health check",
"status" => "passing", "status" => "passing",
"serviceID" => "redis1", "serviceID" => "redis1",
"definition" => [ "definition" => [