From 807e8e25f796c4213dcd9ca0553029c68597cb24 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Tue, 23 Mar 2021 02:29:48 +0800 Subject: [PATCH] modify --- Rpc/Client.php | 82 +++++++++++++++++++++++++++ Rpc/Producer.php | 77 +++++++++++++++++++++++++ Rpc/Service.php | 45 +++++++++++++++ System/Abstracts/TraitApplication.php | 2 + composer.json | 1 + 5 files changed, 207 insertions(+) create mode 100644 Rpc/Client.php create mode 100644 Rpc/Producer.php create mode 100644 Rpc/Service.php diff --git a/Rpc/Client.php b/Rpc/Client.php new file mode 100644 index 00000000..6ab825da --- /dev/null +++ b/Rpc/Client.php @@ -0,0 +1,82 @@ +config = $name; + } + + + /** + * @param string $route + * @param array $param + * @return mixed|string|null + * @throws ConfigException + */ + public function request(string $route, array $param) + { + $service = $this->config; + if (empty($service)) { + return null; + } + if (!($this->client instanceof CClient)) { + $client = $this->getClient(); + } + if (!$this->client->isConnected()) { + if (!$client->connect($service['host'], $service['port'], $service['timeout'])) { + return $client->errCode . ':' . $client->errMsg; + } + } + $isSend = $client->send(serialize(['route' => $route, 'body' => $param])); + if ($isSend === false) { + return $client->errCode . ':' . $client->errMsg; + } + return unserialize($client->recv()); + } + + + /** + * @return mixed + * @throws \Exception + */ + public function getClient(): CClient + { + return objectPool(CClient::class, function () { + $client = new CClient(SWOOLE_SOCK_TCP6); + $client->set([ + 'timeout' => 0.5, + 'connect_timeout' => 1.0, + 'write_timeout' => 10.0, + 'read_timeout' => 0.5, + 'open_tcp_keepalive' => true, + ]); + }); + } + + +} diff --git a/Rpc/Producer.php b/Rpc/Producer.php new file mode 100644 index 00000000..18c99222 --- /dev/null +++ b/Rpc/Producer.php @@ -0,0 +1,77 @@ +producers)) { + $this->producers = Config::get('rpc.producers'); + } + if (empty($this->producers)) { + return $this->addError('Empty by rpc service'); + } + if (!isset($this->producers[$name])) { + return $this->addError('Unknown rpc service'); + } + $rand = $this->producers[$name][array_rand($this->producers[$name])]; + + if (Snowflake::app()->has($this->getName($name, $rand))) { + return Snowflake::app()->get($this->getName($name, $rand)); + } + return Snowflake::app()->set($this->getName($name, $rand), [ + 'class' => Client::class, + 'config' => $rand + ]); + } + + + /** + * @param $name + * @return mixed + * @throws \ReflectionException + * @throws \Snowflake\Exception\ComponentException + * @throws \Snowflake\Exception\ConfigException + * @throws \Snowflake\Exception\NotFindClassException + */ + public function __get($name): mixed + { + return $this->get($name); // TODO: Change the autogenerated stub + } + + + /** + * @param $name + * @param $rand + * @return string + */ + private function getName($name, $rand) + { + return 'rpc.client.' . $name . '.' . $rand['host']; + } + +} diff --git a/Rpc/Service.php b/Rpc/Service.php new file mode 100644 index 00000000..214d8e5e --- /dev/null +++ b/Rpc/Service.php @@ -0,0 +1,45 @@ +getSwoole(); + foreach ($services as $service) { + $mode = $service['mode'] ?? SWOOLE_SOCK_TCP6; + $rpcServer = $server->addlistener($service['host'], $service['port'], $mode); + $rpcServer->set([ + 'open_tcp_keepalive' => true, + 'tcp_keepidle' => 30, + 'tcp_keepinterval' => 10, + 'tcp_keepcount' => 10, + 'open_http_protocol' => false, + 'open_websocket_protocol' => false, + ]); + } + } + + +} diff --git a/System/Abstracts/TraitApplication.php b/System/Abstracts/TraitApplication.php index 51a12069..f773e892 100644 --- a/System/Abstracts/TraitApplication.php +++ b/System/Abstracts/TraitApplication.php @@ -22,6 +22,7 @@ use Snowflake\Event; use Snowflake\Jwt\Jwt; use Snowflake\Pool\Connection; use Snowflake\Pool\Pool as SPool; +use Rpc\Producer as RPCProducer; /** * Trait TraitApplication @@ -46,6 +47,7 @@ use Snowflake\Pool\Pool as SPool; * @property Curl $curl * @property Crontab $crontab * @property HttpFilter $filter + * @property RPCProducer $rpc */ trait TraitApplication { diff --git a/composer.json b/composer.json index e5afb439..5fc24036 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "Database\\": "Database/", "Gii\\": "Gii/", "Kafka\\": "Kafka/", + "Rpc\\": "Rpc/", "Annotation\\": "Annotation/" }, "files": [