This is an automated email from the ASF dual-hosted git repository.
alexstocks pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/3.0 by this push:
new d50f955eb registry type support all (#2140)
d50f955eb is described below
commit d50f955ebc4371ca4d9d7eb1ae856a48f8fc9d47
Author: bobtthp <[email protected]>
AuthorDate: Sun Dec 11 22:53:05 2022 +0800
registry type support all (#2140)
* registry type support all
* fix test
* set default to interface
* use default protocol registry
* fix unit test
* use swith to judge
* add registry support all test
* Resolve registry name conflicts
* fix ut err
Co-authored-by: bobtthp <[email protected]>
Co-authored-by: bob <[email protected]>
Co-authored-by: bobtthp <[email protected]>
---
common/constant/default.go | 1 +
common/constant/key.go | 3 +++
config/config_utils.go | 4 +++
config/registry_config.go | 53 ++++++++++++++++++++++++++++++++++----
config/registry_config_test.go | 13 ++++++++++
config/service_config.go | 4 +--
config/service_config_test.go | 2 +-
registry/protocol/protocol.go | 4 +--
registry/protocol/protocol_test.go | 6 ++---
9 files changed, 77 insertions(+), 13 deletions(-)
diff --git a/common/constant/default.go b/common/constant/default.go
index 747f409e0..93ab319d0 100644
--- a/common/constant/default.go
+++ b/common/constant/default.go
@@ -89,4 +89,5 @@ const (
const (
ServiceDiscoveryDefaultGroup = "DEFAULT_GROUP"
+ NotAvailable = "N/A"
)
diff --git a/common/constant/key.go b/common/constant/key.go
index 7adafa75b..68c7d4526 100644
--- a/common/constant/key.go
+++ b/common/constant/key.go
@@ -169,6 +169,9 @@ const (
RegistrySimplifiedKey = "simplified"
RegistryNamespaceKey = "registry.namespace"
RegistryGroupKey = "registry.group"
+ RegistryTypeInterface = "interface"
+ RegistryTypeService = "service"
+ RegistryTypeAll = "all"
)
const (
diff --git a/config/config_utils.go b/config/config_utils.go
index f08d1b901..fa6b51b2d 100644
--- a/config/config_utils.go
+++ b/config/config_utils.go
@@ -116,3 +116,7 @@ func verify(s interface{}) error {
func clientNameID(config extension.Config, protocol, address string) string {
return strings.Join([]string{config.Prefix(), protocol, address}, "-")
}
+
+func isValid(addr string) bool {
+ return addr != "" && addr != constant.NotAvailable
+}
diff --git a/config/registry_config.go b/config/registry_config.go
index c8cf359ea..facd26729 100644
--- a/config/registry_config.go
+++ b/config/registry_config.go
@@ -91,7 +91,7 @@ func (c *RegistryConfig) getUrlMap(roleType common.RoleType)
url.Values {
func (c *RegistryConfig) startRegistryConfig() error {
c.translateRegistryAddress()
- if GetApplicationConfig().MetadataType ==
constant.DefaultMetadataStorageType && c.RegistryType == constant.ServiceKey {
+ if GetApplicationConfig().MetadataType ==
constant.DefaultMetadataStorageType && c.RegistryType == constant.ServiceKey ||
c.RegistryType == constant.RegistryTypeAll {
if tmpUrl, err := c.toMetadataReportUrl(); err == nil {
instance.SetMetadataReportInstanceByReg(tmpUrl)
} else {
@@ -149,13 +149,56 @@ func (c *RegistryConfig) GetInstance(roleType
common.RoleType) (registry.Registr
func (c *RegistryConfig) toURL(roleType common.RoleType) (*common.URL, error) {
address := c.translateRegistryAddress()
var registryURLProtocol string
- if c.RegistryType == "service" {
- // service discovery protocol
+ switch c.RegistryType {
+ case constant.RegistryTypeService:
registryURLProtocol = constant.ServiceRegistryProtocol
- } else {
+ case constant.RegistryTypeInterface:
registryURLProtocol = constant.RegistryProtocol
+ default:
+ // default use interface
+ registryURLProtocol = constant.RegistryProtocol
+ }
+ return c.createNewURL(registryURLProtocol, address, roleType)
+}
+
+func (c *RegistryConfig) toURLs(roleType common.RoleType) ([]*common.URL,
error) {
+ address := c.translateRegistryAddress()
+ var urls []*common.URL
+ var err error
+ var registryURL *common.URL
+
+ if !isValid(c.Address) {
+ 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
}
- return common.NewURL(registryURLProtocol+"://"+address,
+ switch c.RegistryType {
+ case constant.RegistryTypeService:
+ if registryURL, err =
c.createNewURL(constant.ServiceRegistryProtocol, address, roleType); err == nil
{
+ urls = append(urls, registryURL)
+ }
+ case constant.RegistryTypeInterface:
+ if registryURL, err = c.createNewURL(constant.RegistryProtocol,
address, roleType); err == nil {
+ urls = append(urls, registryURL)
+ }
+ case constant.RegistryTypeAll:
+ if registryURL, err =
c.createNewURL(constant.ServiceRegistryProtocol, address, roleType); err == nil
{
+ urls = append(urls, registryURL)
+ }
+ if registryURL, err = c.createNewURL(constant.RegistryProtocol,
address, roleType); err == nil {
+ urls = append(urls, registryURL)
+ }
+ default:
+ // default use interface
+ if registryURL, err = c.createNewURL(constant.RegistryProtocol,
address, roleType); err == nil {
+ urls = append(urls, registryURL)
+ }
+ }
+ return urls, err
+}
+
+func (c *RegistryConfig) createNewURL(protocol string, address string,
roleType common.RoleType) (*common.URL, error) {
+ return common.NewURL(protocol+"://"+address,
common.WithParams(c.getUrlMap(roleType)),
common.WithParamsValue(constant.RegistrySimplifiedKey,
strconv.FormatBool(c.Simplified)),
common.WithParamsValue(constant.RegistryKey, c.Protocol),
diff --git a/config/registry_config_test.go b/config/registry_config_test.go
index 9c0be8c8f..cf9ff61d0 100644
--- a/config/registry_config_test.go
+++ b/config/registry_config_test.go
@@ -66,6 +66,19 @@ func TestLoadRegistries1(t *testing.T) {
assert.Equal(t, "127.0.0.2:2181", urls[0].Location)
}
+func TestRegistryTypeAll(t *testing.T) {
+ target := []string{"test"}
+ regs := map[string]*RegistryConfig{
+ "test": {
+ Protocol: "mock",
+ Address: "127.0.0.2:2181",
+ RegistryType: constant.RegistryTypeAll,
+ },
+ }
+ urls := loadRegistries(target, regs, common.PROVIDER)
+ assert.Equal(t, 2, len(urls))
+}
+
func TestTranslateRegistryAddress(t *testing.T) {
reg := new(RegistryConfig)
reg.Address = "nacos://127.0.0.1:8848"
diff --git a/config/service_config.go b/config/service_config.go
index d669c8c5c..92a4e269c 100644
--- a/config/service_config.go
+++ b/config/service_config.go
@@ -375,11 +375,11 @@ func loadRegistries(registryIds []string, registries
map[string]*RegistryConfig,
}
if target {
- if registryURL, err := registryConf.toURL(roleType);
err != nil {
+ if urls, err := registryConf.toURLs(roleType); err !=
nil {
logger.Errorf("The registry id: %s url is
invalid, error: %#v", k, err)
panic(err)
} else {
- registryURLs = append(registryURLs, registryURL)
+ registryURLs = append(registryURLs, urls...)
}
}
}
diff --git a/config/service_config_test.go b/config/service_config_test.go
index 5a0639f42..e2a9bebdc 100644
--- a/config/service_config_test.go
+++ b/config/service_config_test.go
@@ -96,7 +96,7 @@ func TestNewServiceConfigBuilder(t *testing.T) {
t.Run("loadRegistries&loadProtocol&getRandomPort", func(t *testing.T) {
registries := loadRegistries(serviceConfig.RegistryIDs,
serviceConfig.RCRegistriesMap, common.PROVIDER)
assert.Equal(t, len(registries), 1)
- assert.Equal(t, registries[0].Protocol, "registry")
+ assert.Equal(t, registries[0].Protocol,
constant.RegistryProtocol)
assert.Equal(t, registries[0].Port, "8848")
assert.Equal(t, registries[0].GetParam("registry.role", "1"),
"3")
assert.Equal(t, registries[0].GetParam("registry", "zk"),
"nacos")
diff --git a/registry/protocol/protocol.go b/registry/protocol/protocol.go
index a869ab716..bc3340dea 100644
--- a/registry/protocol/protocol.go
+++ b/registry/protocol/protocol.go
@@ -82,14 +82,14 @@ func newRegistryProtocol() *registryProtocol {
func (proto *registryProtocol) getRegistry(registryUrl *common.URL)
registry.Registry {
var err error
- reg, loaded := proto.registries.Load(registryUrl.Location)
+ reg, loaded := proto.registries.Load(registryUrl.PrimitiveURL)
if !loaded {
reg, err = extension.GetRegistry(registryUrl.Protocol,
registryUrl)
if err != nil {
logger.Errorf("Registry can not connect success,
program is going to panic.Error message is %s", err.Error())
panic(err)
}
- proto.registries.Store(registryUrl.Location, reg)
+ proto.registries.Store(registryUrl.PrimitiveURL, reg)
}
return reg.(registry.Registry)
}
diff --git a/registry/protocol/protocol_test.go
b/registry/protocol/protocol_test.go
index 7ed3711f9..2c9ff6133 100644
--- a/registry/protocol/protocol_test.go
+++ b/registry/protocol/protocol_test.go
@@ -225,7 +225,7 @@ func TestExportWithOverrideListener(t *testing.T) {
regProtocol := newRegistryProtocol()
url := exporterNormal(t, regProtocol)
var reg *registry.MockRegistry
- if regI, loaded := regProtocol.registries.Load(url.Location); loaded {
+ if regI, loaded := regProtocol.registries.Load(url.PrimitiveURL);
loaded {
reg = regI.(*registry.MockRegistry)
} else {
assert.Fail(t, "regProtocol.registries.Load can not be loaded")
@@ -252,7 +252,7 @@ func TestExportWithServiceConfig(t *testing.T) {
common_cfg.GetEnvInstance().SetDynamicConfiguration(dc)
regProtocol := newRegistryProtocol()
url := exporterNormal(t, regProtocol)
- if _, loaded := regProtocol.registries.Load(url.Location); !loaded {
+ if _, loaded := regProtocol.registries.Load(url.PrimitiveURL); !loaded {
assert.Fail(t, "regProtocol.registries.Load can not be loaded")
return
}
@@ -275,7 +275,7 @@ func TestExportWithApplicationConfig(t *testing.T) {
common_cfg.GetEnvInstance().SetDynamicConfiguration(dc)
regProtocol := newRegistryProtocol()
url := exporterNormal(t, regProtocol)
- if _, loaded := regProtocol.registries.Load(url.Location); !loaded {
+ if _, loaded := regProtocol.registries.Load(url.PrimitiveURL); !loaded {
assert.Fail(t, "regProtocol.registries.Load can not be loaded")
return
}