Skip to content

Commit db16507

Browse files
committed
[lanceadd]
1.Allows custom actions after a remote profile update 2.The two PRs gogf#4025 and gogf#4029 made 'v.17.0'->'v.18.0'->'v1.17.0' to the version number of 'github.com/fatih/color', it seems that 'v.18.0' should be used
1 parent 5c45d35 commit db16507

File tree

4 files changed

+66
-23
lines changed

4 files changed

+66
-23
lines changed

contrib/config/nacos/nacos.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@ package nacos
99

1010
import (
1111
"context"
12-
13-
"github.com/nacos-group/nacos-sdk-go/v2/clients"
14-
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
15-
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
16-
"github.com/nacos-group/nacos-sdk-go/v2/vo"
17-
1812
"github.com/gogf/gf/v2/encoding/gjson"
1913
"github.com/gogf/gf/v2/errors/gerror"
2014
"github.com/gogf/gf/v2/frame/g"
2115
"github.com/gogf/gf/v2/os/gcfg"
16+
"github.com/nacos-group/nacos-sdk-go/v2/clients"
17+
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
18+
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
19+
"github.com/nacos-group/nacos-sdk-go/v2/vo"
2220
)
2321

2422
// Config is the configuration object for nacos client.
2523
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.
24+
ServerConfigs []constant.ServerConfig `v:"required"` // See constant.ServerConfig
25+
ClientConfig constant.ClientConfig `v:"required"` // See constant.ClientConfig
26+
ConfigParam vo.ConfigParam `v:"required"` // See vo.ConfigParam
27+
Watch bool // Watch watches remote configuration updates, which updates local configuration in memory immediately when remote configuration changes.
28+
OnConfigChange func(namespace, group, dataId, data string) // Configure change callback function
3029
}
3130

3231
// Client implements gcfg.Adapter implementing using nacos service.
@@ -125,9 +124,11 @@ func (c *Client) addWatcher() error {
125124
if !c.config.Watch {
126125
return nil
127126
}
128-
129127
c.config.ConfigParam.OnChange = func(namespace, group, dataId, data string) {
130128
c.doUpdate(data)
129+
if c.config.OnConfigChange != nil {
130+
go c.config.OnConfigChange(namespace, group, dataId, data)
131+
}
131132
}
132133

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

contrib/config/nacos/nacos_test.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
package nacos_test
88

99
import (
10-
"testing"
11-
12-
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
13-
"github.com/nacos-group/nacos-sdk-go/v2/vo"
14-
10+
"github.com/gogf/gf/contrib/config/nacos/v2"
11+
"github.com/gogf/gf/v2/encoding/gjson"
1512
"github.com/gogf/gf/v2/frame/g"
1613
"github.com/gogf/gf/v2/os/gctx"
1714
"github.com/gogf/gf/v2/test/gtest"
1815
"github.com/gogf/gf/v2/util/guid"
19-
20-
"github.com/gogf/gf/contrib/config/nacos/v2"
16+
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
17+
"github.com/nacos-group/nacos-sdk-go/v2/vo"
18+
"net/url"
19+
"testing"
20+
"time"
2121
)
2222

2323
var (
@@ -34,6 +34,7 @@ var (
3434
DataId: "config.toml",
3535
Group: "test",
3636
}
37+
configPublishUrl = "http://localhost:8848/nacos/v2/cs/config?type=toml&namespaceId=public&group=test&dataId=config.toml"
3738
)
3839

3940
func TestNacos(t *testing.T) {
@@ -48,7 +49,6 @@ func TestNacos(t *testing.T) {
4849
config.SetAdapter(adapter)
4950

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

contrib/registry/consul/go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/armon/go-metrics v0.4.1 // indirect
1313
github.com/clbanning/mxj/v2 v2.7.0 // indirect
1414
github.com/emirpasic/gods v1.18.1 // indirect
15-
github.com/fatih/color v1.17.0 // indirect
15+
github.com/fatih/color v1.18.0 // indirect
1616
github.com/fsnotify/fsnotify v1.7.0 // indirect
1717
github.com/go-logr/logr v1.4.2 // indirect
1818
github.com/go-logr/stdr v1.2.2 // indirect
@@ -24,7 +24,7 @@ require (
2424
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
2525
github.com/hashicorp/golang-lru v0.5.4 // indirect
2626
github.com/hashicorp/serf v0.10.1 // indirect
27-
github.com/magiconair/properties v1.8.7 // indirect
27+
github.com/magiconair/properties v1.8.9 // indirect
2828
github.com/mattn/go-colorable v0.1.13 // indirect
2929
github.com/mattn/go-isatty v0.0.20 // indirect
3030
github.com/mitchellh/go-homedir v1.1.0 // indirect
@@ -34,8 +34,8 @@ require (
3434
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
3535
go.opentelemetry.io/otel/trace v1.24.0 // indirect
3636
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
37-
golang.org/x/sys v0.22.0 // indirect
38-
golang.org/x/text v0.16.0 // indirect
37+
golang.org/x/sys v0.28.0 // indirect
38+
golang.org/x/text v0.21.0 // indirect
3939
gopkg.in/yaml.v3 v3.0.1 // indirect
4040
)
4141

contrib/registry/consul/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL
3030
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
3131
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
3232
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
33+
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
3334
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
3435
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
3536
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
@@ -105,6 +106,7 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
105106
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
106107
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
107108
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
109+
github.com/magiconair/properties v1.8.9/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
108110
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
109111
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
110112
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
@@ -222,13 +224,15 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
222224
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
223225
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
224226
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
227+
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
225228
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
226229
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
227230
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
228231
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
229232
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
230233
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
231234
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
235+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
232236
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
233237
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
234238
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)