You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@eventmesh.apache.org by "walleliu1016 (via GitHub)" <gi...@apache.org> on 2023/02/02 06:53:30 UTC

[GitHub] [incubator-eventmesh] walleliu1016 commented on a diff in pull request #2960: [ISSUE #2941] Refactor registry nacos

walleliu1016 commented on code in PR #2960:
URL: https://github.com/apache/incubator-eventmesh/pull/2960#discussion_r1094110437


##########
eventmesh-server-go/plugin/registry/nacos/nacos.go:
##########
@@ -0,0 +1,241 @@
+// 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 nacos
+
+import (
+	"fmt"
+	"github.com/apache/incubator-eventmesh/eventmesh-server-go/config"
+	"github.com/apache/incubator-eventmesh/eventmesh-server-go/plugin"
+	"github.com/apache/incubator-eventmesh/eventmesh-server-go/plugin/registry"
+	"github.com/gogf/gf/util/gconv"
+	"github.com/nacos-group/nacos-sdk-go/v2/clients"
+	"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
+	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
+	"github.com/nacos-group/nacos-sdk-go/v2/vo"
+	"go.uber.org/atomic"
+	"net"
+	"strconv"
+	"strings"
+	"sync"
+	"time"
+)
+
+var (
+	defaultConnectTimeout uint64  = 5000
+	DefaultClusterName            = "DEFAULT"
+	DefaultGroupName              = "DEFAULT_GROUP"
+	DefaultWeight         float64 = 10
+	protoList                     = []string{"TCP", "HTTP", "GRPC"}
+)
+
+func init() {
+	plugin.Register("nacos", &Registry{})
+}
+
+type Registry struct {
+	initStatus    *atomic.Bool
+	startStatus   *atomic.Bool
+	cfg           *Config
+	client        naming_client.INamingClient
+	registryInfos *sync.Map // map[string]*registry.EventMeshRegisterInfo
+}
+
+func (r *Registry) Type() string {
+	return registry.Type
+}
+
+func (r *Registry) Setup(name string, dec plugin.Decoder) error {
+	conf := &Config{}
+	if err := dec.Decode(conf); err != nil {
+		return err
+	}
+	r.cfg = conf
+	return nil
+}
+
+func (r *Registry) Init() error {
+	r.initStatus = atomic.NewBool(true)
+	r.startStatus = atomic.NewBool(false)
+	r.registryInfos = new(sync.Map)
+	return nil
+}
+
+func (r *Registry) Start() error {
+	var p vo.NacosClientParam
+	var addresses []string
+
+	if len(r.cfg.AddressList) > 0 {
+		addresses = strings.Split(r.cfg.AddressList, ",")
+	}
+
+	for _, address := range addresses {
+		ip, port, err := net.SplitHostPort(address)
+		if err != nil {
+			return err
+		}
+		p.ServerConfigs = append(p.ServerConfigs, constant.ServerConfig{IpAddr: ip, Port: gconv.Uint64(port)})
+	}
+
+	p.ClientConfig = &constant.ClientConfig{
+		TimeoutMs: defaultConnectTimeout,
+		CacheDir:  r.cfg.CacheDir,
+	}
+	cli, err := clients.NewNamingClient(p)
+	if err != nil {
+		return err
+	}
+
+	r.startStatus.Store(true)
+	r.client = cli
+	return nil
+}
+
+func (r *Registry) Shutdown() error {
+	r.startStatus.Store(false)
+	r.initStatus.Store(false)
+	r.client.CloseClient()
+	return nil
+}
+
+func (r *Registry) FindEventMeshInfoByCluster(clusterName string) ([]*registry.EventMeshDataInfo, error) {
+	var infos []*registry.EventMeshDataInfo
+	meshServerName := config.GlobalConfig().Common.Name
+	cluster := config.GlobalConfig().Common.Cluster
+	for _, proto := range protoList {
+		ins, err := r.client.SelectInstances(vo.SelectInstancesParam{
+			Clusters:    []string{clusterName},
+			ServiceName: fmt.Sprintf("%v-%v", meshServerName, proto),
+			GroupName:   cluster,
+			HealthyOnly: true,
+		})
+		if err != nil {
+			return nil, err
+		}
+		if len(ins) == 0 {
+			continue
+		}
+		for _, in := range ins {
+			infos = append(infos, &registry.EventMeshDataInfo{
+				EventMeshClusterName: in.ClusterName,
+				EventMeshName:        in.ServiceName,
+				Endpoint:             fmt.Sprintf("%v:%v", in.Ip, in.Port),
+				LastUpdateTimestamp:  time.Time{},
+				Metadata:             in.Metadata,
+			})
+		}
+	}
+
+	return infos, nil
+}
+
+func (r *Registry) FindAllEventMeshInfo() ([]*registry.EventMeshDataInfo, error) {
+	var infos []*registry.EventMeshDataInfo
+	meshServerName := config.GlobalConfig().Common.Name
+
+	for _, proto := range protoList {
+		ins, err := r.client.SelectInstances(vo.SelectInstancesParam{
+			Clusters:    []string{},

Review Comment:
   `SelectAllInstances` will return all instances, but `SelectInstances` only return the instances of healthy
   ```
   	// SelectAllInstances return all instances,include healthy=false,enable=false,weight<=0
   	// ServiceName require
   	// Clusters optional,default:DEFAULT
   	// GroupName optional,default:DEFAULT_GROUP
   	SelectAllInstances(param vo.SelectAllInstancesParam) ([]model.Instance, error)
   
   	// SelectInstances only return the instances of healthy=${HealthyOnly},enable=true and weight>0
   	// ServiceName require
   	// Clusters optional,default:DEFAULT
   	// GroupName optional,default:DEFAULT_GROUP
   	// HealthyOnly optional
   	SelectInstances(param vo.SelectInstancesParam) ([]model.Instance, error)
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org