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

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7e819cf
[shanyujie]解决order生成sql错误
Sep 24, 2024
34231b3
[shanyujie]add OrderBy statement generated unit test
Sep 25, 2024
fc90cdd
[shanyujie]rename unit test function name, use standalone sql for Tes…
Sep 25, 2024
f2bd517
Merge remote-tracking branch 'origin/master'
Sep 27, 2024
2cd95ae
Merge branch 'gogf:master' into master
LanceAdd Sep 29, 2024
2ddc9e1
Merge branch 'gogf:master' into master
LanceAdd Oct 8, 2024
5f2eb1e
Merge branch 'gogf:master' into master
LanceAdd Oct 8, 2024
916b202
Merge branch 'gogf:master' into master
LanceAdd Oct 16, 2024
e8a1f65
[shanyujie]Allows custom actions after a remote profile update
Oct 16, 2024
043f39f
[shanyujie]Fix unreasonable implementations and add unit tests
Oct 16, 2024
1ba84bb
Merge branch 'gogf:master' into master
LanceAdd Oct 21, 2024
a7730d4
Enter the configuration string and Nacos-related configuration inform…
LanceAdd Oct 24, 2024
d19ef6a
Fixed the unit test of the nacos configuration change callback function
LanceAdd Oct 24, 2024
2904d4d
Merge branch 'gogf:master' into master
LanceAdd Nov 7, 2024
defe831
Merge branch 'master' into master
houseme Dec 10, 2024
3df70ed
[lanceadd]还原引用
LanceAdd Dec 10, 2024
ede6e55
Merge branch 'gogf:master' into master
LanceAdd Dec 11, 2024
53918f6
[lanceadd]The two PRs #4025 and #4029 made 'v.17.0'->'v.18.0'->'v1.17…
LanceAdd Dec 11, 2024
c94b122
Merge branch 'master' into master
houseme Dec 11, 2024
6603d68
[lanceadd]Supplemental false assertions
LanceAdd Dec 12, 2024
7a91cce
[lanceadd]reRange imports
LanceAdd Dec 12, 2024
fdc0445
[lanceadd]ix golangcl-lint git push
LanceAdd Dec 12, 2024
a6da1ca
Update go.mod
houseme Dec 12, 2024
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"

houseme marked this conversation as resolved.
Show resolved Hide resolved
"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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个引入还原一下

"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