From f69666fd885297fb39341a4387419565b999a844 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Mon, 10 Jul 2023 15:28:12 +0800 Subject: [PATCH] Support grpc for nacos v2 (#278) Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- composer.json | 5 +++++ publish/confd.php | 5 +++++ src/Driver/Nacos.php | 7 ++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 94947e9..a7225c4 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,11 @@ "hyperf/framework": "~3.0.0", "hyperf/support": "~3.0.1" }, + "suggests": { + "google/protobuf": "Required to use nacos v2 grpc apis.(~3.0.0)", + "hyperf/grpc": "Required to use nacos v2 grpc apis.(~3.0.0)", + "hyperf/http2-client": "Required to use nacos v2 grpc apis.(~3.0.0)" + }, "autoload": { "psr-4": { "FriendsOfHyperf\\Confd\\": "src/" diff --git a/publish/confd.php b/publish/confd.php index 5f249d8..cfe4699 100644 --- a/publish/confd.php +++ b/publish/confd.php @@ -38,6 +38,11 @@ 'guzzle' => [ 'config' => ['timeout' => 3, 'connect_timeout' => 1], ], + // Only support for nacos v2. + 'grpc' => [ + 'enable' => false, + 'heartbeat' => 10, + ], ], 'listener_config' => [ 'mysql' => [ diff --git a/src/Driver/Nacos.php b/src/Driver/Nacos.php index bd412a4..daa7819 100644 --- a/src/Driver/Nacos.php +++ b/src/Driver/Nacos.php @@ -17,6 +17,7 @@ use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Nacos\Application; use Hyperf\Nacos\Config; +use InvalidArgumentException; use Psr\Container\ContainerInterface; use function Hyperf\Collection\collect; @@ -28,7 +29,11 @@ class Nacos implements DriverInterface public function __construct(private ContainerInterface $container, private ConfigInterface $config, private StdoutLoggerInterface $logger) { - $config = $this->config->get('confd.drivers.nacos.client', []); + $config = $this->config->get('confd.drivers.nacos.client') ?: $this->config->get('nacos', []); + + if (empty($config)) { + throw new InvalidArgumentException('Nacos config is invalid.'); + } $this->client = make(NacosClient::class, [ 'config' => $this->buildNacosConfig($config),