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. 108
      contrib/registry/nacos/README.md
  2. 25
      contrib/registry/nacos/registry.go

@ -1,50 +1,90 @@
# 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/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"github.com/nacos-group/nacos-sdk-go/clients" "github.com/go-kratos/kratos/contrib/registry/nacos/v2"
"github.com/nacos-group/nacos-sdk-go/common/constant" "github.com/go-kratos/kratos/v2"
"github.com/nacos-group/nacos-sdk-go/vo"
) )
sc := []constant.ServerConfig{ func main() {
*constant.NewServerConfig("127.0.0.1", 8848), sc := []constant.ServerConfig{
} *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, },
}, )
)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
r := nacos.New(client) r := nacos.New(client)
// server // server
app := kratos.New( app := kratos.New(
kratos.Name("helloworld"), kratos.Name("helloworld"),
kratos.Registrar(r), kratos.Registrar(r),
) )
if err := app.Run(); err != nil { if err := app.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
}
} }
```
### client
```go
package main
// client import (
conn, err := grpc.DialInsecure( "context"
context.Background(), "log"
grpc.WithEndpoint("discovery:///helloworld"),
grpc.WithDiscovery(r), "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
conn, err := grpc.DialInsecure(
context.Background(),
grpc.WithEndpoint("discovery:///helloworld"),
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,14 +96,19 @@ 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,
si.Metadata["kind"] = u.Scheme "version": si.Version,
si.Metadata["version"] = si.Version }
rmd := make(map[string]string, len(si.Metadata)) } else {
for k, v := range si.Metadata { rmd = make(map[string]string, len(si.Metadata)+2)
rmd[k] = v for k, v := range si.Metadata {
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,
@ -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