You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
1.8 KiB
99 lines
1.8 KiB
package etcd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
"github.com/bilibili/kratos/pkg/naming"
|
|
"go.etcd.io/etcd/clientv3"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
config := &clientv3.Config{
|
|
Endpoints: []string{"127.0.0.1:2379"},
|
|
DialTimeout: time.Second * 3,
|
|
DialOptions: []grpc.DialOption{grpc.WithBlock()},
|
|
}
|
|
builder, err := New(config)
|
|
|
|
if err != nil {
|
|
fmt.Println("etcd 连接失败")
|
|
return
|
|
}
|
|
app1 := builder.Build("app1")
|
|
|
|
go func() {
|
|
fmt.Printf("Watch \n")
|
|
for {
|
|
select {
|
|
case <-app1.Watch():
|
|
fmt.Printf("app1 节点发生变化 \n")
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
time.Sleep(time.Second)
|
|
|
|
app1Cancel, err := builder.Register(context.Background(), &naming.Instance{
|
|
AppID: "app1",
|
|
Hostname: "h1",
|
|
Zone: "z1",
|
|
})
|
|
|
|
app2Cancel, err := builder.Register(context.Background(), &naming.Instance{
|
|
AppID: "app2",
|
|
Hostname: "h5",
|
|
Zone: "z3",
|
|
})
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
app2 := builder.Build("app2")
|
|
|
|
go func() {
|
|
fmt.Println("节点列表")
|
|
for {
|
|
fmt.Printf("app1: ")
|
|
r1, _ := app1.Fetch(context.Background())
|
|
if r1 != nil {
|
|
for z, ins := range r1.Instances {
|
|
fmt.Printf("zone: %s :", z)
|
|
for _, in := range ins {
|
|
fmt.Printf("app: %s host %s \n", in.AppID, in.Hostname)
|
|
}
|
|
}
|
|
} else {
|
|
fmt.Printf("\n")
|
|
}
|
|
fmt.Printf("app2: ")
|
|
r2, _ := app2.Fetch(context.Background())
|
|
if r2 != nil {
|
|
for z, ins := range r2.Instances {
|
|
fmt.Printf("zone: %s :", z)
|
|
for _, in := range ins {
|
|
fmt.Printf("app: %s host %s \n", in.AppID, in.Hostname)
|
|
}
|
|
}
|
|
} else {
|
|
fmt.Printf("\n")
|
|
}
|
|
time.Sleep(time.Second)
|
|
}
|
|
}()
|
|
|
|
time.Sleep(time.Second * 5)
|
|
fmt.Println("取消app1")
|
|
app1Cancel()
|
|
|
|
time.Sleep(time.Second * 10)
|
|
fmt.Println("取消app2")
|
|
app2Cancel()
|
|
|
|
time.Sleep(30 * time.Second)
|
|
}
|
|
|