1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-16 02:47:03 +02:00
kratos/contrib/registry/nacos
2022-02-23 17:13:57 +08:00
..
go.mod deps: upgrade kratos version to 2.2.0 (#1834) 2022-02-23 17:13:57 +08:00
go.sum deps:upgrade go mod version (#1800) 2022-01-28 14:56:32 +08:00
README.md registry/nacos: reduce twice string copies (#1681) 2021-12-10 00:14:35 +08:00
registry_test.go fix: nacos registry test data race (#1613) 2021-11-06 15:15:56 +08:00
registry.go fix(contrib): get nacos service of customize group name (#1798) 2022-02-22 10:51:47 +08:00
watcher.go fix(nacos): call unsubscribe when watching is stopped (#1697) 2021-12-17 11:08:16 +08:00

Nacos Registry

example

server

package main

import (
	"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),
	}
	

	client, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ServerConfigs: sc,
		},
	)

	if err != nil {
		log.Panic(err)
	}

	r := nacos.New(client)

	// server
	app := kratos.New(
		kratos.Name("helloworld"),
		kratos.Registrar(r),
	)
	if err := app.Run(); err != nil {
		log.Fatal(err)
	}
}

client

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(
		context.Background(),
		grpc.WithEndpoint("discovery:///helloworld"),
		grpc.WithDiscovery(r),
	)
	defer conn.Close()
}