This commit is contained in:
2021-12-01 19:04:59 +08:00
parent b279e56efd
commit 7fef63fa39
3 changed files with 64 additions and 8 deletions
+3 -2
View File
@@ -2,6 +2,7 @@
namespace Kiri\Rpc\Note; namespace Kiri\Rpc\Note;
use Kiri\Consul\Catalog\Catalog;
use Note\Attribute; use Note\Attribute;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Consul\Agent; use Kiri\Consul\Agent;
@@ -40,8 +41,8 @@ use ReflectionException;
public function execute(mixed $class, mixed $method = ''): bool public function execute(mixed $class, mixed $method = ''): bool
{ {
$default = $this->create(); $default = $this->create();
$agent = Kiri::getDi()->get(Agent::class); $agent = Kiri::getDi()->get(Catalog::class);
$data = $agent->service->register($default); $data = $agent->register($default);
if ($data->getStatusCode() != 200) { if ($data->getStatusCode() != 200) {
exit($data->getBody()->getContents()); exit($data->getBody()->getContents());
} }
+17 -6
View File
@@ -2,19 +2,19 @@
namespace Kiri\Rpc; namespace Kiri\Rpc;
use Note\Note;
use Note\Inject;
use Http\Constrict\RequestInterface; use Http\Constrict\RequestInterface;
use Http\Handler\Router; use Http\Handler\Router;
use Http\Message\ServerRequest; use Http\Message\ServerRequest;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Consul\Agent; use Kiri\Consul\Agent;
use Kiri\Consul\Catalog\Catalog;
use Kiri\Context; use Kiri\Context;
use Kiri\Core\Json;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Kiri; use Kiri\Kiri;
use Note\Inject;
use Note\Note;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
@@ -23,7 +23,7 @@ use Server\Contract\OnCloseInterface;
use Server\Contract\OnConnectInterface; use Server\Contract\OnConnectInterface;
use Server\Contract\OnReceiveInterface; use Server\Contract\OnReceiveInterface;
use Server\Events\OnBeforeShutdown; use Server\Events\OnBeforeShutdown;
use Server\Events\OnStart; use Server\Events\OnWorkerStart;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Coroutine\Channel; use Swoole\Coroutine\Channel;
use Swoole\Server; use Swoole\Server;
@@ -60,6 +60,8 @@ 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', 'Rpc');
$this->eventProvider->on(OnWorkerStart::class, [$this, 'register']);
} }
@@ -79,15 +81,24 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/** /**
* @param OnStart $server * @param OnWorkerStart $server
* @throws ConfigException * @throws ConfigException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public function register(OnStart $server) public function register(OnWorkerStart $server)
{ {
if ($server->workerId != 0) {
return;
}
$config = Config::get('rpc'); $config = Config::get('rpc');
$catalog = $this->container->get(Catalog::class);
$catalog->register([
]);
$agent = $this->container->get(Agent::class); $agent = $this->container->get(Agent::class);
$data = $agent->service->register($config['registry']['config']); $data = $agent->service->register($config['registry']['config']);
if ($data->getStatusCode() != 200) { if ($data->getStatusCode() != 200) {
+44
View File
@@ -29,5 +29,49 @@ return [
'port' => '' 'port' => ''
] ]
] ]
],
'service' => [
[
"datacenter" => "dc1",
"id" => "40e4a748-2192-161a-0510-9bf59fe950b5",
"node" => "FriendRpcService",
"skipNodeUpdate" => false,
"service" => [
"id" => "redis1",
"service" => "FriendRpcService",
"address" => "172.26.221.211",
"taggedAddresses" => [
"lan" => [
"address" => "127.0.0.1",
"port" => 8000
],
"wan" => [
"address" => "172.26.221.211",
"port" => 80
]
],
"meta" => [
"redis_version" => "4.0"
],
"port" => 8000
],
"check" => [
"node" => "t2.320",
"checkId" => "service:redis1",
"name" => "Redis health check",
"notes" => "Script based health check",
"status" => "passing",
"serviceID" => "redis1",
"definition" => [
"http" => "172.26.221.211:9527",
"interval" => "5s",
"timeout" => "1s",
"deregisterCriticalServiceAfter" => "30s"
],
],
]
] ]
]; ];