diff --git a/cluster/node/node.go b/cluster/node/node.go index ae65b9b..c8e33e6 100644 --- a/cluster/node/node.go +++ b/cluster/node/node.go @@ -224,15 +224,9 @@ func (n *Node) registerServiceInstance() { events = append(events, event) } - var name string - if n.opts.namespace != "" { - name = fmt.Sprintf("%s%s.%s", n.opts.namespace, string(cluster.Node), n.opts.name) - } else { - name = string(cluster.Node) - } n.instance = ®istry.ServiceInstance{ ID: n.opts.id, - Name: name, + Name: fmt.Sprintf("%s%s", n.opts.namespace, string(cluster.Node)), Kind: cluster.Node, Alias: n.opts.name, State: n.getState(), diff --git a/common/link/link.go b/common/link/link.go index d25b823..ae4b030 100644 --- a/common/link/link.go +++ b/common/link/link.go @@ -2,6 +2,7 @@ package link import ( "context" + "fmt" "github.com/symsimmy/due/cluster" "github.com/symsimmy/due/common/dispatcher" "github.com/symsimmy/due/common/endpoint" @@ -252,7 +253,7 @@ func (l *Link) FetchServiceAliasIDs(ctx context.Context, kind cluster.Kind, alia } func (l *Link) FetchServiceAliasList(ctx context.Context, kind cluster.Kind, alias string, states ...cluster.State) ([]*registry.ServiceInstance, error) { - services, err := l.opts.Registry.Services(ctx, l.opts.Namespace, kind, alias) + services, err := l.opts.Registry.Services(ctx, fmt.Sprintf("%s%s", l.opts.Namespace, string(kind))) if err != nil { return nil, err } @@ -1022,19 +1023,14 @@ func (l *Link) getNodeClientByNID(nid string) (transport.NodeClient, error) { // WatchServiceInstance 监听服务实例 func (l *Link) WatchServiceInstance(ctx context.Context, kinds ...cluster.Kind) { for _, kind := range kinds { - if kind == cluster.Node { - l.watchServiceInstance(ctx, kind, "center") - l.watchServiceInstance(ctx, kind, "game") - } else { - l.watchServiceInstance(ctx, kind, "") - } + l.watchServiceInstance(ctx, kind) } } // 监听服务实例 -func (l *Link) watchServiceInstance(ctx context.Context, kind cluster.Kind, alias string) { +func (l *Link) watchServiceInstance(ctx context.Context, kind cluster.Kind) { rctx, rcancel := context.WithTimeout(ctx, 10*time.Second) - watcher, err := l.opts.Registry.Watch(rctx, l.opts.Namespace, kind, alias) + watcher, err := l.opts.Registry.Watch(rctx, fmt.Sprintf("%s%s", l.opts.Namespace, string(kind))) rcancel() if err != nil { log.Fatalf("the dispatcher instance watch failed: %v", err) diff --git a/registry/consul/registry.go b/registry/consul/registry.go index d8d770f..a2c090f 100644 --- a/registry/consul/registry.go +++ b/registry/consul/registry.go @@ -87,8 +87,7 @@ func (r *Registry) Deregister(ctx context.Context, ins *registry.ServiceInstance } // Services 获取服务实例列表 -func (r *Registry) Services(ctx context.Context, namespace string, kind cluster.Kind, alias string) ([]*registry.ServiceInstance, error) { - serviceName := r.getServicesName(namespace, kind, alias) +func (r *Registry) Services(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) { if r.err != nil { return nil, r.err } @@ -103,8 +102,7 @@ func (r *Registry) Services(ctx context.Context, namespace string, kind cluster. } // Watch 监听服务 -func (r *Registry) Watch(ctx context.Context, namespace string, kind cluster.Kind, alias string) (registry.Watcher, error) { - serviceName := r.getServicesName(namespace, kind, alias) +func (r *Registry) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) { if r.err != nil { return nil, r.err } @@ -123,14 +121,6 @@ func (r *Registry) Watch(ctx context.Context, namespace string, kind cluster.Kin return w.fork(), nil } -func (r *Registry) getServicesName(namespace string, kind cluster.Kind, alias string) string { - if namespace == "" || alias == "" { - return fmt.Sprintf("%s%s", namespace, string(kind)) - } else { - return fmt.Sprintf("%s%s.%s", namespace, string(kind), alias) - } -} - // 获取服务实体列表 func (r *Registry) services(ctx context.Context, serviceName string, waitIndex uint64, passingOnly bool) ([]*registry.ServiceInstance, uint64, error) { opts := &api.QueryOptions{ diff --git a/registry/registry.go b/registry/registry.go index dbf15c8..cb967d6 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -13,16 +13,16 @@ type Registry interface { // Deregister 解注册服务实例 Deregister(ctx context.Context, ins *ServiceInstance) error // Watch 监听相同服务名的服务实例变化 - Watch(ctx context.Context, namespace string, kind cluster.Kind, alias string) (Watcher, error) + Watch(ctx context.Context, serviceName string) (Watcher, error) // Services 获取服务实例列表 - Services(ctx context.Context, namespace string, kind cluster.Kind, alias string) ([]*ServiceInstance, error) + Services(ctx context.Context, serviceName string) ([]*ServiceInstance, error) } type Discovery interface { // Watch 监听相同服务名的服务实例变化 - Watch(ctx context.Context, namespace string, kind cluster.Kind, alias string) (Watcher, error) + Watch(ctx context.Context, serviceName string) (Watcher, error) // Services 获取服务实例列表 - Services(ctx context.Context, namespace string, kind cluster.Kind, alias string) ([]*ServiceInstance, error) + Services(ctx context.Context, serviceName string) ([]*ServiceInstance, error) } type Watcher interface {