registry/nacos: reduce twice string copies (#1681)

* registry/nacos: reduce two string copies

* registry/nacos: update example
pull/1682/head
Cluas 3 years ago committed by GitHub
parent 041ffffb15
commit 5df28f4fdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 54
      contrib/registry/nacos/README.md
  2. 19
      contrib/registry/nacos/registry.go

@ -1,27 +1,29 @@
# Nacos Registry # Nacos Registry
## example
### server
```go ```go
package main
import ( import (
"github.com/go-kratos/kratos/v2" "log"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/nacos-group/nacos-sdk-go/clients" "github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/common/constant" "github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo" "github.com/nacos-group/nacos-sdk-go/vo"
"github.com/go-kratos/kratos/contrib/registry/nacos/v2"
"github.com/go-kratos/kratos/v2"
) )
func main() {
sc := []constant.ServerConfig{ sc := []constant.ServerConfig{
*constant.NewServerConfig("127.0.0.1", 8848), *constant.NewServerConfig("127.0.0.1", 8848),
} }
cc := constant.ClientConfig{
NamespaceId: "public",
TimeoutMs: 5000,
}
client, err := clients.NewNamingClient( client, err := clients.NewNamingClient(
vo.NacosClientParam{ vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc, ServerConfigs: sc,
}, },
) )
@ -40,6 +42,42 @@ app := kratos.New(
if err := app.Run(); err != nil { if err := app.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}
```
### client
```go
package main
import (
"context"
"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/transport/grpc"
)
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 // client
conn, err := grpc.DialInsecure( conn, err := grpc.DialInsecure(
@ -47,4 +85,6 @@ conn, err := grpc.DialInsecure(
grpc.WithEndpoint("discovery:///helloworld"), grpc.WithEndpoint("discovery:///helloworld"),
grpc.WithDiscovery(r), grpc.WithDiscovery(r),
) )
defer conn.Close()
}
``` ```

@ -79,7 +79,7 @@ func New(cli naming_client.INamingClient, opts ...Option) (r *Registry) {
} }
// Register the registration. // 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 == "" { if si.Name == "" {
return fmt.Errorf("kratos/nacos: serviceInstance.name cannot is empty") return fmt.Errorf("kratos/nacos: serviceInstance.name cannot is empty")
} }
@ -96,15 +96,20 @@ func (r *Registry) Register(ctx context.Context, si *registry.ServiceInstance) e
if err != nil { if err != nil {
return err return err
} }
var rmd map[string]string
if si.Metadata == nil { if si.Metadata == nil {
si.Metadata = make(map[string]string, 2) rmd = map[string]string{
"kind": u.Scheme,
"version": si.Version,
} }
si.Metadata["kind"] = u.Scheme } else {
si.Metadata["version"] = si.Version rmd = make(map[string]string, len(si.Metadata)+2)
rmd := make(map[string]string, len(si.Metadata))
for k, v := range si.Metadata { for k, v := range si.Metadata {
rmd[k] = v rmd[k] = v
} }
rmd["kind"] = u.Scheme
rmd["version"] = si.Version
}
_, e := r.cli.RegisterInstance(vo.RegisterInstanceParam{ _, e := r.cli.RegisterInstance(vo.RegisterInstanceParam{
Ip: host, Ip: host,
Port: uint64(p), Port: uint64(p),
@ -125,7 +130,7 @@ func (r *Registry) Register(ctx context.Context, si *registry.ServiceInstance) e
} }
// Deregister the registration. // 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 { for _, endpoint := range service.Endpoints {
u, err := url.Parse(endpoint) u, err := url.Parse(endpoint)
if err != nil { 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. // 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{ res, err := r.cli.SelectInstances(vo.SelectInstancesParam{
ServiceName: serviceName, ServiceName: serviceName,
HealthyOnly: true, HealthyOnly: true,

Loading…
Cancel
Save