forked from shima-park/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer.go
194 lines (158 loc) · 4.07 KB
/
balancer.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
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
package agollo
import (
"errors"
"math/rand"
"os"
"sync"
"sync/atomic"
"time"
)
var (
defaultRefreshIntervalInSecond = time.Second * 60
defaultMetaURL = "http://apollo.meta"
ErrNoConfigServerAvailable = errors.New("no config server availbale")
)
type Balancer interface {
Select() (string, error)
Stop()
}
type autoFetchBalancer struct {
appID string
getConfigServers GetConfigServersFunc
metaServerAddress string
logger Logger
mu sync.RWMutex
b Balancer
stopCh chan struct{}
}
type GetConfigServersFunc func(metaServerURL, appID string) (int, []ConfigServer, error)
func NewAutoFetchBalancer(configServerURL, appID string, getConfigServers GetConfigServersFunc,
refreshIntervalInSecond time.Duration, logger Logger) (Balancer, error) {
if refreshIntervalInSecond <= time.Duration(0) {
refreshIntervalInSecond = defaultRefreshIntervalInSecond
}
b := &autoFetchBalancer{
appID: appID,
getConfigServers: getConfigServers,
metaServerAddress: getMetaServerAddress(configServerURL), // Meta Server只是一个逻辑角色,在部署时和Config Service是在一个JVM进程中的,所以IP、端口和Config Service一致
logger: logger,
stopCh: make(chan struct{}),
b: NewRoundRobin([]string{configServerURL}),
}
err := b.updateConfigServices()
if err != nil {
return nil, err
}
go func() {
ticker := time.NewTicker(refreshIntervalInSecond)
defer ticker.Stop()
for {
select {
case <-b.stopCh:
return
case <-ticker.C:
_ = b.updateConfigServices()
}
}
}()
return b, nil
}
/*
参考了java客户端实现
目前实现方式:
0. 客户端显式传入ConfigServerURL
1. 读取APOLLO_META环境变量
2. 默认如果没有提供meta服务地址默认使用(http://apollo.meta)
未实现:
读取properties的逻辑
https://github.com/ctripcorp/apollo/blob/7545bd3cd7d4b996d7cda50f53cd4aa8b045a2bb/apollo-core/src/main/java/com/ctrip/framework/apollo/core/MetaDomainConsts.java#L27
*/
func getMetaServerAddress(configServerURL string) string {
var urls []string
for _, url := range []string{
configServerURL,
os.Getenv("APOLLO_META"),
} {
if url != "" {
urls = splitCommaSeparatedURL(url)
break
}
}
if len(urls) > 0 {
return normalizeURL(urls[rand.Intn(len(urls))])
}
return defaultMetaURL
}
func (b *autoFetchBalancer) updateConfigServices() error {
css, err := b.getConfigServices()
if err != nil {
return err
}
var urls []string
for _, url := range css {
//check whether /services/config is accessible
status, _, err := b.getConfigServers(url, b.appID)
if err != nil {
continue
}
// select the first available meta server
// https://github.com/ctripcorp/apollo/blob/7545bd3cd7d4b996d7cda50f53cd4aa8b045a2bb/apollo-core/src/main/java/com/ctrip/framework/apollo/core/MetaDomainConsts.java#L166
// 这里这段逻辑是参考java客户端,直接选了第一个可用的meta server
if 200 <= status && status <= 399 {
urls = append(urls, url)
break
}
}
if len(urls) == 0 {
return nil
}
b.mu.Lock()
b.b = NewRoundRobin(css)
b.mu.Unlock()
return nil
}
func (b *autoFetchBalancer) getConfigServices() ([]string, error) {
_, css, err := b.getConfigServers(b.metaServerAddress, b.appID)
if err != nil {
b.logger.Log(
"[Agollo]", "",
"AppID", b.appID,
"MetaServerAddress", b.metaServerAddress,
"Error", err,
)
return nil, err
}
var urls []string
for _, cs := range css {
urls = append(urls, normalizeURL(cs.HomePageURL))
}
return urls, nil
}
func (b *autoFetchBalancer) Select() (string, error) {
b.mu.RLock()
defer b.mu.RUnlock()
return b.b.Select()
}
func (b *autoFetchBalancer) Stop() {
close(b.stopCh)
}
type roundRobin struct {
ss []string
c uint64
}
func NewRoundRobin(ss []string) Balancer {
return &roundRobin{
ss: ss,
c: 0,
}
}
func (rr *roundRobin) Select() (string, error) {
if len(rr.ss) <= 0 {
return "", ErrNoConfigServerAvailable
}
old := atomic.AddUint64(&rr.c, 1) - 1
idx := old % uint64(len(rr.ss))
return rr.ss[idx], nil
}
func (rr *roundRobin) Stop() {
}