Skip to content

Commit 9878895

Browse files
peerless1024evelynwei
andauthored
feat: configAPI support return config version (#265)
Co-authored-by: evelynwei <[email protected]>
1 parent bbe71dc commit 9878895

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

examples/configuration/normal/main.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,33 @@ package main
1919

2020
import (
2121
"encoding/json"
22+
"flag"
23+
"fmt"
2224
"log"
2325

2426
"github.com/polarismesh/polaris-go"
27+
"github.com/polarismesh/polaris-go/api"
2528
"github.com/polarismesh/polaris-go/pkg/model"
2629
)
2730

31+
var debug bool
32+
33+
func initArgs() {
34+
flag.BoolVar(&debug, "debug", true, "是否开启调试模式")
35+
}
36+
2837
func main() {
38+
initArgs()
39+
flag.Parse()
2940
log.Println("开始启动配置文件监听示例...")
30-
41+
if debug {
42+
// 设置日志级别为DEBUG
43+
if err := api.SetLoggersLevel(api.DebugLog); err != nil {
44+
log.Printf("fail to set log level to DEBUG, err is %v", err)
45+
} else {
46+
log.Printf("successfully set log level to DEBUG")
47+
}
48+
}
3149
configAPI, err := polaris.NewConfigAPI()
3250
if err != nil {
3351
log.Printf("创建配置API失败: %v", err)
@@ -53,6 +71,7 @@ func main() {
5371
content := configFile.GetContent()
5472
log.Printf("配置文件内容:\n%s", content)
5573
log.Printf("配置文件内容长度: %d 字符", len(content))
74+
log.Printf("fetched config file:\n %s\n", getString(configFile))
5675

5776
// 方式一:添加监听器
5877
log.Println("添加配置变更监听器(回调函数方式)...")
@@ -67,6 +86,7 @@ func main() {
6786
select {
6887
case event := <-changeChan:
6988
log.Printf("通过通道接收到配置变更事件: %+v", event)
89+
log.Printf("updated config file:\n %s\n", getString(configFile))
7090
}
7191
}
7292
}
@@ -89,3 +109,18 @@ func jsonMarshal(v interface{}) string {
89109
}
90110
return string(data)
91111
}
112+
113+
func getString(c model.ConfigFile) string {
114+
return fmt.Sprintf("ConfigFile{Namespace=%s, FileGroup=%s, FileName=%s, Version=%d, VersionName=%s, Md5=%s, "+
115+
"Labels=%v, HasContent=%t, ContentLength=%d}",
116+
c.GetNamespace(),
117+
c.GetFileGroup(),
118+
c.GetFileName(),
119+
c.GetVersion(),
120+
c.GetVersionName(),
121+
c.GetMd5(),
122+
c.GetLabels(),
123+
c.HasContent(),
124+
len(c.GetContent()),
125+
)
126+
}

pkg/flow/configuration/model.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@ func (c *defaultConfigFile) GetPersistent() model.Persistent {
7373
return c.persistent
7474
}
7575

76+
// GetVersionName 获取配置文件版本名称
77+
func (c *defaultConfigFile) GetVersionName() string {
78+
if c.fileRepo == nil || c.fileRepo.loadRemoteFile() == nil {
79+
return ""
80+
}
81+
return c.fileRepo.loadRemoteFile().GetVersionName()
82+
}
83+
84+
// GetVersion 获取配置文件版本号
85+
func (c *defaultConfigFile) GetVersion() uint64 {
86+
if c.fileRepo == nil || c.fileRepo.loadRemoteFile() == nil {
87+
return 0
88+
}
89+
return c.fileRepo.loadRemoteFile().GetVersion()
90+
}
91+
92+
// GetMd5 获取配置文件MD5值
93+
func (c *defaultConfigFile) GetMd5() string {
94+
if c.fileRepo == nil || c.fileRepo.loadRemoteFile() == nil {
95+
return ""
96+
}
97+
return c.fileRepo.loadRemoteFile().GetMd5()
98+
}
99+
76100
// HasContent 是否有配置内容
77101
func (c *defaultConfigFile) HasContent() bool {
78102
return c.content != "" && c.content != NotExistedFileContent

pkg/model/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ type ConfigFile interface {
117117
AddChangeListener(cb OnConfigFileChange)
118118
// GetPersistent 获取文件持久化数据
119119
GetPersistent() Persistent
120+
// GetVersionName 获取配置文件版本名称
121+
GetVersionName() string
122+
// GetVersion 获取配置文件版本
123+
GetVersion() uint64
124+
// GetMd5 获取配置文件md5值
125+
GetMd5() string
120126
}
121127

122128
// DefaultConfigFileMetadata 默认 ConfigFileMetadata 实现类

0 commit comments

Comments
 (0)