test: add nacos test (#1603)
* add test * upgrade nacos sdk version * upgrade nacos sdk version * fix test * github nacos instance * config test * fix lint * fix test * go mod * fix lint * lintpull/1613/head
parent
5aac2ef5a7
commit
4f013de2ec
@ -0,0 +1,36 @@ |
||||
# Consul Config |
||||
|
||||
```go |
||||
import ( |
||||
kconfig "github.com/go-kratos/kratos/v2/config" |
||||
"github.com/nacos-group/nacos-sdk-go/clients" |
||||
"github.com/nacos-group/nacos-sdk-go/common/constant" |
||||
) |
||||
|
||||
|
||||
sc := []constant.ServerConfig{ |
||||
*constant.NewServerConfig("127.0.0.1", 8848), |
||||
} |
||||
|
||||
cc := &constant.ClientConfig{ |
||||
NamespaceId: "public", //namespace id |
||||
TimeoutMs: 5000, |
||||
NotLoadCacheAtStart: true, |
||||
LogDir: "/tmp/nacos/log", |
||||
CacheDir: "/tmp/nacos/cache", |
||||
RotateTime: "1h", |
||||
MaxAge: 3, |
||||
LogLevel: "debug", |
||||
} |
||||
|
||||
// a more graceful way to create naming client |
||||
client, err := clients.NewNamingClient( |
||||
vo.NacosClientParam{ |
||||
ClientConfig: cc, |
||||
ServerConfigs: sc, |
||||
}, |
||||
) |
||||
if err != nil { |
||||
log.Panic(err) |
||||
} |
||||
``` |
@ -0,0 +1,36 @@ |
||||
# Nacos Config |
||||
|
||||
```go |
||||
import ( |
||||
kconfig "github.com/go-kratos/kratos/v2/config" |
||||
"github.com/nacos-group/nacos-sdk-go/clients" |
||||
"github.com/nacos-group/nacos-sdk-go/common/constant" |
||||
) |
||||
|
||||
|
||||
sc := []constant.ServerConfig{ |
||||
*constant.NewServerConfig("127.0.0.1", 8848), |
||||
} |
||||
|
||||
cc := &constant.ClientConfig{ |
||||
NamespaceId: "public", //namespace id |
||||
TimeoutMs: 5000, |
||||
NotLoadCacheAtStart: true, |
||||
LogDir: "/tmp/nacos/log", |
||||
CacheDir: "/tmp/nacos/cache", |
||||
RotateTime: "1h", |
||||
MaxAge: 3, |
||||
LogLevel: "debug", |
||||
} |
||||
|
||||
// a more graceful way to create naming client |
||||
client, err := clients.NewNamingClient( |
||||
vo.NacosClientParam{ |
||||
ClientConfig: cc, |
||||
ServerConfigs: sc, |
||||
}, |
||||
) |
||||
if err != nil { |
||||
log.Panic(err) |
||||
} |
||||
``` |
@ -0,0 +1,108 @@ |
||||
package config |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net" |
||||
"testing" |
||||
"time" |
||||
|
||||
kconfig "github.com/go-kratos/kratos/v2/config" |
||||
"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" |
||||
"gopkg.in/yaml.v3" |
||||
) |
||||
|
||||
func getIntranetIP() string { |
||||
addrs, err := net.InterfaceAddrs() |
||||
if err != nil { |
||||
return "127.0.0.1" |
||||
} |
||||
|
||||
for _, address := range addrs { |
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { |
||||
if ipnet.IP.To4() != nil { |
||||
return ipnet.IP.String() |
||||
} |
||||
} |
||||
} |
||||
return "127.0.0.1" |
||||
} |
||||
|
||||
func TestGetConfig(t *testing.T) { |
||||
ip := getIntranetIP() |
||||
// ctx := context.Background()
|
||||
|
||||
sc := []constant.ServerConfig{ |
||||
*constant.NewServerConfig(ip, 8848), |
||||
} |
||||
|
||||
cc := constant.ClientConfig{ |
||||
TimeoutMs: 5000, |
||||
NotLoadCacheAtStart: true, |
||||
LogDir: "/tmp/nacos/log", |
||||
CacheDir: "/tmp/nacos/cache", |
||||
RotateTime: "1h", |
||||
MaxAge: 3, |
||||
LogLevel: "debug", |
||||
} |
||||
|
||||
// a more graceful way to create naming client
|
||||
client, err := clients.NewConfigClient( |
||||
vo.NacosClientParam{ |
||||
ClientConfig: &cc, |
||||
ServerConfigs: sc, |
||||
}, |
||||
) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
dataID := "test.yaml" |
||||
group := "test" |
||||
_, err = client.PublishConfig(vo.ConfigParam{DataId: dataID, Group: group, Content: ` |
||||
logger: |
||||
level: info |
||||
`}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
time.Sleep(1 * time.Second) |
||||
c := kconfig.New( |
||||
kconfig.WithSource( |
||||
NewConfigSource(client, WithGroup(group), WithDataID(dataID)), |
||||
), |
||||
kconfig.WithDecoder(func(kv *kconfig.KeyValue, v map[string]interface{}) error { |
||||
return yaml.Unmarshal(kv.Value, v) |
||||
}), |
||||
) |
||||
|
||||
if err = c.Load(); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
name, err := c.Value("logger.level").String() |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
fmt.Println("get value", name) |
||||
|
||||
done := make(chan bool) |
||||
err = c.Watch("logger.level", func(key string, value kconfig.Value) { |
||||
fmt.Println(key, " value change", value) |
||||
done <- true |
||||
}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
_, err = client.PublishConfig(vo.ConfigParam{DataId: dataID, Group: group, Content: ` |
||||
logger: |
||||
level: debug |
||||
`}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
<-done |
||||
} |
@ -1,2 +1,4 @@ |
||||
# Discovery Registry |
||||
|
||||
## [discovery](https://github.com/bilibili/discovery) |
||||
|
||||
|
@ -0,0 +1,50 @@ |
||||
# Nacos Registry |
||||
|
||||
```go |
||||
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" |
||||
) |
||||
|
||||
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, |
||||
}, |
||||
) |
||||
|
||||
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 |
||||
conn, err := grpc.DialInsecure( |
||||
context.Background(), |
||||
grpc.WithEndpoint("discovery:///helloworld"), |
||||
grpc.WithDiscovery(r), |
||||
) |
||||
``` |
@ -0,0 +1,209 @@ |
||||
package nacos |
||||
|
||||
import ( |
||||
"context" |
||||
"log" |
||||
"net" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/go-kratos/kratos/v2/registry" |
||||
"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" |
||||
) |
||||
|
||||
func getIntranetIP() string { |
||||
addrs, err := net.InterfaceAddrs() |
||||
if err != nil { |
||||
return "127.0.0.1" |
||||
} |
||||
|
||||
for _, address := range addrs { |
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { |
||||
if ipnet.IP.To4() != nil { |
||||
return ipnet.IP.String() |
||||
} |
||||
} |
||||
} |
||||
return "127.0.0.1" |
||||
} |
||||
|
||||
func TestRegistry(t *testing.T) { |
||||
ip := getIntranetIP() |
||||
serviceName := "golang-sms@grpc" |
||||
ctx := context.Background() |
||||
|
||||
sc := []constant.ServerConfig{ |
||||
*constant.NewServerConfig(ip, 8848), |
||||
} |
||||
|
||||
cc := constant.ClientConfig{ |
||||
NamespaceId: "public", // namespace id
|
||||
TimeoutMs: 5000, |
||||
NotLoadCacheAtStart: true, |
||||
LogDir: "/tmp/nacos/log", |
||||
CacheDir: "/tmp/nacos/cache", |
||||
RotateTime: "1h", |
||||
MaxAge: 3, |
||||
LogLevel: "debug", |
||||
} |
||||
|
||||
// a more graceful way to create naming client
|
||||
client, err := clients.NewNamingClient( |
||||
vo.NacosClientParam{ |
||||
ClientConfig: &cc, |
||||
ServerConfigs: sc, |
||||
}, |
||||
) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
_, err = client.RegisterInstance(vo.RegisterInstanceParam{ |
||||
Ip: "f", |
||||
Port: 8840, |
||||
ServiceName: serviceName, |
||||
Weight: 10, |
||||
Enable: true, |
||||
Healthy: true, |
||||
Ephemeral: true, |
||||
Metadata: map[string]string{"idc": "shanghai-xs"}, |
||||
}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
time.Sleep(time.Second) |
||||
|
||||
is, err := client.GetService(vo.GetServiceParam{ |
||||
ServiceName: serviceName, |
||||
}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
t.Logf("is %#v", is) |
||||
|
||||
time.Sleep(time.Second) |
||||
r := New(client) |
||||
|
||||
go func() { |
||||
var w registry.Watcher |
||||
w, err = r.Watch(ctx, "golang-sms@grpc") |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
for { |
||||
var res []*registry.ServiceInstance |
||||
res, err = w.Next() |
||||
if err != nil { |
||||
return |
||||
} |
||||
log.Printf("watch: %d", len(res)) |
||||
for _, r := range res { |
||||
log.Printf("next: %+v", r) |
||||
} |
||||
} |
||||
}() |
||||
|
||||
time.Sleep(time.Second) |
||||
|
||||
ins, err := r.GetService(ctx, serviceName) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
for _, in := range ins { |
||||
t.Logf("ins: %#v", in) |
||||
} |
||||
|
||||
time.Sleep(time.Second) |
||||
} |
||||
|
||||
func TestRegistryMany(t *testing.T) { |
||||
ip := getIntranetIP() |
||||
serviceName := "golang-sms@grpc" |
||||
// ctx := context.Background()
|
||||
|
||||
sc := []constant.ServerConfig{ |
||||
*constant.NewServerConfig(ip, 8848), |
||||
} |
||||
|
||||
cc := constant.ClientConfig{ |
||||
NamespaceId: "public", // namespace id
|
||||
TimeoutMs: 5000, |
||||
NotLoadCacheAtStart: true, |
||||
LogDir: "/tmp/nacos/log", |
||||
CacheDir: "/tmp/nacos/cache", |
||||
RotateTime: "1h", |
||||
MaxAge: 3, |
||||
LogLevel: "debug", |
||||
} |
||||
|
||||
// a more graceful way to create naming client
|
||||
client, err := clients.NewNamingClient( |
||||
vo.NacosClientParam{ |
||||
ClientConfig: &cc, |
||||
ServerConfigs: sc, |
||||
}, |
||||
) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
_, err = client.RegisterInstance(vo.RegisterInstanceParam{ |
||||
Ip: "f1", |
||||
Port: 8840, |
||||
ServiceName: serviceName, |
||||
Weight: 10, |
||||
Enable: true, |
||||
Healthy: true, |
||||
Ephemeral: true, |
||||
Metadata: map[string]string{"idc": "shanghai-xs"}, |
||||
}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
_, err = client.RegisterInstance(vo.RegisterInstanceParam{ |
||||
Ip: "f2", |
||||
Port: 8840, |
||||
ServiceName: serviceName, |
||||
Weight: 10, |
||||
Enable: true, |
||||
Healthy: true, |
||||
Ephemeral: true, |
||||
Metadata: map[string]string{"idc": "shanghai-xs"}, |
||||
}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
_, err = client.RegisterInstance(vo.RegisterInstanceParam{ |
||||
Ip: "f3", |
||||
Port: 8840, |
||||
ServiceName: serviceName, |
||||
Weight: 10, |
||||
Enable: true, |
||||
Healthy: true, |
||||
Ephemeral: true, |
||||
Metadata: map[string]string{"idc": "shanghai-xs"}, |
||||
}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
time.Sleep(time.Second) |
||||
|
||||
is, err := client.GetService(vo.GetServiceParam{ |
||||
ServiceName: serviceName, |
||||
}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
for _, host := range is.Hosts { |
||||
t.Logf("host: %#v,e: %v", host, err) |
||||
} |
||||
|
||||
time.Sleep(time.Second) |
||||
} |
Loading…
Reference in new issue