Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(contrib/config/nacos): add OnChange callbacks configuration support #3858

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions contrib/config/nacos/nacos.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package nacos

import (
"context"

"github.com/nacos-group/nacos-sdk-go/v2/clients"
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
Expand All @@ -23,10 +22,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 +125,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
45 changes: 39 additions & 6 deletions contrib/config/nacos/nacos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
package nacos_test

import (
"testing"

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

"github.com/gogf/gf/contrib/config/nacos/v2"
"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"
"github.com/gogf/gf/v2/util/guid"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
"net/url"
"testing"
"time"
)

var (
Expand All @@ -33,6 +34,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 @@ -47,7 +49,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 @@ -57,3 +58,35 @@ 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, _ := g.Cfg().Data(ctx)
j := gjson.New(c)
j.Set("app.name", "gf")
res, _ := j.ToTomlString()
_, err = g.Client().Post(ctx, configPublishUrl+"&content="+url.QueryEscape(res))
t.AssertNil(err)
time.Sleep(5 * time.Second)
j.Remove("app")
res2, _ := j.ToTomlString()
_, err = g.Client().Post(ctx, configPublishUrl+"&content="+url.QueryEscape(res2))
})
}
Loading