From 0ad17ae4c5256692b88ff6d53881e30c6647ea44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 31 May 2022 11:11:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Annotation/JsonRpc.php | 60 +++--------------------------------------- RpcJsonp.php | 18 +++++++++---- RpcManager.php | 6 +---- 3 files changed, 18 insertions(+), 66 deletions(-) diff --git a/Annotation/JsonRpc.php b/Annotation/JsonRpc.php index 2911ae0..97fc8e9 100644 --- a/Annotation/JsonRpc.php +++ b/Annotation/JsonRpc.php @@ -16,19 +16,13 @@ use ReflectionException; private string $uniqueId = ''; - - + + /** * @param string $service - * @param string $driver - * @param array $tags - * @param array $meta - * @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 string $checkUrl = '') + public function __construct(public string $service) { - $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()))); } @@ -36,58 +30,12 @@ use ReflectionException; * @param mixed $class * @param mixed|string $method * @return mixed - * @throws ConfigException */ public function execute(mixed $class, mixed $method = ''): bool { $manager = Kiri::getDi()->get(RpcManager::class); - return $manager->add($this->service, $class, $this->create()); + return $manager->add($this->service, $class); } - - /** - * @throws ConfigException - */ - protected function create(): array - { - $rpcPort = Config::get('rpc.port'); - if (empty($this->checkUrl)) { - $this->checkUrl = Network::local() . ":" . Config::get('rpc.port'); - } - $defaultConfig = [ - "ID" => "rpc.json.{$this->service}." . $this->uniqueId, - "Name" => $this->service, - "EnableTagOverride" => false, - "TaggedAddresses" => [ - "lan_ipv4" => [ - "address" => "127.0.0.1", - "port" => $rpcPort - ], - "wan_ipv4" => [ - "address" => Network::local(), - "port" => $rpcPort - ] - ], - "Check" => [ - "CheckId" => "service:rpc.json.{$this->service}." . $this->uniqueId, - "Name" => "service " . $this->service . ' health check', - "Notes" => "Script based health check", - "ServiceID" => $this->service, - "TCP" => $this->checkUrl, - "Interval" => "5s", - "Timeout" => "1s", - "DeregisterCriticalServiceAfter" => "30s" - ], - ]; - if (!empty($this->meta)) { - $defaultConfig["Meta"] = $this->meta; - } - if (!empty($this->tags)) { - $defaultConfig["tags"] = $this->tags; - } - return $defaultConfig; - } - - } diff --git a/RpcJsonp.php b/RpcJsonp.php index 5db71b3..382d9ad 100644 --- a/RpcJsonp.php +++ b/RpcJsonp.php @@ -128,13 +128,21 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa { Timer::clear($this->timerId); } - - - /** - * @param OnServerBeforeStart $server - */ + + + /** + * @param OnServerBeforeStart $server + * @throws ConfigException + */ public function register(OnServerBeforeStart $server) { + $consumers = Config::get("rpc.consumers", []); + if (!empty($consumers)) { + $manager = Kiri::getDi()->get(RpcManager::class); + foreach ($consumers as $service => $consumer) { + $manager->add($service, $consumer); + } + } $this->manager->register(); } diff --git a/RpcManager.php b/RpcManager.php index 5ddbecc..8cb8300 100644 --- a/RpcManager.php +++ b/RpcManager.php @@ -77,14 +77,10 @@ class RpcManager extends Component /** * @param string $name * @param string $class - * @param array $serviceConfig * @return bool */ - public function add(string $name, string $class, array $serviceConfig): bool + public function add(string $name, string $class): bool { - if (!isset($this->_rpc[$name])) { -// $this->_rpc[$name] = ['id' => $serviceConfig['ID'], 'config' => $serviceConfig]; - } Router::addServer('rpc', static function () use ($name, $class) { Router::get($name, $class); });