From 3994b4faca0a14b34d2416046204a4dbf233c741 Mon Sep 17 00:00:00 2001 From: ppanphper Date: Wed, 2 Jul 2025 14:55:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?nacos=20=E5=8F=98=E6=9B=B4=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E5=A2=9E=E5=8A=A0=E5=8F=98=E6=9B=B4=E5=89=8D=E7=9A=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=AF=B9=E8=B1=A1=EF=BC=8C=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E7=94=A8=E6=9D=A5=E5=88=A4=E6=96=AD=E5=93=AA=E4=BA=9B=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8F=98=E6=9B=B4=E8=BF=9B=E8=A1=8C=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contrib/config/nacos/nacos.go | 17 ++++++++++------- contrib/config/nacos/nacos_test.go | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/contrib/config/nacos/nacos.go b/contrib/config/nacos/nacos.go index a952fb3fb5f..246889910d7 100644 --- a/contrib/config/nacos/nacos.go +++ b/contrib/config/nacos/nacos.go @@ -23,11 +23,11 @@ import ( // Config is the configuration object for nacos client. type Config struct { - ServerConfigs []constant.ServerConfig `v:"required"` // See constant.ServerConfig - ClientConfig constant.ClientConfig `v:"required"` // See constant.ClientConfig - ConfigParam vo.ConfigParam `v:"required"` // See vo.ConfigParam - Watch bool // Watch watches remote configuration updates, which updates local configuration in memory immediately when remote configuration changes. - OnConfigChange func(namespace, group, dataId, data string) // Configure change callback function + ServerConfigs []constant.ServerConfig `v:"required"` // See constant.ServerConfig + ClientConfig constant.ClientConfig `v:"required"` // See constant.ClientConfig + ConfigParam vo.ConfigParam `v:"required"` // See vo.ConfigParam + Watch bool // Watch watches remote configuration updates, which updates local configuration in memory immediately when remote configuration changes. + OnConfigChange func(beforeValue *g.Var, namespace, group, dataId, data string) // Configure change callback function } // Client implements gcfg.Adapter implementing using nacos service. @@ -127,9 +127,12 @@ func (c *Client) addWatcher() error { return nil } c.config.ConfigParam.OnChange = func(namespace, group, dataId, data string) { - c.doUpdate(data) if c.config.OnConfigChange != nil { - go c.config.OnConfigChange(namespace, group, dataId, data) + beforeValue := g.NewVar(gjson.New(c.value.Val().(*gjson.Json).String())) + c.doUpdate(data) + go c.config.OnConfigChange(beforeValue, namespace, group, dataId, data) + } else { + _ = c.doUpdate(data) } } diff --git a/contrib/config/nacos/nacos_test.go b/contrib/config/nacos/nacos_test.go index 473a8174786..355244dec66 100644 --- a/contrib/config/nacos/nacos_test.go +++ b/contrib/config/nacos/nacos_test.go @@ -69,7 +69,7 @@ func TestNacosOnConfigChangeFunc(t *testing.T) { ClientConfig: clientConfig, ConfigParam: configParam, Watch: true, - OnConfigChange: func(namespace, group, dataId, data string) { + OnConfigChange: func(beforeValue *g.Var, namespace, group, dataId, data string) { gtest.Assert("public", namespace) gtest.Assert("test", group) gtest.Assert("config.toml", dataId) From 04a54788b8d5b549fba6134bc18f269854965c73 Mon Sep 17 00:00:00 2001 From: ppanphper Date: Wed, 2 Jul 2025 15:01:32 +0800 Subject: [PATCH 2/2] adjust onConfigChange variable name --- contrib/config/nacos/nacos.go | 14 +++++++------- contrib/config/nacos/nacos_test.go | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/contrib/config/nacos/nacos.go b/contrib/config/nacos/nacos.go index 246889910d7..c764ca7bfc5 100644 --- a/contrib/config/nacos/nacos.go +++ b/contrib/config/nacos/nacos.go @@ -23,11 +23,11 @@ import ( // Config is the configuration object for nacos client. type Config struct { - ServerConfigs []constant.ServerConfig `v:"required"` // See constant.ServerConfig - ClientConfig constant.ClientConfig `v:"required"` // See constant.ClientConfig - ConfigParam vo.ConfigParam `v:"required"` // See vo.ConfigParam - Watch bool // Watch watches remote configuration updates, which updates local configuration in memory immediately when remote configuration changes. - OnConfigChange func(beforeValue *g.Var, namespace, group, dataId, data string) // Configure change callback function + ServerConfigs []constant.ServerConfig `v:"required"` // See constant.ServerConfig + ClientConfig constant.ClientConfig `v:"required"` // See constant.ClientConfig + ConfigParam vo.ConfigParam `v:"required"` // See vo.ConfigParam + Watch bool // Watch watches remote configuration updates, which updates local configuration in memory immediately when remote configuration changes. + OnConfigChange func(beforeCfg *g.Var, namespace, group, dataId, data string) // Configure change callback function } // Client implements gcfg.Adapter implementing using nacos service. @@ -128,9 +128,9 @@ func (c *Client) addWatcher() error { } c.config.ConfigParam.OnChange = func(namespace, group, dataId, data string) { if c.config.OnConfigChange != nil { - beforeValue := g.NewVar(gjson.New(c.value.Val().(*gjson.Json).String())) + beforeCfg := g.NewVar(gjson.New(c.value.Val().(*gjson.Json).String())) c.doUpdate(data) - go c.config.OnConfigChange(beforeValue, namespace, group, dataId, data) + go c.config.OnConfigChange(beforeCfg, namespace, group, dataId, data) } else { _ = c.doUpdate(data) } diff --git a/contrib/config/nacos/nacos_test.go b/contrib/config/nacos/nacos_test.go index 355244dec66..a2832f303d8 100644 --- a/contrib/config/nacos/nacos_test.go +++ b/contrib/config/nacos/nacos_test.go @@ -69,7 +69,7 @@ func TestNacosOnConfigChangeFunc(t *testing.T) { ClientConfig: clientConfig, ConfigParam: configParam, Watch: true, - OnConfigChange: func(beforeValue *g.Var, namespace, group, dataId, data string) { + OnConfigChange: func(beforeCfg *g.Var, namespace, group, dataId, data string) { gtest.Assert("public", namespace) gtest.Assert("test", group) gtest.Assert("config.toml", dataId)