-
Notifications
You must be signed in to change notification settings - Fork 5
/
nacos_config.go
60 lines (53 loc) · 1.47 KB
/
nacos_config.go
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
package gfnacos
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gcfg"
"github.com/nacos-group/nacos-sdk-go/vo"
)
func initConfigService() {
if !nacosCfg.EnableConfig {
return
}
dataId := fmt.Sprintf("%s-%s.%s", nacosCfg.AppName, nacosCfg.Mode, nacosCfg.FileExtension)
content, err := nacosClients.configClient.GetConfig(vo.ConfigParam{
Group: nacosCfg.ConfigGroup,
DataId: dataId,
})
if err!=nil{
g.Log().Errorf("读取nacos配置文件[Group=%s,dataId=%s]发生错误!%s", nacosCfg.ConfigGroup, dataId, err)
}
setGcfgContent(content)
nacosClients.configClient.ListenConfig(vo.ConfigParam{
Group: nacosCfg.ConfigGroup,
DataId: dataId,
OnChange: onConfigChange,
})
}
func onConfigChange(namespace, group, dataId, data string) {
g.Log().Printf("nacos config change, namespace=%s, group=%s, dataId=%s, data=%s", namespace, group, dataId, data)
setGcfgContent(data)
}
func setGcfgContent(content string){
//g.Log().Debugf("got config file content: %s", content)
if content!=""{
g.Cfg().Clear()
gcfg.SetContent(content)
if configListener!=nil{
configListener(content)
}
}
}
func RemoveConfigListener(){
if !nacosCfg.EnableConfig {
return
}
dataId := fmt.Sprintf("%s-%s.%s", nacosCfg.AppName, nacosCfg.Mode, nacosCfg.FileExtension)
err := nacosClients.configClient.CancelListenConfig(vo.ConfigParam{
Group: nacosCfg.ConfigGroup,
DataId: dataId,
})
if err!=nil{
g.Log().Errorf("取消nacos配置监听失败!%s", err)
}
}