Skip to content

Commit

Permalink
Ftr: support custom registry group name on nacos for 1.5 (#1513)
Browse files Browse the repository at this point in the history
* Support Custom Registry GroupName on Nacos

* Support Custom Registry GroupName on Nacos

* 优化参数传递方式

Co-authored-by: Changeden <chenzhiduan@unizone.tech>
  • Loading branch information
ChangedenCZD and Changeden committed Oct 17, 2021
1 parent 1dbb573 commit fbe3ed4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions common/constant/key.go
Expand Up @@ -111,6 +111,7 @@ const (
ZONE_KEY = "zone"
ZONE_FORCE_KEY = "zone.force"
REGISTRY_TTL_KEY = "registry.ttl"
REGISTRY_GROUP_KEY = "registry.group"
)

const (
Expand Down
7 changes: 6 additions & 1 deletion registry/nacos/listener.go
Expand Up @@ -188,7 +188,12 @@ func (nl *nacosListener) startListen() error {
return perrors.New("nacos naming namingClient stopped")
}
serviceName := getSubscribeName(nl.listenUrl)
nl.subscribeParam = &vo.SubscribeParam{ServiceName: serviceName, SubscribeCallback: nl.Callback}
groupName := nl.listenUrl.GetParam(constant.REGISTRY_GROUP_KEY, defaultGroup)
nl.subscribeParam = &vo.SubscribeParam{
ServiceName: serviceName,
SubscribeCallback: nl.Callback,
GroupName: groupName,
}
go func() {
_ = nl.namingClient.Client().Subscribe(nl.subscribeParam)
}()
Expand Down
15 changes: 11 additions & 4 deletions registry/nacos/registry.go
Expand Up @@ -83,7 +83,7 @@ func appendParam(target *bytes.Buffer, url *common.URL, key string) {
}
}

func createRegisterParam(url *common.URL, serviceName string) vo.RegisterInstanceParam {
func createRegisterParam(url *common.URL, serviceName string, groupName string) vo.RegisterInstanceParam {
category := getCategory(url)
params := make(map[string]string)

Expand Down Expand Up @@ -111,14 +111,16 @@ func createRegisterParam(url *common.URL, serviceName string) vo.RegisterInstanc
Healthy: true,
Ephemeral: true,
ServiceName: serviceName,
GroupName: groupName,
}
return instance
}

// Register will register the service @url to its nacos registry center
func (nr *nacosRegistry) Register(url *common.URL) error {
serviceName := getServiceName(url)
param := createRegisterParam(url, serviceName)
groupName := nr.URL.GetParam(constant.GROUP_KEY, defaultGroup)
param := createRegisterParam(url, serviceName, groupName)
isRegistry, err := nr.namingClient.Client().RegisterInstance(param)
if err != nil {
return err
Expand All @@ -130,7 +132,7 @@ func (nr *nacosRegistry) Register(url *common.URL) error {
return nil
}

func createDeregisterParam(url *common.URL, serviceName string) vo.DeregisterInstanceParam {
func createDeregisterParam(url *common.URL, serviceName string, groupName string) vo.DeregisterInstanceParam {
if len(url.Ip) == 0 {
url.Ip = localIP
}
Expand All @@ -142,13 +144,15 @@ func createDeregisterParam(url *common.URL, serviceName string) vo.DeregisterIns
Ip: url.Ip,
Port: uint64(port),
ServiceName: serviceName,
GroupName: groupName,
Ephemeral: true,
}
}

func (nr *nacosRegistry) DeRegister(url *common.URL) error {
serviceName := getServiceName(url)
param := createDeregisterParam(url, serviceName)
groupName := nr.URL.GetParam(constant.GROUP_KEY, defaultGroup)
param := createDeregisterParam(url, serviceName, groupName)
isDeRegistry, err := nr.namingClient.Client().DeregisterInstance(param)
if err != nil {
return err
Expand Down Expand Up @@ -181,6 +185,9 @@ func (nr *nacosRegistry) Subscribe(url *common.URL, notifyListener registry.Noti
return perrors.New("nacosRegistry is not available.")
}

groupName := nr.GetParam(constant.GROUP_KEY, defaultGroup)
url.SetParam(constant.REGISTRY_GROUP_KEY, groupName) // update to registry.group

listener, err := nr.subscribe(url)
if err != nil {
if !nr.IsAvailable() {
Expand Down
3 changes: 3 additions & 0 deletions registry/nacos/service_discovery.go
Expand Up @@ -160,6 +160,7 @@ func (n *nacosServiceDiscovery) GetInstances(serviceName string) []registry.Serv
Enable: ins.Enable,
Healthy: ins.Healthy,
Metadata: metadata,
GroupName: n.group,
})
}

Expand Down Expand Up @@ -215,6 +216,7 @@ func (n *nacosServiceDiscovery) GetRequestInstances(serviceNames []string, offse
func (n *nacosServiceDiscovery) AddListener(listener *registry.ServiceInstancesChangedListener) error {
return n.namingClient.Client().Subscribe(&vo.SubscribeParam{
ServiceName: listener.ServiceName,
GroupName: n.group,
SubscribeCallback: func(services []model.SubscribeService, err error) {
if err != nil {
logger.Errorf("Could not handle the subscribe notification because the err is not nil."+
Expand All @@ -236,6 +238,7 @@ func (n *nacosServiceDiscovery) AddListener(listener *registry.ServiceInstancesC
Enable: service.Enable,
Healthy: true,
Metadata: metadata,
GroupName: n.group,
})
}

Expand Down
1 change: 1 addition & 0 deletions registry/service_instance.go
Expand Up @@ -56,6 +56,7 @@ type DefaultServiceInstance struct {
Enable bool
Healthy bool
Metadata map[string]string
GroupName string
}

// GetId will return this instance's id. It should be unique.
Expand Down

0 comments on commit fbe3ed4

Please sign in to comment.