diff --git a/common/url.go b/common/url.go index 47b44cf57feb9cc9eadb18e597a935073918fe99..c594df235134d71d98b83c11b2f6eb9312283aae 100644 --- a/common/url.go +++ b/common/url.go @@ -136,6 +136,11 @@ func WithPath(path string) option { } } +func WithLocation(location string) option { + return func(url *URL) { + url.Location = location + } +} func NewURLWithOptions(opts ...option) *URL { url := &URL{} for _, opt := range opts { diff --git a/config/registry_config.go b/config/registry_config.go index 1a926b459e598ff313e141a39956dc88cb3daad3..3d54c348aadd2a2f836849d12e50887c36909d31 100644 --- a/config/registry_config.go +++ b/config/registry_config.go @@ -68,13 +68,28 @@ func loadRegistries(targetRegistries string, registries map[string]*RegistryConf } if target { - url, err := common.NewURL( - context.TODO(), - constant.REGISTRY_PROTOCOL+"://"+registryConf.Address, - common.WithParams(registryConf.getUrlMap(roleType)), - common.WithUsername(registryConf.Username), - common.WithPassword(registryConf.Password), + var ( + url common.URL + err error ) + if addresses := strings.Split(registryConf.Address, ","); len(addresses) > 1 { + url, err = common.NewURL( + context.Background(), + constant.REGISTRY_PROTOCOL+"://"+addresses[0], + common.WithParams(registryConf.getUrlMap(roleType)), + common.WithUsername(registryConf.Username), + common.WithPassword(registryConf.Password), + common.WithLocation(registryConf.Address), + ) + } else { + url, err = common.NewURL( + context.Background(), + constant.REGISTRY_PROTOCOL+"://"+registryConf.Address, + common.WithParams(registryConf.getUrlMap(roleType)), + common.WithUsername(registryConf.Username), + common.WithPassword(registryConf.Password), + ) + } if err != nil { logger.Errorf("The registry id:%s url is invalid ,and will skip the registry, error: %#v", k, err) diff --git a/config/registry_config_test.go b/config/registry_config_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f600a21a0117572349aaf4de1bdee5e1270f67b4 --- /dev/null +++ b/config/registry_config_test.go @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package config + +import ( + "fmt" + "testing" +) +import ( + "github.com/apache/dubbo-go/common" + "github.com/stretchr/testify/assert" +) + +func Test_loadRegistries(t *testing.T) { + target := "shanghai1" + regs := map[string]*RegistryConfig{ + + "shanghai1": { + Protocol: "mock", + TimeoutStr: "2s", + Group: "shanghai_idc", + Address: "127.0.0.2:2181,128.0.0.1:2181", + Username: "user1", + Password: "pwd1", + }, + } + urls := loadRegistries(target, regs, common.CONSUMER) + fmt.Println(urls[0]) + assert.Equal(t, "127.0.0.2:2181,128.0.0.1:2181", urls[0].Location) +} +func Test_loadRegistries1(t *testing.T) { + target := "shanghai1" + regs := map[string]*RegistryConfig{ + + "shanghai1": { + Protocol: "mock", + TimeoutStr: "2s", + Group: "shanghai_idc", + Address: "127.0.0.2:2181", + Username: "user1", + Password: "pwd1", + }, + } + urls := loadRegistries(target, regs, common.CONSUMER) + fmt.Println(urls[0]) + assert.Equal(t, "127.0.0.2:2181", urls[0].Location) +} diff --git a/remoting/zookeeper/client.go b/remoting/zookeeper/client.go index d1d5f9900dbd67bbd061b411eaf2e40ffa0e7561..ffd98391af0b031ecb3dd110f9fbb63887bd3704 100644 --- a/remoting/zookeeper/client.go +++ b/remoting/zookeeper/client.go @@ -125,7 +125,8 @@ func ValidateZookeeperClient(container zkClientFacade, opts ...Option) error { url.GetParam(constant.REGISTRY_TIMEOUT_KEY, constant.DEFAULT_REG_TIMEOUT), err.Error()) return perrors.WithMessagef(err, "newZookeeperClient(address:%+v)", url.Location) } - newClient, err := newZookeeperClient(opions.zkName, []string{url.Location}, timeout) + zkAddresses := strings.Split(url.Location, ",") + newClient, err := newZookeeperClient(opions.zkName, zkAddresses, timeout) if err != nil { logger.Warnf("newZookeeperClient(name{%s}, zk addresss{%v}, timeout{%d}) = error{%v}", opions.zkName, url.Location, timeout.String(), err)