diff --git a/contrib/registry/nacos/README.md b/contrib/registry/nacos/README.md index 05c30547f..1a3e09cb8 100644 --- a/contrib/registry/nacos/README.md +++ b/contrib/registry/nacos/README.md @@ -1,50 +1,90 @@ # Nacos Registry +## example +### server ```go +package main + import ( - "github.com/go-kratos/kratos/v2" - "github.com/go-kratos/kratos/v2/transport/grpc" - - "github.com/nacos-group/nacos-sdk-go/clients" - "github.com/nacos-group/nacos-sdk-go/common/constant" - "github.com/nacos-group/nacos-sdk-go/vo" + "log" + + "github.com/nacos-group/nacos-sdk-go/clients" + "github.com/nacos-group/nacos-sdk-go/common/constant" + "github.com/nacos-group/nacos-sdk-go/vo" + + "github.com/go-kratos/kratos/contrib/registry/nacos/v2" + "github.com/go-kratos/kratos/v2" ) -sc := []constant.ServerConfig{ - *constant.NewServerConfig("127.0.0.1", 8848), -} +func main() { + sc := []constant.ServerConfig{ + *constant.NewServerConfig("127.0.0.1", 8848), + } + -cc := constant.ClientConfig{ - NamespaceId: "public", - TimeoutMs: 5000, -} + client, err := clients.NewNamingClient( + vo.NacosClientParam{ + ServerConfigs: sc, + }, + ) -client, err := clients.NewNamingClient( - vo.NacosClientParam{ - ClientConfig: &cc, - ServerConfigs: sc, - }, -) + if err != nil { + log.Panic(err) + } + + r := nacos.New(client) -if err != nil { - log.Panic(err) + // server + app := kratos.New( + kratos.Name("helloworld"), + kratos.Registrar(r), + ) + if err := app.Run(); err != nil { + log.Fatal(err) + } } +``` +### client +```go +package main + +import ( + "context" + "log" -r := nacos.New(client) + "github.com/nacos-group/nacos-sdk-go/clients" + "github.com/nacos-group/nacos-sdk-go/common/constant" + "github.com/nacos-group/nacos-sdk-go/vo" -// server -app := kratos.New( - kratos.Name("helloworld"), - kratos.Registrar(r), + "github.com/go-kratos/kratos/contrib/registry/nacos/v2" + "github.com/go-kratos/kratos/v2/transport/grpc" ) -if err := app.Run(); err != nil { - log.Fatal(err) -} -// client -conn, err := grpc.DialInsecure( - context.Background(), - grpc.WithEndpoint("discovery:///helloworld"), - grpc.WithDiscovery(r), -) +func main() { + + cc := constant.ClientConfig{ + NamespaceId: "public", + TimeoutMs: 5000, + } + + client, err := clients.NewNamingClient( + vo.NacosClientParam{ + ClientConfig: &cc, + }, + ) + + if err != nil { + log.Panic(err) + } + + r := nacos.New(client) + + // client + conn, err := grpc.DialInsecure( + context.Background(), + grpc.WithEndpoint("discovery:///helloworld"), + grpc.WithDiscovery(r), + ) + defer conn.Close() +} ``` \ No newline at end of file diff --git a/contrib/registry/nacos/registry.go b/contrib/registry/nacos/registry.go index e7854a09f..5a5d9744a 100644 --- a/contrib/registry/nacos/registry.go +++ b/contrib/registry/nacos/registry.go @@ -79,7 +79,7 @@ func New(cli naming_client.INamingClient, opts ...Option) (r *Registry) { } // Register the registration. -func (r *Registry) Register(ctx context.Context, si *registry.ServiceInstance) error { +func (r *Registry) Register(_ context.Context, si *registry.ServiceInstance) error { if si.Name == "" { return fmt.Errorf("kratos/nacos: serviceInstance.name cannot is empty") } @@ -96,14 +96,19 @@ func (r *Registry) Register(ctx context.Context, si *registry.ServiceInstance) e if err != nil { return err } + var rmd map[string]string if si.Metadata == nil { - si.Metadata = make(map[string]string, 2) - } - si.Metadata["kind"] = u.Scheme - si.Metadata["version"] = si.Version - rmd := make(map[string]string, len(si.Metadata)) - for k, v := range si.Metadata { - rmd[k] = v + rmd = map[string]string{ + "kind": u.Scheme, + "version": si.Version, + } + } else { + rmd = make(map[string]string, len(si.Metadata)+2) + for k, v := range si.Metadata { + rmd[k] = v + } + rmd["kind"] = u.Scheme + rmd["version"] = si.Version } _, e := r.cli.RegisterInstance(vo.RegisterInstanceParam{ Ip: host, @@ -125,7 +130,7 @@ func (r *Registry) Register(ctx context.Context, si *registry.ServiceInstance) e } // Deregister the registration. -func (r *Registry) Deregister(ctx context.Context, service *registry.ServiceInstance) error { +func (r *Registry) Deregister(_ context.Context, service *registry.ServiceInstance) error { for _, endpoint := range service.Endpoints { u, err := url.Parse(endpoint) if err != nil { @@ -159,7 +164,7 @@ func (r *Registry) Watch(ctx context.Context, serviceName string) (registry.Watc } // GetService return the service instances in memory according to the service name. -func (r *Registry) GetService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) { +func (r *Registry) GetService(_ context.Context, serviceName string) ([]*registry.ServiceInstance, error) { res, err := r.cli.SelectInstances(vo.SelectInstancesParam{ ServiceName: serviceName, HealthyOnly: true,