-
Notifications
You must be signed in to change notification settings - Fork 51
/
apollo_client.go
211 lines (182 loc) · 5.72 KB
/
apollo_client.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package agollo
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"time"
)
var (
defaultClientTimeout = 90 * time.Second
)
const (
// ENV_APOLLO_ACCESS_KEY 默认从环境变量中读取Apollo的AccessKey
// 会被显示传入的AccessKey所覆盖
ENV_APOLLO_ACCESS_KEY = "APOLLO_ACCESS_KEY"
)
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,...}
AccessKey string
SignatureFunc SignatureFunc
}
func NewApolloClient(opts ...ApolloClientOption) ApolloClient {
c := &apolloClient{
IP: getLocalIP(),
ConfigType: defaultConfigType,
Doer: &http.Client{
Timeout: defaultClientTimeout, // Notifications由于服务端会hold住请求60秒,所以请确保客户端访问服务端的超时时间要大于60秒。
},
AccessKey: os.Getenv(ENV_APOLLO_ACCESS_KEY),
SignatureFunc: DefaultSignatureFunc,
}
c.Apply(opts...)
return c
}
func (c *apolloClient) Apply(opts ...ApolloClientOption) {
for _, opt := range opts {
opt(c)
}
}
func (c *apolloClient) Notifications(configServerURL, appID, cluster string, notifications []Notification) (status int, result []Notification, err error) {
if len(notifications) == 0 {
return 0, []Notification{}, nil
}
configServerURL = normalizeURL(configServerURL)
requestURI := fmt.Sprintf("/notifications/v2?appId=%s&cluster=%s¬ifications=%s",
url.QueryEscape(appID),
url.QueryEscape(cluster),
url.QueryEscape(Notifications(notifications).String()),
)
apiURL := fmt.Sprintf("%s%s", configServerURL, requestURI)
headers := c.SignatureFunc(&SignatureContext{
ConfigServerURL: configServerURL,
RequestURI: requestURI,
AccessKey: c.AccessKey,
AppID: appID,
Cluster: cluster,
})
status, err = c.do("GET", apiURL, headers, &result)
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)
requestURI := fmt.Sprintf("/configs/%s/%s/%s?releaseKey=%s&ip=%s",
url.QueryEscape(appID),
url.QueryEscape(cluster),
url.QueryEscape(c.getNamespace(namespace)),
options.ReleaseKey,
c.IP,
)
apiURL := fmt.Sprintf("%s%s", configServerURL, requestURI)
headers := c.SignatureFunc(&SignatureContext{
ConfigServerURL: configServerURL,
RequestURI: requestURI,
AccessKey: c.AccessKey,
AppID: appID,
Cluster: cluster,
})
config = new(Config)
status, err = c.do("GET", apiURL, headers, config)
return
}
func (c *apolloClient) GetConfigsFromCache(configServerURL, appID, cluster, namespace string) (config Configurations, err error) {
configServerURL = normalizeURL(configServerURL)
requestURI := fmt.Sprintf("/configfiles/json/%s/%s/%s?ip=%s",
url.QueryEscape(appID),
url.QueryEscape(cluster),
url.QueryEscape(c.getNamespace(namespace)),
c.IP,
)
apiURL := fmt.Sprintf("%s%s", configServerURL, requestURI)
headers := c.SignatureFunc(&SignatureContext{
ConfigServerURL: configServerURL,
RequestURI: requestURI,
AccessKey: c.AccessKey,
AppID: appID,
Cluster: cluster,
})
config = make(Configurations)
_, err = c.do("GET", apiURL, headers, config)
return
}
func (c *apolloClient) GetConfigServers(metaServerURL, appID string) (int, []ConfigServer, error) {
metaServerURL = normalizeURL(metaServerURL)
requestURI := fmt.Sprintf("/services/config?id=%s&appId=%s", c.IP, appID)
apiURL := fmt.Sprintf("%s%s", metaServerURL, requestURI)
headers := c.SignatureFunc(&SignatureContext{
ConfigServerURL: metaServerURL,
RequestURI: requestURI,
AccessKey: c.AccessKey,
AppID: appID,
Cluster: "",
})
var cfs []ConfigServer
status, err := c.do("GET", apiURL, headers, &cfs)
return status, cfs, err
}
func (c *apolloClient) do(method, url string, headers map[string]string, v interface{}) (status int, err error) {
var req *http.Request
req, err = http.NewRequest(method, url, nil)
if err != nil {
return
}
for key, val := range headers {
req.Header.Set(key, val)
}
var body []byte
status, body, err = parseResponseBody(c.Doer, req)
if err != nil {
return
}
if status == http.StatusOK {
err = json.Unmarshal(body, v)
}
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
}