1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package main
import (
"fmt"
"github.com/nacos-group/nacos-sdk-go/v2/clients"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
"time"
)
func main() {
//sc := []constant.ServerConfig{
//
// *constant.NewServerConfig("nacos.kubegems.io", 30498, constant.WithContextPath("/nacos")),
//}
//
//cc := constant.ClientConfig{
// NamespaceId: "2cfea28330290809ac7ebc7747fa7a6f3672cba5", //namespace id
// TimeoutMs: 5000,
// NotLoadCacheAtStart: true,
// LogDir: "/tmp/nacos/log",
// CacheDir: "/tmp/nacos/cache",
// LogLevel: "debug",
//}
sc := []constant.ServerConfig{
*constant.NewServerConfig("nacos.kubegems.io", 32698),
}
//create ClientConfig
cc := *constant.NewClientConfig(
constant.WithNamespaceId("20a02f696f9440e9e8cf0461574921bbeff04c18"),
constant.WithTimeoutMs(5000),
constant.WithNotLoadCacheAtStart(true),
constant.WithLogDir("/tmp/nacos/log"),
constant.WithCacheDir("/tmp/nacos/cache"),
constant.WithLogLevel("debug"),
)
// create config client
client, err := clients.NewConfigClient(
vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc,
},
)
if err != nil {
panic(err)
}
_, err = client.PublishConfig(vo.ConfigParam{
DataId: "test-data",
Group: "test",
Content: "hello world!",
})
_, err = client.PublishConfig(vo.ConfigParam{
DataId: "dataId",
Group: "test",
Content: "hello world!222222"})
if err != nil {
fmt.Printf("PublishConfig err:%+v \n", err)
}
content, err := client.GetConfig(vo.ConfigParam{
DataId: "hxf",
Group: "test",
})
fmt.Println("GetConfig,config :" + content)
if err != nil {
fmt.Println(err)
}
err = client.ListenConfig(vo.ConfigParam{
DataId: "hxf",
Group: "test",
OnChange: func(namespace, group, dataId, data string) {
fmt.Println("config changed group:" + group + ", dataId:" + dataId + ", content:" + data)
},
})
time.Sleep(300 * time.Second)
}
|