Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
156 changes: 156 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* 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 internal contains dubbo-go-internal code, to avoid polluting
// the top-level dubbo-go package. It must not import any dubbo-go symbols
// except internal symbols to avoid circular dependencies.
package internal

import (
"net/url"
"strconv"
"strings"
)

import (
"github.com/dubbogo/gost/log/logger"
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/global"
)

func LoadRegistries(registryIds []string, registries map[string]*global.RegistryConfig, roleType common.RoleType) []*common.URL {
var registryURLs []*common.URL

for k, registryConf := range registries {
target := false

if len(registryIds) == 0 || (len(registryIds) == 1 && registryIds[0] == "") {
target = true
} else {
for _, tr := range registryIds {
if tr == k {
target = true
break
}
}
}

if target {
if urls, err := toURLs(registryConf, roleType); err != nil {
logger.Errorf("The registry id: %s url is invalid, error: %#v", k, err)
panic(err)
} else {
for _, u := range urls {
u.AddParam(constant.RegistryIdKey, k)
}
registryURLs = append(registryURLs, urls...)
}
}
}

return registryURLs
}

func toURLs(registriesConfig *global.RegistryConfig, roleType common.RoleType) ([]*common.URL, error) {
address := translateRegistryAddress(registriesConfig)
var urls []*common.URL
var err error
var registryURL *common.URL

if address == "" || address == constant.NotAvailable {
logger.Infof("Empty or N/A registry address found, the process will work with no registry enabled " +
"which means that the address of this instance will not be registered and not able to be found by other consumer instances.")
return urls, nil
}
switch registriesConfig.RegistryType {
case constant.RegistryTypeService:
// service discovery protocol
if registryURL, err = createNewURL(registriesConfig, constant.ServiceRegistryProtocol, address, roleType); err == nil {
urls = append(urls, registryURL)
}
case constant.RegistryTypeInterface:
if registryURL, err = createNewURL(registriesConfig, constant.RegistryProtocol, address, roleType); err == nil {
urls = append(urls, registryURL)
}
case constant.RegistryTypeAll:
if registryURL, err = createNewURL(registriesConfig, constant.ServiceRegistryProtocol, address, roleType); err == nil {
urls = append(urls, registryURL)
}
if registryURL, err = createNewURL(registriesConfig, constant.RegistryProtocol, address, roleType); err == nil {
urls = append(urls, registryURL)
}
default:
if registryURL, err = createNewURL(registriesConfig, constant.ServiceRegistryProtocol, address, roleType); err == nil {
urls = append(urls, registryURL)
}
}
return urls, err
}

func createNewURL(registriesConfig *global.RegistryConfig, protocol string, address string, roleType common.RoleType) (*common.URL, error) {
return common.NewURL(protocol+"://"+address,
common.WithParams(getUrlMap(registriesConfig, roleType)),
common.WithParamsValue(constant.RegistrySimplifiedKey, strconv.FormatBool(registriesConfig.Simplified)),
common.WithParamsValue(constant.RegistryKey, registriesConfig.Protocol),
common.WithParamsValue(constant.RegistryNamespaceKey, registriesConfig.Namespace),
common.WithParamsValue(constant.RegistryTimeoutKey, registriesConfig.Timeout),
common.WithUsername(registriesConfig.Username),
common.WithPassword(registriesConfig.Password),
common.WithLocation(registriesConfig.Address),
)
}

func translateRegistryAddress(registriesConfig *global.RegistryConfig) string {
if strings.Contains(registriesConfig.Address, "://") {
u, err := url.Parse(registriesConfig.Address)
if err != nil {
logger.Errorf("The registry url is invalid, error: %#v", err)
panic(err)
}
registriesConfig.Protocol = u.Scheme
registriesConfig.Address = strings.Join([]string{u.Host, u.Path}, "")
}
return registriesConfig.Address
}

func getUrlMap(registriesConfig *global.RegistryConfig, roleType common.RoleType) url.Values {
urlMap := url.Values{}
urlMap.Set(constant.RegistryGroupKey, registriesConfig.Group)
urlMap.Set(constant.RegistryRoleKey, strconv.Itoa(int(roleType)))
urlMap.Set(constant.RegistryKey, registriesConfig.Protocol)
urlMap.Set(constant.RegistryTimeoutKey, registriesConfig.Timeout)
urlMap.Set(constant.RegistryKey+"."+constant.RegistryLabelKey, strconv.FormatBool(true))
urlMap.Set(constant.RegistryKey+"."+constant.PreferredKey, strconv.FormatBool(registriesConfig.Preferred))
urlMap.Set(constant.RegistryKey+"."+constant.RegistryZoneKey, registriesConfig.Zone)
urlMap.Set(constant.RegistryKey+"."+constant.WeightKey, strconv.FormatInt(registriesConfig.Weight, 10))
urlMap.Set(constant.RegistryTTLKey, registriesConfig.TTL)
urlMap.Set(constant.ClientNameKey, clientNameID(registriesConfig.Protocol, registriesConfig.Address))
urlMap.Set(constant.RegistryTypeKey, registriesConfig.RegistryType)

for k, v := range registriesConfig.Params {
urlMap.Set(k, v)
}
return urlMap
}

func clientNameID(protocol, address string) string {
return strings.Join([]string{constant.RegistryConfigPrefix, protocol, address}, "-")
}
6 changes: 3 additions & 3 deletions server/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/graceful_shutdown"
"dubbo.apache.org/dubbo-go/v3/internal"
"dubbo.apache.org/dubbo-go/v3/protocol/base"
"dubbo.apache.org/dubbo-go/v3/protocol/protocolwrapper"
)
Expand Down Expand Up @@ -141,7 +141,7 @@ func (svcOpts *ServiceOptions) Export() error {

regUrls := make([]*common.URL, 0)
if !svcConf.NotRegister {
regUrls = config.LoadRegistries(svcConf.RegistryIDs, svcOpts.registriesCompat, common.PROVIDER)
regUrls = internal.LoadRegistries(svcConf.RegistryIDs, svcOpts.Registries, common.PROVIDER)
}

urlMap := svcOpts.getUrlMap()
Expand Down Expand Up @@ -327,7 +327,7 @@ func (svcOpts *ServiceOptions) Implement(rpcService common.RPCService) {

func (svcOpts *ServiceOptions) getUrlMap() url.Values {
svcConf := svcOpts.Service
app := svcOpts.applicationCompat
app := svcOpts.Application
metrics := svcOpts.srvOpts.Metrics
tracing := svcOpts.srvOpts.Otel.TracingConfig

Expand Down
17 changes: 8 additions & 9 deletions server/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/protocol/base"
)
Expand Down Expand Up @@ -302,7 +301,7 @@ func TestGetUrlMapBasic(t *testing.T) {
Version: "1.0.0",
},
Provider: &global.ProviderConfig{},
applicationCompat: &config.ApplicationConfig{
Application: &global.ApplicationConfig{
Name: "test-app",
Organization: "test-org",
Module: "test-module",
Expand Down Expand Up @@ -343,7 +342,7 @@ func TestGetUrlMapWithParams(t *testing.T) {
Serialization: constant.JSONSerialization,
},
Provider: &global.ProviderConfig{},
applicationCompat: &config.ApplicationConfig{
Application: &global.ApplicationConfig{
Name: "test-app",
Organization: "test-org",
},
Expand Down Expand Up @@ -375,7 +374,7 @@ func TestGetUrlMapWithGroupAndVersion(t *testing.T) {
Params: map[string]string{},
},
Provider: &global.ProviderConfig{},
applicationCompat: &config.ApplicationConfig{
Application: &global.ApplicationConfig{
Name: "test-app",
},
srvOpts: &ServerOptions{
Expand Down Expand Up @@ -412,7 +411,7 @@ func TestGetUrlMapWithMethods(t *testing.T) {
},
},
Provider: &global.ProviderConfig{},
applicationCompat: &config.ApplicationConfig{
Application: &global.ApplicationConfig{
Name: "test-app",
},
srvOpts: &ServerOptions{
Expand Down Expand Up @@ -545,7 +544,7 @@ func TestGetUrlMapWithTpsLimit(t *testing.T) {
Params: map[string]string{},
},
Provider: &global.ProviderConfig{},
applicationCompat: &config.ApplicationConfig{
Application: &global.ApplicationConfig{
Name: "test-app",
},
srvOpts: &ServerOptions{
Expand Down Expand Up @@ -578,7 +577,7 @@ func TestGetUrlMapWithExecuteLimit(t *testing.T) {
Params: map[string]string{},
},
Provider: &global.ProviderConfig{},
applicationCompat: &config.ApplicationConfig{
Application: &global.ApplicationConfig{
Name: "test-app",
},
srvOpts: &ServerOptions{
Expand Down Expand Up @@ -609,7 +608,7 @@ func TestGetUrlMapWithAuthAndParamSign(t *testing.T) {
Params: map[string]string{},
},
Provider: &global.ProviderConfig{},
applicationCompat: &config.ApplicationConfig{
Application: &global.ApplicationConfig{
Name: "test-app",
},
srvOpts: &ServerOptions{
Expand Down Expand Up @@ -639,7 +638,7 @@ func TestGetUrlMapWithAccessLog(t *testing.T) {
Params: map[string]string{},
},
Provider: &global.ProviderConfig{},
applicationCompat: &config.ApplicationConfig{
Application: &global.ApplicationConfig{
Name: "test-app",
},
srvOpts: &ServerOptions{
Expand Down
63 changes: 0 additions & 63 deletions server/compat.go

This file was deleted.

Loading
Loading