registry/nacos: reduce twice string copies (#1681)
* registry/nacos: reduce two string copies * registry/nacos: update examplepull/1682/head
parent
041ffffb15
commit
5df28f4fdc
@ -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() |
||||
} |
||||
``` |
Loading…
Reference in new issue