Skip to content
Snippets Groups Projects
Unverified Commit eeee72f4 authored by Xin.Zh's avatar Xin.Zh Committed by GitHub
Browse files

Merge pull request #124 from hxmhlt/master

Add support for zookeeper registry cluster
parents 8238cab9 7be16012
Branches
Tags
No related merge requests found
...@@ -136,6 +136,11 @@ func WithPath(path string) option { ...@@ -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 { func NewURLWithOptions(opts ...option) *URL {
url := &URL{} url := &URL{}
for _, opt := range opts { for _, opt := range opts {
......
...@@ -68,13 +68,28 @@ func loadRegistries(targetRegistries string, registries map[string]*RegistryConf ...@@ -68,13 +68,28 @@ func loadRegistries(targetRegistries string, registries map[string]*RegistryConf
} }
if target { if target {
url, err := common.NewURL( var (
context.TODO(), 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, constant.REGISTRY_PROTOCOL+"://"+registryConf.Address,
common.WithParams(registryConf.getUrlMap(roleType)), common.WithParams(registryConf.getUrlMap(roleType)),
common.WithUsername(registryConf.Username), common.WithUsername(registryConf.Username),
common.WithPassword(registryConf.Password), common.WithPassword(registryConf.Password),
) )
}
if err != nil { if err != nil {
logger.Errorf("The registry id:%s url is invalid ,and will skip the registry, error: %#v", k, err) logger.Errorf("The registry id:%s url is invalid ,and will skip the registry, error: %#v", k, err)
......
/*
* 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)
}
...@@ -125,7 +125,8 @@ func ValidateZookeeperClient(container zkClientFacade, opts ...Option) error { ...@@ -125,7 +125,8 @@ func ValidateZookeeperClient(container zkClientFacade, opts ...Option) error {
url.GetParam(constant.REGISTRY_TIMEOUT_KEY, constant.DEFAULT_REG_TIMEOUT), err.Error()) url.GetParam(constant.REGISTRY_TIMEOUT_KEY, constant.DEFAULT_REG_TIMEOUT), err.Error())
return perrors.WithMessagef(err, "newZookeeperClient(address:%+v)", url.Location) 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 { if err != nil {
logger.Warnf("newZookeeperClient(name{%s}, zk addresss{%v}, timeout{%d}) = error{%v}", logger.Warnf("newZookeeperClient(name{%s}, zk addresss{%v}, timeout{%d}) = error{%v}",
opions.zkName, url.Location, timeout.String(), err) opions.zkName, url.Location, timeout.String(), err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment