|
|
|
@ -4,16 +4,15 @@ import ( |
|
|
|
|
"context" |
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
|
"github.com/go-kratos/kratos/v2/registry" |
|
|
|
|
"github.com/nacos-group/nacos-sdk-go/clients/naming_client" |
|
|
|
|
"github.com/nacos-group/nacos-sdk-go/model" |
|
|
|
|
"github.com/nacos-group/nacos-sdk-go/vo" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
_ registry.Watcher = (*watcher)(nil) |
|
|
|
|
"github.com/go-kratos/kratos/v2/registry" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var _ registry.Watcher = (*watcher)(nil) |
|
|
|
|
|
|
|
|
|
type watcher struct { |
|
|
|
|
serviceName string |
|
|
|
|
clusters []string |
|
|
|
@ -46,36 +45,34 @@ func newWatcher(ctx context.Context, cli naming_client.INamingClient, serviceNam |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (w *watcher) Next() ([]*registry.ServiceInstance, error) { |
|
|
|
|
for { |
|
|
|
|
select { |
|
|
|
|
case <-w.ctx.Done(): |
|
|
|
|
return nil, w.ctx.Err() |
|
|
|
|
case <-w.watchChan: |
|
|
|
|
} |
|
|
|
|
res, err := w.cli.GetService(vo.GetServiceParam{ |
|
|
|
|
ServiceName: w.serviceName, |
|
|
|
|
GroupName: w.groupName, |
|
|
|
|
Clusters: w.clusters, |
|
|
|
|
select { |
|
|
|
|
case <-w.ctx.Done(): |
|
|
|
|
return nil, w.ctx.Err() |
|
|
|
|
case <-w.watchChan: |
|
|
|
|
} |
|
|
|
|
res, err := w.cli.GetService(vo.GetServiceParam{ |
|
|
|
|
ServiceName: w.serviceName, |
|
|
|
|
GroupName: w.groupName, |
|
|
|
|
Clusters: w.clusters, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
items := make([]*registry.ServiceInstance, 0, len(res.Hosts)) |
|
|
|
|
for _, in := range res.Hosts { |
|
|
|
|
items = append(items, ®istry.ServiceInstance{ |
|
|
|
|
ID: in.InstanceId, |
|
|
|
|
Name: res.Name, |
|
|
|
|
Version: in.Metadata["version"], |
|
|
|
|
Metadata: in.Metadata, |
|
|
|
|
Endpoints: []string{fmt.Sprintf("%s://%s:%d", in.Metadata["kind"], in.Ip, in.Port)}, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
var items []*registry.ServiceInstance |
|
|
|
|
for _, in := range res.Hosts { |
|
|
|
|
items = append(items, ®istry.ServiceInstance{ |
|
|
|
|
ID: in.InstanceId, |
|
|
|
|
Name: res.Name, |
|
|
|
|
Version: in.Metadata["version"], |
|
|
|
|
Metadata: in.Metadata, |
|
|
|
|
Endpoints: []string{fmt.Sprintf("%s://%s:%d", in.Metadata["kind"], in.Ip, in.Port)}, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
return items, nil |
|
|
|
|
} |
|
|
|
|
return items, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (w *watcher) Stop() error { |
|
|
|
|
w.cancel() |
|
|
|
|
//close
|
|
|
|
|
// close
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|