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
## example
### server
```go
package main
import (
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/transport/grpc"
"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"
)
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{
ClientConfig: &cc,
ServerConfigs: sc,
},
)
@ -40,6 +42,42 @@ app := kratos.New(
if err := app.Run(); err != nil {
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
conn, err := grpc.DialInsecure(
@ -47,4 +85,6 @@ conn, err := grpc.DialInsecure(
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.
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,15 +96,20 @@ 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)
rmd = map[string]string{
"kind": u.Scheme,
"version": si.Version,
}
si.Metadata["kind"] = u.Scheme
si.Metadata["version"] = si.Version
rmd := make(map[string]string, len(si.Metadata))
} 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,
Port: uint64(p),
@ -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,

Loading…
Cancel
Save