go get github.com/kitex-contrib/registry-nacos
go get github.com/kitex-contrib/registry-nacos/v2
Kitex provides two functions to create service registry.
NewDefaultNacosRegistry
uses nacos to create a new service registry, and read info from environment variable to create “Nacos Client”. Customizable service registry configuration, see Option
for configuration details.
Environment Variable Name | Environment Variable Default Value | Environment Variable Introduction |
---|---|---|
serverAddr | 127.0.0.1 | nacos server address |
serverPort | 8848 | nacos server port |
namespace | the namespaceId of nacos |
Function signature:
func NewDefaultNacosRegistry(opts ...Option) (registry.Registry, error
Example:
import (
// ...
"github.com/kitex-contrib/registry-nacos/registry"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"github.com/cloudwego/kitex/pkg/rpcinfo"
// ...
)
func main() {
// ...
r, err := registry.NewDefaultNacosRegistry()
if err != nil {
panic(err)
}
svr := echo.NewServer(
new(EchoImpl),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "echo"}),
server.WithRegistry(r),
)
if err := svr.Run(); err != nil {
log.Println("server stopped with error:", err)
} else {
log.Println("server stopped")
}
// ...
}
NewNacosRegistry
uses nacos to create a new service registry, requires passing in a self-configured client. Customizable service registry configuration, see Option
for configuration details.
Function signature:
func NewNacosRegistry(cli naming_client.INamingClient, opts ...Option) registry.Registry
Example:
import (
// ...
"github.com/kitex-contrib/registry-nacos/registry"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
// ...
)
func main() {
// ...
sc := []constant.ServerConfig{
*constant.NewServerConfig("127.0.0.1", 8848),
}
cc := constant.ClientConfig{
NamespaceId: "public",
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
LogLevel: "info",
Username: "your-name",
Password: "your-password",
}
cli, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc,
},
)
if err != nil {
panic(err)
}
svr := echo.NewServer(new(EchoImpl),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "echo"}),
server.WithRegistry(registry.NewNacosRegistry(cli)),
)
if err := svr.Run(); err != nil {
log.Println("server stopped with error:", err)
} else {
log.Println("server stopped")
}
// ...
}
Nacos extension provides option configuration in the service registry section.
Nacos extension provides WithCluster
to help users configure the cluster in nacos. The default value is “DEFAULT”.
Function signature:
func WithCluster(cluster string) Option
Nacos extension provides WithGroup
to help users configure the cluster in nacos. The default value is “DEFAULT_GROUP”.
Function signature:
func WithGroup(group string) Option
Kitex provides two functions to create Resolver.
NewDefaultNacosResolver
uses nacos to create a new service resolver, and read info from environment variable to create “Nacos Client”. Customizable service registry configuration, see Option
for configuration details.
Environment Variable Name | Environment Variable Default Value | Environment Variable Introduction |
---|---|---|
serverAddr | 127.0.0.1 | nacos server address |
serverPort | 8848 | nacos server port |
namespace | the namespaceId of nacos |
Function signature:
func NewDefaultNacosResolver(opts ...Option) (discovery.Resolver, error)
Example:
import (
// ...
"github.com/kitex-contrib/registry-nacos/resolver"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
// ...
)
func main() {
// ...
r, err := resolver.NewDefaultNacosResolver()
if err != nil {
panic(err)
}
client, err := echo.NewClient("echo", client.WithResolver(r))
if err != nil {
log.Fatal(err)
}
// ...
}
NewNacosResolver
uses nacos to create a new service resolver, requires passing in a self-configured client. Customizable service registry configuration, see Option
for configuration details.
Function signature:
func NewNacosResolver(cli naming_client.INamingClient, opts ...Option) discovery.Resolver
Example:
import (
// ...
"github.com/kitex-contrib/registry-nacos/resolver"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
// ...
)
func main() {
// ...
sc := []constant.ServerConfig{
*constant.NewServerConfig("127.0.0.1", 8848),
}
cc := constant.ClientConfig{
NamespaceId: "public",
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
LogLevel: "info",
Username: "your-name",
Password: "your-password",
}
cli, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc,
},
)
if err != nil {
panic(err)
}
client, err := echo.NewClient("echo", client.WithResolver(resolver.NewNacosResolver(cli))
if err != nil {
log.Fatal(err)
}
// ...
}
Nacos extension provides option configuration in the service discovery section.
Nacos extension provides WithCluster
to help users configure the cluster in nacos. The default value is “DEFAULT”.
Function signature:
func WithCluster(cluster string) Option
Nacos extension provides WithGroup
to help users configure the cluster in nacos. The default value is “DEFAULT_GROUP”.
Function signature:
func WithGroup(group string) Option
package main
import (
"context"
"log"
"net"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/server"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api/hello"
"github.com/kitex-contrib/registry-nacos/registry"
)
type HelloImpl struct{}
func (h *HelloImpl) Echo(_ context.Context, req *api.Request) (resp *api.Response, err error) {
resp = &api.Response{
Message: req.Message,
}
return
}
func main() {
r, err := registry.NewDefaultNacosRegistry()
if err != nil {
panic(err)
}
svr := hello.NewServer(
new(HelloImpl),
server.WithRegistry(r),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "Hello"}),
server.WithServiceAddr(&net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8080}),
)
if err := svr.Run(); err != nil {
log.Println("server stopped with error:", err)
} else {
log.Println("server stopped")
}
}
package main
import (
"context"
"log"
"time"
"github.com/cloudwego/kitex/client"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api/hello"
"github.com/kitex-contrib/registry-nacos/resolver"
)
func main() {
r, err := resolver.NewDefaultNacosResolver()
if err != nil {
panic(err)
}
newClient := hello.MustNewClient(
"Hello",
client.WithResolver(r),
client.WithRPCTimeout(time.Second*3),
)
for {
resp, err := newClient.Echo(context.Background(), &api.Request{Message: "Hello"})
if err != nil {
log.Fatal(err)
}
log.Println(resp)
time.Sleep(time.Second)
}
}
The configuration of Etcd client and server can be customized, refer to the configuration of nacos-sdk-go.
For more, see example.