From ee843711b780bc39d8e4ce71ca34297dcad12b21 Mon Sep 17 00:00:00 2001 From: zxyfaxcn Date: Sun, 26 Jul 2020 14:15:00 +0800 Subject: [PATCH] Fixed bug that custom processes cannot fetch config from nacos. (#2149) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加支持配置同步更新至用户自定义进程 * 修复心跳namespaceId参数缺少导致nacos心跳至public * 调整服务、实例创建注册顺序 Co-authored-by: 李铭昕 <715557344@qq.com> --- CHANGELOG-2.0.md | 1 + src/nacos/publish/nacos.php | 2 ++ src/nacos/src/Api/NacosInstance.php | 3 ++- src/nacos/src/Config/FetchConfigProcess.php | 11 +++++++++++ src/nacos/src/Config/OnPipeMessageListener.php | 2 ++ src/nacos/src/Listener/MainWorkerStartListener.php | 14 +++++++------- src/nacos/src/Model/AbstractModel.php | 2 +- 7 files changed, 26 insertions(+), 9 deletions(-) diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index e4102cef2..5f3193db9 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -12,6 +12,7 @@ ## Fixed +- [#2149](https://github.com/hyperf/hyperf/pull/2149) Fixed bug that custom processes cannot fetch config from nacos. - [#2159](https://github.com/hyperf/hyperf/pull/2159) Fixed fatal exception caused by exist file when using `gen:migration`. ## Optimized diff --git a/src/nacos/publish/nacos.php b/src/nacos/publish/nacos.php index 665ea29b2..28c6b6e76 100644 --- a/src/nacos/publish/nacos.php +++ b/src/nacos/publish/nacos.php @@ -10,6 +10,7 @@ declare(strict_types=1); * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ return [ + // The nacos host info 'host' => '127.0.0.1', 'port' => 8848, // The service info. @@ -36,6 +37,7 @@ return [ 'listener_config' => [ // dataId, group, tenant, type, content //[ + // 'tenant' => 'tenant', // corresponding with service.namespaceId // 'data_id' => 'hyperf-service-config', // 'group' => 'DEFAULT_GROUP', //], diff --git a/src/nacos/src/Api/NacosInstance.php b/src/nacos/src/Api/NacosInstance.php index a73bb38f4..e7d205143 100644 --- a/src/nacos/src/Api/NacosInstance.php +++ b/src/nacos/src/Api/NacosInstance.php @@ -99,7 +99,8 @@ class NacosInstance extends AbstractNacos $serviceName = $serviceModel->serviceName; $groupName = $serviceModel->groupName; $ephemeral = $instanceModel->ephemeral; - $params = array_filter(compact('serviceName', 'groupName', 'ephemeral'), function ($item) { + $namespaceId = $instanceModel->namespaceId; + $params = array_filter(compact('serviceName', 'groupName', 'ephemeral', 'namespaceId'), function ($item) { return $item !== null; }); $params['beat'] = $instanceModel->toJson(); diff --git a/src/nacos/src/Config/FetchConfigProcess.php b/src/nacos/src/Config/FetchConfigProcess.php index 5a5f5513e..fe7c530bf 100644 --- a/src/nacos/src/Config/FetchConfigProcess.php +++ b/src/nacos/src/Config/FetchConfigProcess.php @@ -14,6 +14,7 @@ namespace Hyperf\Nacos\Config; use Hyperf\Contract\ConfigInterface; use Hyperf\Nacos\Client; use Hyperf\Process\AbstractProcess; +use Hyperf\Process\ProcessCollector; use Swoole\Coroutine\Server as CoServer; use Swoole\Server; @@ -48,6 +49,16 @@ class FetchConfigProcess extends AbstractProcess for ($workerId = 0; $workerId <= $workerCount; ++$workerId) { $this->server->sendMessage($pipeMessage, $workerId); } + + $processes = ProcessCollector::all(); + if ($processes) { + $string = serialize($pipeMessage); + /** @var \Swoole\Process $process */ + foreach ($processes as $process) { + $process->exportSocket()->send($string); + } + } + $cache = $remoteConfig; } sleep((int) $config->get('nacos.config_reload_interval', 3)); diff --git a/src/nacos/src/Config/OnPipeMessageListener.php b/src/nacos/src/Config/OnPipeMessageListener.php index 9eb091acd..778e64ba9 100644 --- a/src/nacos/src/Config/OnPipeMessageListener.php +++ b/src/nacos/src/Config/OnPipeMessageListener.php @@ -14,6 +14,7 @@ namespace Hyperf\Nacos\Config; use Hyperf\Contract\ConfigInterface; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Framework\Event\OnPipeMessage; +use Hyperf\Process\Event\PipeMessage as UserProcessPipMessage; use Psr\Container\ContainerInterface; class OnPipeMessageListener implements ListenerInterface @@ -35,6 +36,7 @@ class OnPipeMessageListener implements ListenerInterface { return [ OnPipeMessage::class, + UserProcessPipMessage::class, ]; } diff --git a/src/nacos/src/Listener/MainWorkerStartListener.php b/src/nacos/src/Listener/MainWorkerStartListener.php index cf8aa5e53..59c721ac8 100644 --- a/src/nacos/src/Listener/MainWorkerStartListener.php +++ b/src/nacos/src/Listener/MainWorkerStartListener.php @@ -56,13 +56,6 @@ class MainWorkerStartListener implements ListenerInterface return; } - $instance = $this->container->get(Instance::class); - $nacosInstance = $this->container->get(NacosInstance::class); - if (! $nacosInstance->register($instance)) { - throw new RuntimeException(sprintf('nacos register instance fail: %s', $instance)); - } - $this->logger->info('nacos register instance success.', compact('instance')); - $nacosService = $this->container->get(NacosService::class); $service = $this->container->get(Service::class); $exist = $nacosService->detail($service); @@ -71,6 +64,13 @@ class MainWorkerStartListener implements ListenerInterface } $this->logger->info('nacos register service success.', compact('service')); + $instance = $this->container->get(Instance::class); + $nacosInstance = $this->container->get(NacosInstance::class); + if (! $nacosInstance->register($instance)) { + throw new RuntimeException(sprintf('nacos register instance fail: %s', $instance)); + } + $this->logger->info('nacos register instance success.', compact('instance')); + $client = $this->container->get(Client::class); $config = $this->container->get(ConfigInterface::class); $appendNode = $config->get('nacos.config_append_node'); diff --git a/src/nacos/src/Model/AbstractModel.php b/src/nacos/src/Model/AbstractModel.php index e89a936c1..79b6e9b1c 100644 --- a/src/nacos/src/Model/AbstractModel.php +++ b/src/nacos/src/Model/AbstractModel.php @@ -41,7 +41,7 @@ abstract class AbstractModel implements Arrayable $params = array_filter(get_object_vars($this), function ($item) { return $item !== null; }); - unset($params['required_field']); + unset($params['requiredFields']); $intersect = array_intersect(array_keys($params), $this->requiredFields); sort($this->requiredFields); sort($intersect);