From cb02ef34e9873cba04ce6842eac0bcf83e4e3b91 Mon Sep 17 00:00:00 2001 From: bob Date: Fri, 23 Dec 2022 22:19:46 +0800 Subject: [PATCH] del front --- 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 ++-- 6 files changed, 13 insertions(+), 69 deletions(-) diff --git a/config/registry_config.go b/config/registry_config.go index facd26729f..c8cf359ea5 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 || c.RegistryType == constant.RegistryTypeAll { + if GetApplicationConfig().MetadataType == constant.DefaultMetadataStorageType && c.RegistryType == constant.ServiceKey { if tmpUrl, err := c.toMetadataReportUrl(); err == nil { instance.SetMetadataReportInstanceByReg(tmpUrl) } else { @@ -149,56 +149,13 @@ 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 - switch c.RegistryType { - case constant.RegistryTypeService: + if c.RegistryType == "service" { + // service discovery protocol registryURLProtocol = constant.ServiceRegistryProtocol - case constant.RegistryTypeInterface: + } else { 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 } - 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, + return common.NewURL(registryURLProtocol+"://"+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 cf9ff61d05..9c0be8c8f3 100644 --- a/config/registry_config_test.go +++ b/config/registry_config_test.go @@ -66,19 +66,6 @@ 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 92a4e269cf..d669c8c5c3 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 urls, err := registryConf.toURLs(roleType); err != nil { + if registryURL, err := registryConf.toURL(roleType); err != nil { logger.Errorf("The registry id: %s url is invalid, error: %#v", k, err) panic(err) } else { - registryURLs = append(registryURLs, urls...) + registryURLs = append(registryURLs, registryURL) } } } diff --git a/config/service_config_test.go b/config/service_config_test.go index e2a9bebdca..5a0639f420 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, constant.RegistryProtocol) + assert.Equal(t, registries[0].Protocol, "registry") 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 bc3340dea3..a869ab716f 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.PrimitiveURL) + reg, loaded := proto.registries.Load(registryUrl.Location) 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.PrimitiveURL, reg) + proto.registries.Store(registryUrl.Location, reg) } return reg.(registry.Registry) } diff --git a/registry/protocol/protocol_test.go b/registry/protocol/protocol_test.go index 2c9ff6133b..7ed3711f92 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.PrimitiveURL); loaded { + if regI, loaded := regProtocol.registries.Load(url.Location); 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.PrimitiveURL); !loaded { + if _, loaded := regProtocol.registries.Load(url.Location); !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.PrimitiveURL); !loaded { + if _, loaded := regProtocol.registries.Load(url.Location); !loaded { assert.Fail(t, "regProtocol.registries.Load can not be loaded") return }