forked from shima-park/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagollo.go
More file actions
508 lines (426 loc) · 11.4 KB
/
agollo.go
File metadata and controls
508 lines (426 loc) · 11.4 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package agollo
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sync"
"time"
)
var (
localIP = getLocalIP()
defaultConfigFilePath = "app.properties"
defaultCluster = "default"
defaultNamespace = "application"
defaultConfigType = "properties"
defaultBackupFile = ".agollo"
defaultClientTimeout = 90 * time.Second
defaultNotificationID = -1
defaultLongPollInterval = 1 * time.Second
defaultAutoFetchOnCacheMiss = false
defaultFailTolerantOnBackupExists = false
defaultWatchTimeout = 500 * time.Millisecond
defaultAgollo Agollo
)
type Agollo interface {
Start() <-chan *LongPollerError
Stop()
Get(key string, opts ...GetOption) string
GetNameSpace(namespace string) Configurations
Watch() <-chan *ApolloResponse
WatchNamespace(namespace string, stop chan bool) <-chan *ApolloResponse
Options() Options
}
type ApolloResponse struct {
Namespace string
OldValue Configurations
NewValue Configurations
Changes Changes
Error error
}
type LongPollerError struct {
ConfigServerURL string
AppID string
Cluster string
Notifications []Notification
Namespace string // 服务响应200后去非缓存接口拉取时的namespace
Err error
}
type agollo struct {
opts Options
notificationMap sync.Map // key: namespace value: notificationId
releaseKeyMap sync.Map // key: namespace value: releaseKey
cache sync.Map // key: namespace value: Configurations
initialized sync.Map // key: namespace value: bool
watchCh chan *ApolloResponse // watch all namespace
watchNamespaceChMap sync.Map // key: namespace value: chan *ApolloResponse
errorsCh chan *LongPollerError
runOnce sync.Once
stop bool
stopCh chan struct{}
stopLock sync.Mutex
}
func NewWithConfigFile(configFilePath string, opts ...Option) (Agollo, error) {
f, err := os.Open(configFilePath)
if err != nil {
return nil, err
}
defer f.Close()
var conf struct {
AppID string `json:"appId,omitempty"`
Cluster string `json:"cluster,omitempty"`
NamespaceNames []string `json:"namespaceNames,omitempty"`
IP string `json:"ip,omitempty"`
}
if err := json.NewDecoder(f).Decode(&conf); err != nil {
return nil, err
}
return New(
conf.IP,
conf.AppID,
append(
[]Option{
Cluster(conf.Cluster),
PreloadNamespaces(conf.NamespaceNames...),
},
opts...,
)...,
)
}
func New(configServerURL, appID string, opts ...Option) (Agollo, error) {
a := &agollo{
stopCh: make(chan struct{}),
errorsCh: make(chan *LongPollerError),
opts: newOptions(opts...),
}
a.opts.ConfigServerURL = normalizeURL(configServerURL)
a.opts.AppID = appID
return a.preload()
}
func (a *agollo) preload() (Agollo, error) {
for _, namespace := range a.opts.PreloadNamespaces {
err := a.initNamespace(namespace)
if err != nil {
return nil, err
}
}
return a, nil
}
func (a *agollo) initNamespace(namespace string) error {
_, found := a.initialized.LoadOrStore(namespace, true)
if !found {
_, err := a.reloadNamespace(namespace, defaultNotificationID)
return err
}
return nil
}
func (a *agollo) reloadNamespace(namespace string, notificationID int) (conf Configurations, err error) {
a.notificationMap.Store(namespace, notificationID)
var (
status int
config *Config
cachedReleaseKey, _ = a.releaseKeyMap.LoadOrStore(namespace, "")
)
status, config, err = a.opts.ApolloClient.GetConfigsFromNonCache(
a.opts.ConfigServerURL,
a.opts.AppID,
a.opts.Cluster,
namespace,
ReleaseKey(cachedReleaseKey.(string)),
)
if err != nil || status != http.StatusOK {
conf = Configurations{}
if a.opts.FailTolerantOnBackupExists {
backupConfig, lerr := a.loadBackup(namespace)
if lerr == nil {
conf = backupConfig
err = nil
}
}
a.cache.Store(namespace, conf)
a.releaseKeyMap.Store(namespace, cachedReleaseKey.(string))
return
}
conf = config.Configurations
a.cache.Store(namespace, config.Configurations) // 覆盖旧缓存
a.releaseKeyMap.Store(namespace, config.ReleaseKey) // 存储最新的release_key
// 备份配置
if err = a.backup(); err != nil {
return
}
return
}
func (a *agollo) Get(key string, opts ...GetOption) string {
getOpts := newGetOptions(
append(
[]GetOption{
WithNamespace(a.opts.DefaultNamespace),
},
opts...,
)...,
)
val, found := a.GetNameSpace(getOpts.Namespace)[key]
if !found {
return getOpts.DefaultValue
}
v, _ := ToStringE(val)
return v
}
func (a *agollo) GetNameSpace(namespace string) Configurations {
config, found := a.cache.LoadOrStore(namespace, Configurations{})
if !found && a.opts.AutoFetchOnCacheMiss {
err := a.initNamespace(namespace)
if err != nil {
a.log("Namespace", namespace, "Action", "GetNameSpace", "Error", err.Error())
} else {
config, _ = a.cache.Load(namespace)
}
}
return config.(Configurations)
}
func (a *agollo) Options() Options {
return a.opts
}
// 启动goroutine去轮训apollo通知接口
func (a *agollo) Start() <-chan *LongPollerError {
a.runOnce.Do(func() {
go func() {
timer := time.NewTimer(a.opts.LongPollerInterval)
defer timer.Stop()
for !a.shouldStop() {
select {
case <-timer.C:
a.longPoll()
timer.Reset(a.opts.LongPollerInterval)
case <-a.stopCh:
return
}
}
}()
})
return a.errorsCh
}
func (a *agollo) Stop() {
a.stopLock.Lock()
defer a.stopLock.Unlock()
if a.stop {
return
}
a.stop = true
close(a.stopCh)
}
func (a *agollo) shouldStop() bool {
select {
case <-a.stopCh:
return true
default:
return false
}
}
func (a *agollo) Watch() <-chan *ApolloResponse {
if a.watchCh == nil {
a.watchCh = make(chan *ApolloResponse)
}
return a.watchCh
}
func (a *agollo) WatchNamespace(namespace string, stop chan bool) <-chan *ApolloResponse {
watchCh, exists := a.watchNamespaceChMap.LoadOrStore(namespace, make(chan *ApolloResponse))
if !exists {
go func() {
// 非预加载以外的namespace,初始化基础meta信息,否则没有longpoll
err := a.initNamespace(namespace)
if err != nil {
watchCh.(chan *ApolloResponse) <- &ApolloResponse{
Namespace: namespace,
Error: err,
}
}
if stop != nil {
<-stop
a.watchNamespaceChMap.Delete(namespace)
}
}()
}
return watchCh.(chan *ApolloResponse)
}
func (a *agollo) sendWatchCh(namespace string, oldVal, newVal Configurations) {
changes := oldVal.Different(newVal)
if len(changes) == 0 {
return
}
resp := &ApolloResponse{
Namespace: namespace,
OldValue: oldVal,
NewValue: newVal,
Changes: changes,
}
timer := time.NewTimer(defaultWatchTimeout)
for _, watchCh := range a.getWatchChs(namespace) {
select {
case watchCh <- resp:
case <-timer.C: // 防止创建全局监听或者某个namespace监听却不消费死锁问题
timer.Reset(defaultWatchTimeout)
}
}
}
func (a *agollo) getWatchChs(namespace string) []chan *ApolloResponse {
var chs []chan *ApolloResponse
if a.watchCh != nil {
chs = append(chs, a.watchCh)
}
if watchNamespaceCh, found := a.watchNamespaceChMap.Load(namespace); found {
chs = append(chs, watchNamespaceCh.(chan *ApolloResponse))
}
return chs
}
func (a *agollo) sendErrorsCh(notifications []Notification, namespace string, err error) {
longPollerError := &LongPollerError{
ConfigServerURL: a.opts.ConfigServerURL,
AppID: a.opts.AppID,
Cluster: a.opts.Cluster,
Notifications: notifications,
Namespace: namespace,
Err: err,
}
select {
case a.errorsCh <- longPollerError:
default:
}
}
func (a *agollo) log(kvs ...interface{}) {
a.opts.Logger.Log(
append([]interface{}{
"[Agollo]", "",
"ConfigServerUrl", a.opts.ConfigServerURL,
"AppID", a.opts.AppID,
"Cluster", a.opts.Cluster,
},
kvs...,
)...,
)
}
func (a *agollo) backup() error {
backup := map[string]Configurations{}
a.cache.Range(func(key, val interface{}) bool {
k, _ := key.(string)
conf, _ := val.(Configurations)
backup[k] = conf
return true
})
data, err := json.Marshal(backup)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Dir(a.opts.BackupFile), 0777)
if err != nil && !os.IsExist(err) {
return err
}
return ioutil.WriteFile(a.opts.BackupFile, data, 0666)
}
func (a *agollo) loadBackup(specifyNamespace string) (Configurations, error) {
if _, err := os.Stat(a.opts.BackupFile); err != nil {
return nil, err
}
data, err := ioutil.ReadFile(a.opts.BackupFile)
if err != nil {
return nil, err
}
backup := map[string]Configurations{}
err = json.Unmarshal(data, &backup)
if err != nil {
return nil, err
}
for namespace, configs := range backup {
if namespace == specifyNamespace {
return configs, nil
}
}
return nil, nil
}
func (a *agollo) longPoll() {
notifications := a.notifications()
status, notifications, err := a.opts.ApolloClient.Notifications(
a.opts.ConfigServerURL,
a.opts.AppID,
a.opts.Cluster,
notifications,
)
if err != nil {
a.log("Notifications", Notifications(a.notifications()).String(),
"Error", err.Error(), "Action", "LongPoll")
a.sendErrorsCh(notifications, "", err)
}
if status == http.StatusOK {
// 服务端判断没有改变,不会返回结果,这个时候不需要修改,遍历空数组跳过
for _, notification := range notifications {
// 读取旧缓存用来给监听队列
oldValue := func() Configurations {
v, _ := a.cache.Load(notification.NamespaceName)
return v.(Configurations)
}()
isSendChange := a.isSendChange(notification.NamespaceName)
// 更新namespace
newValue, err := a.reloadNamespace(notification.NamespaceName, notification.NotificationID)
if err == nil {
if isSendChange {
// 发送到监听channel
a.sendWatchCh(notification.NamespaceName,
oldValue,
newValue)
}
} else {
a.sendErrorsCh(notifications, notification.NamespaceName, err)
}
}
}
}
func (a *agollo) isSendChange(namespace string) bool {
v, ok := a.notificationMap.Load(namespace)
return ok && v.(int) > defaultNotificationID
}
func (a *agollo) notifications() []Notification {
var notifications []Notification
a.notificationMap.Range(func(key, val interface{}) bool {
k, _ := key.(string)
v, _ := val.(int)
notifications = append(notifications, Notification{
NamespaceName: k,
NotificationID: v,
})
return true
})
return notifications
}
func Init(configServerURL, appID string, opts ...Option) (err error) {
defaultAgollo, err = New(configServerURL, appID, opts...)
return
}
func InitWithConfigFile(configFilePath string, opts ...Option) (err error) {
defaultAgollo, err = NewWithConfigFile(configFilePath, opts...)
return
}
func InitWithDefaultConfigFile(opts ...Option) error {
return InitWithConfigFile(defaultConfigFilePath, opts...)
}
func Start() <-chan *LongPollerError {
return defaultAgollo.Start()
}
func Stop() {
defaultAgollo.Stop()
}
func Get(key string, opts ...GetOption) string {
return defaultAgollo.Get(key, opts...)
}
func GetNameSpace(namespace string) Configurations {
return defaultAgollo.GetNameSpace(namespace)
}
func Watch() <-chan *ApolloResponse {
return defaultAgollo.Watch()
}
func WatchNamespace(namespace string, stop chan bool) <-chan *ApolloResponse {
return defaultAgollo.WatchNamespace(namespace, stop)
}
func GetAgollo() Agollo {
return defaultAgollo
}