diff --git a/contrib/config/nacos/nacos.go b/contrib/config/nacos/nacos.go index a952fb3fb5f..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(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. @@ -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) + beforeCfg := g.NewVar(gjson.New(c.value.Val().(*gjson.Json).String())) + c.doUpdate(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 473a8174786..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(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)