Skip to content

Commit e3e82c7

Browse files
authored
feat(contrib/config/nacos): add OnChange callbacks configuration support (#4038)
1 parent ced4b57 commit e3e82c7

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

contrib/config/nacos/nacos.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import (
2323

2424
// Config is the configuration object for nacos client.
2525
type Config struct {
26-
ServerConfigs []constant.ServerConfig `v:"required"` // See constant.ServerConfig
27-
ClientConfig constant.ClientConfig `v:"required"` // See constant.ClientConfig
28-
ConfigParam vo.ConfigParam `v:"required"` // See vo.ConfigParam
29-
Watch bool // Watch watches remote configuration updates, which updates local configuration in memory immediately when remote configuration changes.
26+
ServerConfigs []constant.ServerConfig `v:"required"` // See constant.ServerConfig
27+
ClientConfig constant.ClientConfig `v:"required"` // See constant.ClientConfig
28+
ConfigParam vo.ConfigParam `v:"required"` // See vo.ConfigParam
29+
Watch bool // Watch watches remote configuration updates, which updates local configuration in memory immediately when remote configuration changes.
30+
OnConfigChange func(namespace, group, dataId, data string) // Configure change callback function
3031
}
3132

3233
// Client implements gcfg.Adapter implementing using nacos service.
@@ -125,9 +126,11 @@ func (c *Client) addWatcher() error {
125126
if !c.config.Watch {
126127
return nil
127128
}
128-
129129
c.config.ConfigParam.OnChange = func(namespace, group, dataId, data string) {
130130
c.doUpdate(data)
131+
if c.config.OnConfigChange != nil {
132+
go c.config.OnConfigChange(namespace, group, dataId, data)
133+
}
131134
}
132135

133136
if err := c.client.ListenConfig(c.config.ConfigParam); err != nil {

contrib/config/nacos/nacos_test.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
package nacos_test
88

99
import (
10+
"net/url"
1011
"testing"
12+
"time"
1113

1214
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
1315
"github.com/nacos-group/nacos-sdk-go/v2/vo"
1416

17+
"github.com/gogf/gf/v2/encoding/gjson"
1518
"github.com/gogf/gf/v2/frame/g"
1619
"github.com/gogf/gf/v2/os/gctx"
1720
"github.com/gogf/gf/v2/test/gtest"
@@ -34,6 +37,7 @@ var (
3437
DataId: "config.toml",
3538
Group: "test",
3639
}
40+
configPublishUrl = "http://localhost:8848/nacos/v2/cs/config?type=toml&namespaceId=public&group=test&dataId=config.toml"
3741
)
3842

3943
func TestNacos(t *testing.T) {
@@ -48,7 +52,6 @@ func TestNacos(t *testing.T) {
4852
config.SetAdapter(adapter)
4953

5054
t.Assert(config.Available(ctx), true)
51-
5255
v, err := config.Get(ctx, `server.address`)
5356
t.AssertNil(err)
5457
t.Assert(v.String(), ":8000")
@@ -58,3 +61,41 @@ func TestNacos(t *testing.T) {
5861
t.AssertGT(len(m), 0)
5962
})
6063
}
64+
65+
func TestNacosOnConfigChangeFunc(t *testing.T) {
66+
gtest.C(t, func(t *gtest.T) {
67+
adapter, _ := nacos.New(ctx, nacos.Config{
68+
ServerConfigs: []constant.ServerConfig{serverConfig},
69+
ClientConfig: clientConfig,
70+
ConfigParam: configParam,
71+
Watch: true,
72+
OnConfigChange: func(namespace, group, dataId, data string) {
73+
gtest.Assert("public", namespace)
74+
gtest.Assert("test", group)
75+
gtest.Assert("config.toml", dataId)
76+
gtest.Assert("gf", g.Cfg().MustGet(gctx.GetInitCtx(), "app.name").String())
77+
},
78+
})
79+
g.Cfg().SetAdapter(adapter)
80+
t.Assert(g.Cfg().Available(ctx), true)
81+
appName, err := g.Cfg().Get(ctx, "app.name")
82+
t.AssertNil(err)
83+
t.Assert(appName.String(), "")
84+
c, err := g.Cfg().Data(ctx)
85+
t.AssertNil(err)
86+
j := gjson.New(c)
87+
err = j.Set("app.name", "gf")
88+
t.AssertNil(err)
89+
res, err := j.ToTomlString()
90+
t.AssertNil(err)
91+
_, err = g.Client().Post(ctx, configPublishUrl+"&content="+url.QueryEscape(res))
92+
t.AssertNil(err)
93+
time.Sleep(5 * time.Second)
94+
err = j.Remove("app")
95+
t.AssertNil(err)
96+
res2, err := j.ToTomlString()
97+
t.AssertNil(err)
98+
_, err = g.Client().Post(ctx, configPublishUrl+"&content="+url.QueryEscape(res2))
99+
t.AssertNil(err)
100+
})
101+
}

0 commit comments

Comments
 (0)