Skip to content

Commit 559999e

Browse files
committed
Finish v4.2.0
2 parents f89da36 + c3eb3db commit 559999e

File tree

8 files changed

+351
-71
lines changed

8 files changed

+351
-71
lines changed

component/remote/async.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ func (*asyncApolloConfig) GetNotifyURLSuffix(notifications string, config config
5959
}
6060

6161
func (*asyncApolloConfig) GetSyncURI(config config.AppConfig, namespaceName string) string {
62-
return fmt.Sprintf("configs/%s/%s/%s?releaseKey=%s&ip=%s",
62+
return fmt.Sprintf("configs/%s/%s/%s?releaseKey=%s&ip=%s&label=%s",
6363
url.QueryEscape(config.AppID),
6464
url.QueryEscape(config.Cluster),
6565
url.QueryEscape(namespaceName),
6666
url.QueryEscape(config.GetCurrentApolloConfig().GetReleaseKey(namespaceName)),
67-
utils.GetInternal())
67+
utils.GetInternal(),
68+
url.QueryEscape(config.Label))
6869
}
6970

7071
func (a *asyncApolloConfig) Sync(appConfigFunc func() config.AppConfig) []*config.ApolloConfig {

component/remote/async_test.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,37 @@ func init() {
4646
}
4747

4848
const configResponseStr = `{
49-
"appId": "100004458",
50-
"cluster": "default",
51-
"namespaceName": "application",
52-
"configurations": {
53-
"key1":"value1",
54-
"key2":"value2"
55-
},
56-
"releaseKey": "20170430092936-dee2d58e74515ff3"
49+
"appId": "100004458",
50+
"cluster": "default",
51+
"namespaceName": "application",
52+
"configurations": {
53+
"key1":"value1",
54+
"key2":"value2"
55+
},
56+
"releaseKey": "20170430092936-dee2d58e74515ff3"
57+
}`
58+
59+
const grayConfigResponseStr = `{
60+
"appId": "100004458",
61+
"cluster": "default",
62+
"namespaceName": "application",
63+
"configurations": {
64+
"key1":"gray_value1",
65+
"key2":"gray_value2"
66+
},
67+
"releaseKey": "20170430092936-dee2d58e74515ff3"
5768
}`
5869

5970
const configFilesResponseStr = `{
6071
"key1":"value1",
6172
"key2":"value2"
6273
}`
6374

75+
const grayConfigFilesResponseStr = `{
76+
"key1":"gray_value1",
77+
"key2":"gray_value2"
78+
}`
79+
6480
const configAbc1ResponseStr = `{
6581
"appId": "100004458",
6682
"cluster": "default",
@@ -77,6 +93,13 @@ const tworesponseStr = `[{"namespaceName":"application","notificationId":%d},{"n
7793

7894
func onlyNormalConfigResponse(rw http.ResponseWriter, req *http.Request) {
7995
rw.WriteHeader(http.StatusOK)
96+
97+
label, ok := req.URL.Query()["label"]
98+
if ok && len(label) > 0 && label[0] == grayLabel {
99+
fmt.Fprintf(rw, grayConfigResponseStr)
100+
return
101+
}
102+
80103
fmt.Fprintf(rw, configResponseStr)
81104
}
82105

@@ -192,6 +215,23 @@ func TestApolloConfig_SyncTwoOk(t *testing.T) {
192215
Assert(t, appConfig.GetNotificationsMap().GetNotify("abc1"), Equal(int64(3)))
193216
}
194217

218+
func TestApolloConfig_GraySync(t *testing.T) {
219+
server := initMockNotifyAndConfigServer()
220+
appConfig := initNotifications()
221+
appConfig.IP = server.URL
222+
appConfig.Label = grayLabel
223+
apolloConfigs := asyncApollo.Sync(func() config.AppConfig {
224+
return *appConfig
225+
})
226+
//err keep nil
227+
Assert(t, apolloConfigs, NotNilVal())
228+
Assert(t, len(apolloConfigs), Equal(1))
229+
230+
apolloConfig := apolloConfigs[0]
231+
Assert(t, "gray_value1", Equal(apolloConfig.Configurations["key1"]))
232+
Assert(t, "gray_value2", Equal(apolloConfig.Configurations["key2"]))
233+
}
234+
195235
func TestApolloConfig_SyncABC1Error(t *testing.T) {
196236
server := initMockNotifyAndConfigServerWithTwoErrResponse()
197237
appConfig := initNotifications()

component/remote/sync.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ func (*syncApolloConfig) GetNotifyURLSuffix(notifications string, config config.
4747
}
4848

4949
func (*syncApolloConfig) GetSyncURI(config config.AppConfig, namespaceName string) string {
50-
return fmt.Sprintf("configfiles/json/%s/%s/%s?&ip=%s",
50+
return fmt.Sprintf("configfiles/json/%s/%s/%s?&ip=%s&label=%s",
5151
url.QueryEscape(config.AppID),
5252
url.QueryEscape(config.Cluster),
5353
url.QueryEscape(namespaceName),
54-
utils.GetInternal())
54+
utils.GetInternal(),
55+
url.QueryEscape(config.Label))
5556
}
5657

5758
func (*syncApolloConfig) CallBack(namespace string) http.CallBack {

component/remote/sync_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
)
3838

3939
var (
40+
grayLabel = "gray"
4041
normalConfigCount = 1
4142
syncApollo *syncApolloConfig
4243
)
@@ -60,6 +61,14 @@ func runNormalConfigResponse() *httptest.Server {
6061
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6162
normalConfigCount++
6263
if normalConfigCount%2 == 0 {
64+
label, ok := r.URL.Query()["label"]
65+
if ok && len(label) > 0 && label[0] == grayLabel {
66+
w.WriteHeader(http.StatusOK)
67+
w.Write([]byte(grayConfigFilesResponseStr))
68+
69+
return
70+
}
71+
6372
w.WriteHeader(http.StatusOK)
6473
w.Write([]byte(configFilesResponseStr))
6574
} else {
@@ -153,3 +162,24 @@ func TestAutoSyncConfigServicesError(t *testing.T) {
153162

154163
Assert(t, len(apolloConfigs), Equal(0))
155164
}
165+
166+
func TestClientLabelConfigService(t *testing.T) {
167+
server := runNormalConfigResponse()
168+
newAppConfig := initNotifications()
169+
newAppConfig.IP = server.URL
170+
newAppConfig.Label = grayLabel
171+
172+
apolloConfigs := syncApollo.Sync(func() config.AppConfig {
173+
return *newAppConfig
174+
})
175+
176+
Assert(t, apolloConfigs, NotNilVal())
177+
Assert(t, len(apolloConfigs), Equal(1))
178+
179+
apolloConfig := apolloConfigs[0]
180+
newAppConfig.GetCurrentApolloConfig().Set(newAppConfig.NamespaceName, &apolloConfig.ApolloConnConfig)
181+
c := newAppConfig.GetCurrentApolloConfig().Get()[newAppConfig.NamespaceName]
182+
Assert(t, "application", Equal(c.NamespaceName))
183+
Assert(t, "gray_value1", Equal(apolloConfig.Configurations["key1"]))
184+
Assert(t, "gray_value2", Equal(apolloConfig.Configurations["key2"]))
185+
}

env/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type AppConfig struct {
4848
IsBackupConfig bool `default:"true" json:"isBackupConfig"`
4949
BackupConfigPath string `json:"backupConfigPath"`
5050
Secret string `json:"secret"`
51+
Label string `json:"label"`
5152
SyncServerTimeout int `json:"syncServerTimeout"`
5253
// MustStart 可用于控制第一次同步必须成功
5354
MustStart bool `default:"false"`

protocol/http/request.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ var (
6262
func getDefaultTransport(insecureSkipVerify bool) *http.Transport {
6363
once.Do(func() {
6464
defaultTransport = &http.Transport{
65+
Proxy: http.ProxyFromEnvironment,
6566
MaxIdleConns: defaultMaxConnsPerHost,
6667
MaxIdleConnsPerHost: defaultMaxConnsPerHost,
6768
DialContext: (&net.Dialer{

0 commit comments

Comments
 (0)