diff --git a/Annotation/Rpc/RpcClient.php b/Annotation/Rpc/RpcClient.php index f836a945..3ce5c4f6 100644 --- a/Annotation/Rpc/RpcClient.php +++ b/Annotation/Rpc/RpcClient.php @@ -33,8 +33,8 @@ use Snowflake\Snowflake; public function __construct( public string $cmd, public int $port, - public int $timeout = 1, - public int $mode = SWOOLE_SOCK_TCP6 + public int $timeout, + public int $mode ) { $this->config = ['port' => $port, 'mode' => $mode, 'timeout' => $timeout]; @@ -54,10 +54,6 @@ use Snowflake\Snowflake; $rpc = Snowflake::app()->getRpc(); $rpc->addProducer($this->cmd, $handler, $this->config); - if ($handler[0] instanceof IProducer) { - $handler[0]->initClient(); - } - return parent::execute($handler); } diff --git a/Rpc/Client.php b/Rpc/Client.php index 202f2b8d..53c230cf 100644 --- a/Rpc/Client.php +++ b/Rpc/Client.php @@ -6,9 +6,7 @@ namespace Rpc; use Exception; use Snowflake\Abstracts\Component; -use Swoole; use Snowflake\Core\Json; -use Snowflake\Exception\ConfigException; use Swoole\Coroutine\Client as CClient; diff --git a/Rpc/Producer.php b/Rpc/Producer.php index a225e6f4..3f063080 100644 --- a/Rpc/Producer.php +++ b/Rpc/Producer.php @@ -16,49 +16,49 @@ use Snowflake\Snowflake; class Producer extends Component { - private array $producers = []; + private array $producers = []; - private array $classAlias = []; + private array $classAlias = []; - private array $consumers = []; + private array $consumers = []; - private array $cmds = []; + private array $cods = []; - /** - * @param string $name - * @param array $handler - * @param array $node - */ - public function addProducer(string $name, array $handler, array $node) - { - $this->classAlias[get_class($handler[0])] = $name; + /** + * @param string $name + * @param array $handler + * @param array $node + */ + public function addProducer(string $name, array $handler, array $node) + { + $this->classAlias[get_class($handler[0])] = $name; - $this->consumers[$name] = $handler[0]; + $this->consumers[$name] = $handler[0]; - $this->producers[$name] = $node; - } + $this->producers[$name] = $node; + } /** * @param string $cmd * @param array $handler */ - public function addConsumer(string $cmd, array $handler) - { - $class = get_class($handler[0]); + public function addConsumer(string $cmd, array $handler) + { + $class = get_class($handler[0]); - if (!isset($this->classAlias[$class])) { - return; - } + if (!isset($this->classAlias[$class])) { + return; + } - $name = $this->classAlias[$class]; + $name = $this->classAlias[$class]; - $this->cmds[$name . '.' . $cmd] = $handler; - } + $this->cods[$name . '.' . $cmd] = $handler; + } /** @@ -66,70 +66,74 @@ class Producer extends Component * @param mixed ...$params * @return mixed */ - public function dispatch($cmd, mixed ...$params): mixed - { - $handler = $this->cmds[$cmd] ?? null; - if (empty($handler)) { - return false; - } - return call_user_func($handler, ...$params); - } + public function dispatch($cmd, mixed ...$params): mixed + { + $handler = $this->cods[$cmd] ?? null; + if (empty($handler)) { + return false; + } + return call_user_func($handler, ...$params); + } - /** - * @param $name - * @return mixed - * @throws Exception - */ - public function get($name): mixed - { - if (!isset($this->consumers[$name])) { - throw new Exception('Unknown rpc client.'); - } - return $this->consumers[$name]; - } + /** + * @param $name + * @return mixed + * @throws Exception + */ + public function get($name): mixed + { + if (!isset($this->consumers[$name])) { + throw new Exception('Unknown rpc client.'); + } + return $this->consumers[$name]; + } - /** - * @param string $name - * @return mixed - * @throws Exception - */ - public function getClient(string $name): Client - { - if (!is_array($producer = $this->producers[$name] ?? null)) { - throw new Exception('Unknown rpc client config.'); - } - $producerName = $this->getName($name, $producer); + /** + * @param string $name + * @return mixed + * @throws Exception + */ + public function getClient(string $name): Client + { + if (!is_array($producer = $this->producers[$name] ?? null)) { + throw new Exception('Unknown rpc client config.'); + } + $producerName = $this->getName($name, $producer); - $snowflake = Snowflake::app(); - if (!$snowflake->has($producerName)) { - return $snowflake->set($producerName, ['class' => Client::class, 'config' => $producer]); - } else { - return $snowflake->get($producerName); - } - } + $snowflake = Snowflake::app(); + if (!$snowflake->has($producerName)) { + return $snowflake->set($producerName, [ + 'class' => Client::class, + 'service' => $name, + 'config' => $producer + ]); + } else { + return $snowflake->get($producerName); + } + } - /** - * @param $name - * @return Client|bool - * @throws Exception - */ - public function __get($name): Client|bool - { - return $this->get($name); // TODO: Change the autogenerated stub - } + /** + * @param $name + * @return Client|bool + * @throws Exception + */ + public function __get($name): Client|bool + { + return $this->get($name); // TODO: Change the autogenerated stub + } - /** - * @param $name - * @param $rand - * @return string - */ - private function getName($name, $rand): string - { - return 'rpc.client.' . $name . '.' . $rand['host']; - } + /** + * @param $name + * @param $rand + * @return string + */ + private function getName($name, $rand): string + { + return 'rpc.client.' . $name . '.' . $rand['host']; + } }