diff --git a/common/url.go b/common/url.go index 6431ee12995a95848c6f3369c490bc5dbf07bb6b..eca1d27992f7ea1e46f292d26544756842867de5 100644 --- a/common/url.go +++ b/common/url.go @@ -419,7 +419,7 @@ func (c URL) ToMap() map[string]string { //TODO configuration merge, in the future , the configuration center's config should merge too. func MergeUrl(serviceUrl URL, referenceUrl *URL) URL { mergedUrl := serviceUrl - var methodConfigMergeFcn = []func(method string){} + //iterator the referenceUrl if serviceUrl not have the key ,merge in for k, v := range referenceUrl.Params { @@ -427,28 +427,11 @@ func MergeUrl(serviceUrl URL, referenceUrl *URL) URL { mergedUrl.Params.Set(k, v[0]) } } - //loadBalance strategy config - if v := referenceUrl.Params.Get(constant.LOADBALANCE_KEY); v != "" { - mergedUrl.Params.Set(constant.LOADBALANCE_KEY, v) - } - methodConfigMergeFcn = append(methodConfigMergeFcn, func(method string) { - if v := referenceUrl.Params.Get(method + "." + constant.LOADBALANCE_KEY); v != "" { - mergedUrl.Params.Set(method+"."+constant.LOADBALANCE_KEY, v) - } - }) - - //cluster strategy config - if v := referenceUrl.Params.Get(constant.CLUSTER_KEY); v != "" { - mergedUrl.Params.Set(constant.CLUSTER_KEY, v) - } - methodConfigMergeFcn = append(methodConfigMergeFcn, func(method string) { - if v := referenceUrl.Params.Get(method + "." + constant.CLUSTER_KEY); v != "" { - mergedUrl.Params.Set(method+"."+constant.CLUSTER_KEY, v) - } - }) + //loadBalance,cluster,retries strategy config + methodConfigMergeFcn := mergeNormalParam(mergedUrl, referenceUrl, []string{constant.LOADBALANCE_KEY, constant.CLUSTER_KEY, constant.RETRIES_KEY}) //remote timestamp - if v := serviceUrl.Params.Get(constant.TIMESTAMP_KEY); v != "" { + if v := serviceUrl.Params.Get(constant.TIMESTAMP_KEY); len(v) > 0 { mergedUrl.Params.Set(constant.REMOTE_TIMESTAMP_KEY, v) mergedUrl.Params.Set(constant.TIMESTAMP_KEY, referenceUrl.Params.Get(constant.TIMESTAMP_KEY)) } @@ -462,3 +445,18 @@ func MergeUrl(serviceUrl URL, referenceUrl *URL) URL { return mergedUrl } + +func mergeNormalParam(mergedUrl URL, referenceUrl *URL, paramKeys []string) []func(method string) { + var methodConfigMergeFcn = []func(method string){} + for _, paramKey := range paramKeys { + if v := referenceUrl.Params.Get(paramKey); len(v) > 0 { + mergedUrl.Params.Set(paramKey, v) + } + methodConfigMergeFcn = append(methodConfigMergeFcn, func(method string) { + if v := referenceUrl.Params.Get(method + "." + paramKey); len(v) > 0 { + mergedUrl.Params.Set(method+"."+paramKey, v) + } + }) + } + return methodConfigMergeFcn +} diff --git a/common/url_test.go b/common/url_test.go index 143e31cb34f2ec1da7efc910a6b4133f0f4789b5..9366b0c1cc39910bfec48efd052fd1fa31cf1750 100644 --- a/common/url_test.go +++ b/common/url_test.go @@ -220,15 +220,21 @@ func TestURL_GetMethodParam(t *testing.T) { func TestMergeUrl(t *testing.T) { referenceUrlParams := url.Values{} referenceUrlParams.Set(constant.CLUSTER_KEY, "random") + referenceUrlParams.Set(constant.RETRIES_KEY, "1") referenceUrlParams.Set("test3", "1") + referenceUrlParams.Set("methods.testMethod."+constant.RETRIES_KEY, "1") serviceUrlParams := url.Values{} serviceUrlParams.Set("test2", "1") serviceUrlParams.Set(constant.CLUSTER_KEY, "roundrobin") - referenceUrl, _ := NewURL(context.TODO(), "mock1://127.0.0.1:1111", WithParams(referenceUrlParams)) + serviceUrlParams.Set(constant.RETRIES_KEY, "2") + serviceUrlParams.Set("methods.testMethod."+constant.RETRIES_KEY, "2") + referenceUrl, _ := NewURL(context.TODO(), "mock1://127.0.0.1:1111", WithParams(referenceUrlParams), WithMethods([]string{"testMethod"})) serviceUrl, _ := NewURL(context.TODO(), "mock2://127.0.0.1:20000", WithParams(serviceUrlParams)) mergedUrl := MergeUrl(serviceUrl, &referenceUrl) assert.Equal(t, "random", mergedUrl.GetParam(constant.CLUSTER_KEY, "")) assert.Equal(t, "1", mergedUrl.GetParam("test2", "")) assert.Equal(t, "1", mergedUrl.GetParam("test3", "")) + assert.Equal(t, "1", mergedUrl.GetParam(constant.RETRIES_KEY, "")) + assert.Equal(t, "1", mergedUrl.GetParam("methods.testMethod."+constant.RETRIES_KEY, "")) }