Skip to content

Commit

Permalink
del front
Browse files Browse the repository at this point in the history
  • Loading branch information
bob committed Dec 23, 2022
1 parent 21516be commit cb02ef3
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 69 deletions.
53 changes: 5 additions & 48 deletions config/registry_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
13 changes: 0 additions & 13 deletions config/registry_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion config/service_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions registry/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions registry/protocol/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit cb02ef3

Please sign in to comment.