Skip to content

Commit

Permalink
Merge pull request #1581 from apache/fix/nacos-2.0
Browse files Browse the repository at this point in the history
fix: nacos servicediscovery group、namespace
  • Loading branch information
justxuewei authored Nov 11, 2021
2 parents 0c85c55 + b02124d commit fce2f73
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 26 deletions.
2 changes: 0 additions & 2 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ const (
ServiceRegistryProtocol = "service-discovery-registry"
RegistryRoleKey = "registry.role"
RegistryDefaultKey = "registry.default"
RegistryUsernameKey = "registry.username"
RegistryPasswordKey = "registry.password"
RegistryAccessKey = "registry.accesskey"
RegistrySecretKey = "registry.secretkey"
RegistryTimeoutKey = "registry.timeout"
Expand Down
17 changes: 2 additions & 15 deletions registry/base_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (r *BaseRegistry) providerRegistry(c *common.URL, params url.Values, f crea
if c.Path == "" || len(c.Methods) == 0 {
return "", "", perrors.Errorf("conf{Path:%s, Methods:%s}", c.Path, c.Methods)
}
dubboPath = fmt.Sprintf("/dubbo/%s/%s", r.service(c), common.DubboNodes[common.PROVIDER])
dubboPath = fmt.Sprintf("/%s/%s/%s", r.URL.GetParam(constant.RegistryGroupKey, "dubbo"), r.service(c), common.DubboNodes[common.PROVIDER])
if f != nil {
err = f(dubboPath)
}
Expand Down Expand Up @@ -346,9 +346,7 @@ func (r *BaseRegistry) providerRegistry(c *common.URL, params url.Values, f crea

s, _ := url.QueryUnescape(params.Encode())
rawURL = fmt.Sprintf("%s://%s%s?%s", c.Protocol, host, c.Path, s)

// Print your own registration service providers.
dubboPath = fmt.Sprintf("/dubbo/%s/%s", r.service(c), (common.RoleType(common.PROVIDER)).String())
logger.Debugf("provider path:%s, url:%s", dubboPath, rawURL)
return dubboPath, rawURL, nil
}
Expand All @@ -360,16 +358,7 @@ func (r *BaseRegistry) consumerRegistry(c *common.URL, params url.Values, f crea
rawURL string
err error
)
dubboPath = fmt.Sprintf("/dubbo/%s/%s", r.service(c), common.DubboNodes[common.CONSUMER])

if f != nil {
err = f(dubboPath)
}
if err != nil {
logger.Errorf("facadeBasedRegistry.CreatePath(path{%s}) = error{%v}", dubboPath, perrors.WithStack(err))
return "", "", perrors.WithStack(err)
}
dubboPath = fmt.Sprintf("/dubbo/%s/%s", r.service(c), common.DubboNodes[common.PROVIDER])
dubboPath = fmt.Sprintf("/%s/%s/%s", r.URL.GetParam(constant.RegistryGroupKey, "dubbo"), r.service(c), common.DubboNodes[common.PROVIDER])

if f != nil {
err = f(dubboPath)
Expand All @@ -383,8 +372,6 @@ func (r *BaseRegistry) consumerRegistry(c *common.URL, params url.Values, f crea
params.Add("protocol", c.Protocol)
s, _ := url.QueryUnescape(params.Encode())
rawURL = fmt.Sprintf("consumer://%s%s?%s", localIP, c.Path, s)
dubboPath = fmt.Sprintf("/dubbo/%s/%s", r.service(c), (common.RoleType(common.CONSUMER)).String())

logger.Debugf("consumer path:%s, url:%s", dubboPath, rawURL)
return dubboPath, rawURL, nil
}
Expand Down
3 changes: 2 additions & 1 deletion registry/nacos/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ func newNacosRegistry(url *common.URL) (registry.Registry, error) {
logger.Infof("[Nacos Registry] New nacos registry with url = %+v", url.ToMap())
// key transfer: registry -> nacos
url.SetParam(constant.NacosNamespaceID, url.GetParam(constant.RegistryNamespaceKey, ""))
url.SetParam(constant.NacosUsername, url.GetParam(constant.RegistryUsernameKey, ""))
url.SetParam(constant.NacosUsername, url.Username)
url.SetParam(constant.NacosPassword, url.Password)
url.SetParam(constant.NacosAccessKey, url.GetParam(constant.RegistryAccessKey, ""))
url.SetParam(constant.NacosSecretKey, url.GetParam(constant.RegistrySecretKey, ""))
url.SetParam(constant.TimeoutKey, url.GetParam(constant.RegistryTimeoutKey, ""))
Expand Down
12 changes: 6 additions & 6 deletions registry/nacos/service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,21 +332,21 @@ func newNacosServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, error
discoveryURL := common.NewURLWithOptions(
common.WithParams(url.GetParams()),
common.WithParamsValue(constant.TimeoutKey, url.GetParam(constant.RegistryTimeoutKey, constant.DefaultRegTimeout)),
common.WithParamsValue(constant.RegistryUsernameKey, url.GetParam(constant.RegistryUsernameKey, "")),
common.WithParamsValue(constant.RegistryPasswordKey, url.GetParam(constant.RegistryPasswordKey, "")),
common.WithParamsValue(constant.NacosGroupKey, url.GetParam(constant.RegistryGroupKey, defaultGroup)),
common.WithParamsValue(constant.NacosUsername, url.Username),
common.WithParamsValue(constant.NacosPassword, url.Password),
common.WithParamsValue(constant.NacosNamespaceID, url.GetParam(constant.RegistryNamespaceKey, "")))
discoveryURL.Location = url.Location
discoveryURL.Username = url.Username
discoveryURL.Password = url.Password
client, err := nacos.NewNacosClientByURL(discoveryURL)
if err != nil {
return nil, perrors.WithMessage(err, "create nacos namingClient failed.")
}

descriptor := fmt.Sprintf("nacos-service-discovery[%s]", discoveryURL.Location)

group := discoveryURL.Group()
if len(group) == 0 {
group = defaultGroup
}
group := url.GetParam(constant.RegistryGroupKey, defaultGroup)
newInstance := &nacosServiceDiscovery{
group: group,
namingClient: client,
Expand Down
4 changes: 2 additions & 2 deletions registry/zookeeper/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (r *zkRegistry) InitListeners() {
regConfigListener.Close()
}
newDataListener.SubscribeURL(regConfigListener.subscribeURL, NewRegistryConfigurationListener(r.client, r, regConfigListener.subscribeURL))
go r.listener.ListenServiceEvent(regConfigListener.subscribeURL, fmt.Sprintf("/dubbo/%s/"+constant.DefaultCategory, url.QueryEscape(regConfigListener.subscribeURL.Service())), newDataListener)
go r.listener.ListenServiceEvent(regConfigListener.subscribeURL, fmt.Sprintf("/%s/%s/"+constant.DefaultCategory, r.URL.GetParam(constant.RegistryGroupKey, "dubbo"), url.QueryEscape(regConfigListener.subscribeURL.Service())), newDataListener)

}
}
Expand Down Expand Up @@ -283,7 +283,7 @@ func (r *zkRegistry) getListener(conf *common.URL) (*RegistryConfigurationListen
// Interested register to dataconfig.
r.dataListener.SubscribeURL(conf, zkListener)

go r.listener.ListenServiceEvent(conf, fmt.Sprintf("/dubbo/%s/"+constant.DefaultCategory, url.QueryEscape(conf.Service())), r.dataListener)
go r.listener.ListenServiceEvent(conf, fmt.Sprintf("/%s/%s/"+constant.DefaultCategory, r.URL.GetParam(constant.RegistryGroupKey, "dubbo"), url.QueryEscape(conf.Service())), r.dataListener)

return zkListener, nil
}
Expand Down

0 comments on commit fce2f73

Please sign in to comment.