diff --git a/src/Annotation/JsonRpc.php b/src/Annotation/JsonRpc.php index 6e3ba31..ea0cd21 100644 --- a/src/Annotation/JsonRpc.php +++ b/src/Annotation/JsonRpc.php @@ -3,6 +3,10 @@ namespace Kiri\Rpc\Annotation; use Annotation\Attribute; +use Kiri\Abstracts\Config; +use Kiri\Consul\Agent; +use Kiri\Exception\ConfigException; +use Kiri\Kiri; use Kiri\Rpc\RpcManager; use ReflectionException; @@ -12,10 +16,15 @@ use ReflectionException; /** * @param string $method - * @param string $version - * @param string $protocol + * @param string $driver + * @param array $checkOptions */ - public function __construct(public string $method, public string $version = '2.0', public string $protocol = 'json') + public function __construct(public string $method, public string $driver, public array $checkOptions = [ + "DeregisterCriticalServiceAfter" => "1m", + "Http" => "http://127.0.0.1:9527", + "Interval" => "1s", + "Timeout" => "1s" + ]) { } @@ -26,10 +35,39 @@ use ReflectionException; * @param mixed|string $method * @return mixed * @throws ReflectionException + * @throws ConfigException */ public function execute(mixed $class, mixed $method = ''): bool { - return RpcManager::add($this->method, $class); + $default = $this->create(); + $agent = Kiri::getDi()->get(Agent::class); + $data = $agent->service->register($default); + if ($data->getStatusCode() != 200) { + exit($data->getBody()->getContents()); + } + return RpcManager::add($this->method, $class, $default['id']); + } + + + /** + * @throws ConfigException + */ + protected function create(): array + { + $content = swoole_get_local_ip()['eth0']; + return [ + "id" => uniqid("rpc.json.{$this->method}."), + "name" => $this->method, + "address" => swoole_get_local_ip()['eth0'], + "port" => 9526, + "enableTagOverride" => true, + "check" => [ + "DeregisterCriticalServiceAfter" => "1m", + "TCP" => $content . ":" . Config::get('rpc.port'), + "Interval" => "1s", + "Timeout" => "1s" + ] + ]; } diff --git a/src/RpcJsonp.php b/src/RpcJsonp.php index a1ca7fd..fd5f596 100644 --- a/src/RpcJsonp.php +++ b/src/RpcJsonp.php @@ -59,16 +59,14 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa /** * @param OnBeforeShutdown $beforeShutdown - * @throws ConfigException */ public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown) { - $config = Config::get('rpc.registry.config'); - - $config = array_change_key_case($config, CASE_LOWER); - + $doneList = RpcManager::doneList(); $agent = $this->container->get(Agent::class); - $agent->service->deregister($config['id']); + foreach ($doneList as $value) { + $agent->service->deregister($value); + } } @@ -173,7 +171,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa private function dispatch($data): array { try { - [$handler, $params] = RpcManager::get($data['service'], $data['method']); + [$handler, $params, $_] = RpcManager::get($data['service'], $data['method']); if (is_null($handler)) { throw new \Exception('Method not found', -32601); } else { diff --git a/src/RpcManager.php b/src/RpcManager.php index b07e62b..072aaf2 100644 --- a/src/RpcManager.php +++ b/src/RpcManager.php @@ -14,10 +14,11 @@ class RpcManager /** * @param string $name * @param string $class + * @param string $serviceId * @return bool * @throws ReflectionException */ - public static function add(string $name, string $class): bool + public static function add(string $name, string $class, string $serviceId): bool { $methods = Kiri::getDi()->getReflect($class); $lists = $methods->getMethods(\ReflectionMethod::IS_PUBLIC); @@ -27,12 +28,26 @@ class RpcManager foreach ($lists as $reflection) { $methodName = $reflection->getName(); - static::$_rpc[$name][$methodName] = [[$class, $methodName], null]; + static::$_rpc[$name][$methodName] = [[$class, $methodName], null, $serviceId]; } return true; } + public static function doneList(): array + { + $array = []; + foreach (static::$_rpc as $list) { + + foreach ($list as $value) { + $array[] = $value[2]; + } + + } + return $array; + } + + /** * @param string $name * @param string $method @@ -40,7 +55,7 @@ class RpcManager */ public static function get(string $name, string $method): array { - return static::$_rpc[$name][$method] ?? [null, null]; + return static::$_rpc[$name][$method] ?? [null, null, null]; } }