Skip to content

Commit

Permalink
feat(contrib/config/nacos): add OnChange callbacks configuration supp…
Browse files Browse the repository at this point in the history
…ort (#4038)
  • Loading branch information
LanceAdd authored Dec 13, 2024
1 parent ced4b57 commit e3e82c7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
13 changes: 8 additions & 5 deletions contrib/config/nacos/nacos.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +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.
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
}

// Client implements gcfg.Adapter implementing using nacos service.
Expand Down Expand Up @@ -125,9 +126,11 @@ func (c *Client) addWatcher() error {
if !c.config.Watch {
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)
}
}

if err := c.client.ListenConfig(c.config.ConfigParam); err != nil {
Expand Down
43 changes: 42 additions & 1 deletion contrib/config/nacos/nacos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
package nacos_test

import (
"net/url"
"testing"
"time"

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

"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/test/gtest"
Expand All @@ -34,6 +37,7 @@ var (
DataId: "config.toml",
Group: "test",
}
configPublishUrl = "http://localhost:8848/nacos/v2/cs/config?type=toml&namespaceId=public&group=test&dataId=config.toml"
)

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

t.Assert(config.Available(ctx), true)

v, err := config.Get(ctx, `server.address`)
t.AssertNil(err)
t.Assert(v.String(), ":8000")
Expand All @@ -58,3 +61,41 @@ func TestNacos(t *testing.T) {
t.AssertGT(len(m), 0)
})
}

func TestNacosOnConfigChangeFunc(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
adapter, _ := nacos.New(ctx, nacos.Config{
ServerConfigs: []constant.ServerConfig{serverConfig},
ClientConfig: clientConfig,
ConfigParam: configParam,
Watch: true,
OnConfigChange: func(namespace, group, dataId, data string) {
gtest.Assert("public", namespace)
gtest.Assert("test", group)
gtest.Assert("config.toml", dataId)
gtest.Assert("gf", g.Cfg().MustGet(gctx.GetInitCtx(), "app.name").String())
},
})
g.Cfg().SetAdapter(adapter)
t.Assert(g.Cfg().Available(ctx), true)
appName, err := g.Cfg().Get(ctx, "app.name")
t.AssertNil(err)
t.Assert(appName.String(), "")
c, err := g.Cfg().Data(ctx)
t.AssertNil(err)
j := gjson.New(c)
err = j.Set("app.name", "gf")
t.AssertNil(err)
res, err := j.ToTomlString()
t.AssertNil(err)
_, err = g.Client().Post(ctx, configPublishUrl+"&content="+url.QueryEscape(res))
t.AssertNil(err)
time.Sleep(5 * time.Second)
err = j.Remove("app")
t.AssertNil(err)
res2, err := j.ToTomlString()
t.AssertNil(err)
_, err = g.Client().Post(ctx, configPublishUrl+"&content="+url.QueryEscape(res2))
t.AssertNil(err)
})
}

0 comments on commit e3e82c7

Please sign in to comment.