forked from shima-park/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapollo_client.go
More file actions
248 lines (205 loc) · 6.33 KB
/
Copy pathapollo_client.go
File metadata and controls
248 lines (205 loc) · 6.33 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
package agollo
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
)
// https://github.com/ctripcorp/apollo/wiki/%E5%85%B6%E5%AE%83%E8%AF%AD%E8%A8%80%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
type ApolloClient interface {
Notifications(configServerURL, appID, clusterName string, notifications []Notification) (int, []Notification, error)
// 该接口会直接从数据库中获取配置,可以配合配置推送通知实现实时更新配置。
GetConfigsFromNonCache(configServerURL, appID, cluster, namespace string, opts ...NotificationsOption) (int, *Config, error)
// 该接口会从缓存中获取配置,适合频率较高的配置拉取请求,如简单的每30秒轮询一次配置。
GetConfigsFromCache(configServerURL, appID, cluster, namespace string) (Configurations, error)
}
type Notifications []Notification
func (n Notifications) String() string {
bytes, _ := json.Marshal(n)
return string(bytes)
}
type Notification struct {
NamespaceName string `json:"namespaceName"` // namespaceName: "application",
NotificationID int `json:"notificationId"` // notificationId: 107
}
type NotificationsOptions struct {
ReleaseKey string
}
type NotificationsOption func(*NotificationsOptions)
func ReleaseKey(releaseKey string) NotificationsOption {
return func(o *NotificationsOptions) {
o.ReleaseKey = releaseKey
}
}
type Config struct {
AppID string `json:"appId"` // appId: "AppTest",
Cluster string `json:"cluster"` // cluster: "default",
NamespaceName string `json:"namespaceName"` // namespaceName: "TEST.Namespace1",
Configurations Configurations `json:"configurations"` // configurations: {Name: "Foo"},
ReleaseKey string `json:"releaseKey"` // releaseKey: "20181017110222-5ce3b2da895720e8"
}
type Doer interface {
Do(*http.Request) (*http.Response, error)
}
type apolloClient struct {
Doer Doer
IP string
ConfigType string // 默认properties不需要在namespace后加后缀名,其他情况例如application.json {xml,yml,yaml,json,...}
}
type ApolloClientOption func(*apolloClient)
func WithDoer(d Doer) ApolloClientOption {
return func(a *apolloClient) {
a.Doer = d
}
}
func WithIP(ip string) ApolloClientOption {
return func(a *apolloClient) {
a.IP = ip
}
}
func WithConfigType(configType string) ApolloClientOption {
return func(a *apolloClient) {
a.ConfigType = configType
}
}
func NewApolloClient(opts ...ApolloClientOption) ApolloClient {
c := &apolloClient{}
for _, opt := range opts {
opt(c)
}
if c.Doer == nil {
c.Doer = &http.Client{
Timeout: defaultClientTimeout, // Notifications由于服务端会hold住请求60秒,所以请确保客户端访问服务端的超时时间要大于60秒。
}
}
if c.IP == "" {
c.IP = localIP
}
if c.ConfigType == "" {
c.ConfigType = defaultConfigType
}
return c
}
func (c *apolloClient) Notifications(configServerURL, appID, cluster string, notifications []Notification) (status int, result []Notification, err error) {
configServerURL = normalizeURL(configServerURL)
url := fmt.Sprintf("%s/notifications/v2?appId=%s&cluster=%s¬ifications=%s",
configServerURL,
url.QueryEscape(appID),
url.QueryEscape(cluster),
url.QueryEscape(Notifications(notifications).String()),
)
var req *http.Request
req, err = http.NewRequest("GET", url, nil)
if err != nil {
return
}
var body []byte
status, body, err = parseResponseBody(c.Doer, req)
if err != nil {
return
}
if status == http.StatusOK {
err = json.Unmarshal(body, &result)
return
}
return
}
func (c *apolloClient) GetConfigsFromNonCache(configServerURL, appID, cluster, namespace string, opts ...NotificationsOption) (status int, config *Config, err error) {
var options = NotificationsOptions{}
for _, opt := range opts {
opt(&options)
}
configServerURL = normalizeURL(configServerURL)
url := fmt.Sprintf("%s/configs/%s/%s/%s?releaseKey=%s&ip=%s",
configServerURL,
url.QueryEscape(appID),
url.QueryEscape(cluster),
url.QueryEscape(c.getNamespace(namespace)),
options.ReleaseKey,
c.IP,
)
var req *http.Request
req, err = http.NewRequest("GET", url, nil)
if err != nil {
return
}
var body []byte
status, body, err = parseResponseBody(c.Doer, req)
if err != nil {
return
}
if status == http.StatusOK {
config = new(Config)
err = json.Unmarshal(body, config)
return
}
return
}
func (c *apolloClient) GetConfigsFromCache(configServerURL, appID, cluster, namespace string) (config Configurations, err error) {
configServerURL = normalizeURL(configServerURL)
url := fmt.Sprintf("%s/configfiles/json/%s/%s/%s?ip=%s",
configServerURL,
url.QueryEscape(appID),
url.QueryEscape(cluster),
url.QueryEscape(c.getNamespace(namespace)),
c.IP,
)
var req *http.Request
req, err = http.NewRequest("GET", url, nil)
if err != nil {
return
}
var (
body []byte
status int
)
status, body, err = parseResponseBody(c.Doer, req)
if err != nil {
return
}
if status == http.StatusOK {
config = make(Configurations)
err = json.Unmarshal(body, &config)
} else {
err = errors.New(string(body))
}
return
}
// 配置文件有多种格式,例如:properties、xml、yml、yaml、json等。同样Namespace也具有这些格式。在Portal UI中可以看到“application”的Namespace上有一个“properties”标签,表明“application”是properties格式的。
// 如果使用Http接口直接调用时,对应的namespace参数需要传入namespace的名字加上后缀名,如datasources.json。
func (c *apolloClient) getNamespace(namespace string) string {
if c.ConfigType == "" || c.ConfigType == defaultConfigType {
return namespace
}
return namespace + "." + c.ConfigType
}
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func parseResponseBody(doer Doer, req *http.Request) (int, []byte, error) {
resp, err := doer.Do(req)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, nil, err
}
return resp.StatusCode, body, nil
}