-
Notifications
You must be signed in to change notification settings - Fork 179
/
apollo.go
82 lines (67 loc) · 1.66 KB
/
apollo.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package config
import (
"fmt"
"strings"
"github.com/spf13/viper"
"github.com/apolloconfig/agollo/v4"
"github.com/apolloconfig/agollo/v4/env/config"
"github.com/apolloconfig/agollo/v4/storage"
)
const FlagApollo = "config.apollo"
type ApolloClient struct {
Namespace string
*agollo.Client
oecConf *OecConfig
}
func NewApolloClient(oecConf *OecConfig) *ApolloClient {
// IP|AppID|NamespaceName
params := strings.Split(viper.GetString(FlagApollo), "|")
if len(params) != 3 {
panic("failed init apollo: invalid connection config")
}
c := &config.AppConfig{
IP: params[0],
AppID: params[1],
NamespaceName: params[2],
Cluster: "default",
IsBackupConfig: false,
//Secret: "",
}
client, err := agollo.StartWithConfig(func() (*config.AppConfig, error) {
return c, nil
})
if err != nil {
panic(fmt.Errorf("failed init apollo: %v", err))
}
apc := &ApolloClient{
params[2],
client,
oecConf,
}
client.AddChangeListener(&CustomChangeListener{oecConf})
return apc
}
func (a *ApolloClient) LoadConfig() (loaded bool) {
cache := a.GetConfigCache(a.Namespace)
cache.Range(func(key, value interface{}) bool {
loaded = true
a.oecConf.update(key, value)
return true
})
confLogger.Info(a.oecConf.format())
return
}
type CustomChangeListener struct {
oecConf *OecConfig
}
func (c *CustomChangeListener) OnChange(changeEvent *storage.ChangeEvent) {
for key, value := range changeEvent.Changes {
if value.ChangeType != storage.DELETED {
c.oecConf.update(key, value.NewValue)
}
}
confLogger.Info(c.oecConf.format())
}
func (c *CustomChangeListener) OnNewestChange(event *storage.FullChangeEvent) {
return
}