Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持客户端标签功能 #224

Merged
merged 2 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions component/remote/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ func (*asyncApolloConfig) GetNotifyURLSuffix(notifications string, config config
}

func (*asyncApolloConfig) GetSyncURI(config config.AppConfig, namespaceName string) string {
return fmt.Sprintf("configs/%s/%s/%s?releaseKey=%s&ip=%s",
return fmt.Sprintf("configs/%s/%s/%s?releaseKey=%s&ip=%s&label=%s",
url.QueryEscape(config.AppID),
url.QueryEscape(config.Cluster),
url.QueryEscape(namespaceName),
url.QueryEscape(config.GetCurrentApolloConfig().GetReleaseKey(namespaceName)),
utils.GetInternal())
utils.GetInternal(),
url.QueryEscape(config.Label))
}

func (a *asyncApolloConfig) Sync(appConfigFunc func() config.AppConfig) []*config.ApolloConfig {
Expand Down
56 changes: 48 additions & 8 deletions component/remote/async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,37 @@ func init() {
}

const configResponseStr = `{
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"key1":"value1",
"key2":"value2"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"key1":"value1",
"key2":"value2"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

const grayConfigResponseStr = `{
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"key1":"gray_value1",
"key2":"gray_value2"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

const configFilesResponseStr = `{
"key1":"value1",
"key2":"value2"
}`

const grayConfigFilesResponseStr = `{
"key1":"gray_value1",
"key2":"gray_value2"
}`

const configAbc1ResponseStr = `{
"appId": "100004458",
"cluster": "default",
Expand All @@ -77,6 +93,13 @@ const tworesponseStr = `[{"namespaceName":"application","notificationId":%d},{"n

func onlyNormalConfigResponse(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

label, ok := req.URL.Query()["label"]
if ok && len(label) > 0 && label[0] == grayLabel {
fmt.Fprintf(rw, grayConfigResponseStr)
return
}

fmt.Fprintf(rw, configResponseStr)
}

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

func TestApolloConfig_GraySync(t *testing.T) {
server := initMockNotifyAndConfigServer()
appConfig := initNotifications()
appConfig.IP = server.URL
appConfig.Label = grayLabel
apolloConfigs := asyncApollo.Sync(func() config.AppConfig {
return *appConfig
})
//err keep nil
Assert(t, apolloConfigs, NotNilVal())
Assert(t, len(apolloConfigs), Equal(1))

apolloConfig := apolloConfigs[0]
Assert(t, "gray_value1", Equal(apolloConfig.Configurations["key1"]))
Assert(t, "gray_value2", Equal(apolloConfig.Configurations["key2"]))
}

func TestApolloConfig_SyncABC1Error(t *testing.T) {
server := initMockNotifyAndConfigServerWithTwoErrResponse()
appConfig := initNotifications()
Expand Down
5 changes: 3 additions & 2 deletions component/remote/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ func (*syncApolloConfig) GetNotifyURLSuffix(notifications string, config config.
}

func (*syncApolloConfig) GetSyncURI(config config.AppConfig, namespaceName string) string {
return fmt.Sprintf("configfiles/json/%s/%s/%s?&ip=%s",
return fmt.Sprintf("configfiles/json/%s/%s/%s?&ip=%s&label=%s",
url.QueryEscape(config.AppID),
url.QueryEscape(config.Cluster),
url.QueryEscape(namespaceName),
utils.GetInternal())
utils.GetInternal(),
url.QueryEscape(config.Label))
}

func (*syncApolloConfig) CallBack(namespace string) http.CallBack {
Expand Down
30 changes: 30 additions & 0 deletions component/remote/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
)

var (
grayLabel = "gray"
normalConfigCount = 1
syncApollo *syncApolloConfig
)
Expand All @@ -60,6 +61,14 @@ func runNormalConfigResponse() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
normalConfigCount++
if normalConfigCount%2 == 0 {
label, ok := r.URL.Query()["label"]
if ok && len(label) > 0 && label[0] == grayLabel {
w.WriteHeader(http.StatusOK)
w.Write([]byte(grayConfigFilesResponseStr))

return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte(configFilesResponseStr))
} else {
Expand Down Expand Up @@ -153,3 +162,24 @@ func TestAutoSyncConfigServicesError(t *testing.T) {

Assert(t, len(apolloConfigs), Equal(0))
}

func TestClientLabelConfigService(t *testing.T) {
server := runNormalConfigResponse()
newAppConfig := initNotifications()
newAppConfig.IP = server.URL
newAppConfig.Label = grayLabel

apolloConfigs := syncApollo.Sync(func() config.AppConfig {
return *newAppConfig
})

Assert(t, apolloConfigs, NotNilVal())
Assert(t, len(apolloConfigs), Equal(1))

apolloConfig := apolloConfigs[0]
newAppConfig.GetCurrentApolloConfig().Set(newAppConfig.NamespaceName, &apolloConfig.ApolloConnConfig)
c := newAppConfig.GetCurrentApolloConfig().Get()[newAppConfig.NamespaceName]
Assert(t, "application", Equal(c.NamespaceName))
Assert(t, "gray_value1", Equal(apolloConfig.Configurations["key1"]))
Assert(t, "gray_value2", Equal(apolloConfig.Configurations["key2"]))
}
1 change: 1 addition & 0 deletions env/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type AppConfig struct {
IsBackupConfig bool `default:"true" json:"isBackupConfig"`
BackupConfigPath string `json:"backupConfigPath"`
Secret string `json:"secret"`
Label string `json:"label"`
SyncServerTimeout int `json:"syncServerTimeout"`
// MustStart 可用于控制第一次同步必须成功
MustStart bool `default:"false"`
Expand Down