Skip to content

Commit

Permalink
Support modification of prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
logicwu0 committed May 8, 2024
1 parent 8f24094 commit bb52900
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
2 changes: 1 addition & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package etcd

import "fmt"

const (
var (
etcdPrefixTpl = "kitex/registry-etcd/%v/"
)

Expand Down
121 changes: 121 additions & 0 deletions etcd_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,124 @@ func teardownEmbedEtcd(s *embed.Etcd) {
s.Close()
_ = os.RemoveAll(s.Config().Dir)
}

func TestEtcdResolverWithEtcdPrefix(t *testing.T) {
s, endpoint := setupEmbedEtcd(t)

rg, err := NewEtcdRegistry([]string{endpoint})
require.Nil(t, err)
tpl := "etcd/v1"
rs, err := NewEtcdResolver([]string{endpoint}, WithEtcdPrefixNewTpl(tpl))
require.Nil(t, err)

infoList := []registry.Info{
{
ServiceName: "registry-etcd-test-suffix",
Addr: utils.NewNetAddr("tcp", "127.0.0.1:8888"),
Weight: 66,
Tags: map[string]string{"hello": "world"},
},
{
ServiceName: "registry-etcd-test",
Addr: utils.NewNetAddr("tcp", "127.0.0.1:8889"),
Weight: 66,
Tags: map[string]string{"hello": "world"},
},
}

// test register service
{
for _, info := range infoList {
err = rg.Register(&info)
require.Nil(t, err)

desc := rs.Target(context.TODO(), rpcinfo.NewEndpointInfo(info.ServiceName, "", nil, nil))
result, err := rs.Resolve(context.TODO(), desc)
require.Nil(t, err)
expected := discovery.Result{
Cacheable: true,
CacheKey: info.ServiceName,
Instances: []discovery.Instance{
discovery.NewInstance(info.Addr.Network(), info.Addr.String(), info.Weight, info.Tags),
},
}
require.Equal(t, expected, result)
prefix := serviceKeyPrefix(info.ServiceName)
println(prefix)
require.Equal(t, fmt.Sprintf(tpl+"/%v/", info.ServiceName), prefix)
}
}

// test deregister service
{
for _, info := range infoList {
err = rg.Deregister(&info)
require.Nil(t, err)
desc := rs.Target(context.TODO(), rpcinfo.NewEndpointInfo(info.ServiceName, "", nil, nil))
_, err = rs.Resolve(context.TODO(), desc)
require.NotNil(t, err)
}
}

teardownEmbedEtcd(s)
}

func TestEtcdResolverWithEtcdPrefix2(t *testing.T) {
s, endpoint := setupEmbedEtcd(t)

rg, err := NewEtcdRegistry([]string{endpoint})
require.Nil(t, err)
rs, err := NewEtcdResolver([]string{endpoint})
require.Nil(t, err)

infoList := []registry.Info{
{
ServiceName: "registry-etcd-test-suffix",
Addr: utils.NewNetAddr("tcp", "127.0.0.1:8888"),
Weight: 66,
Tags: map[string]string{"hello": "world"},
},
{
ServiceName: "registry-etcd-test",
Addr: utils.NewNetAddr("tcp", "127.0.0.1:8889"),
Weight: 66,
Tags: map[string]string{"hello": "world"},
},
}

// test register service
{
for _, info := range infoList {
err = rg.Register(&info)
require.Nil(t, err)

desc := rs.Target(context.TODO(), rpcinfo.NewEndpointInfo(info.ServiceName, "", nil, nil))
result, err := rs.Resolve(context.TODO(), desc)
require.Nil(t, err)
expected := discovery.Result{
Cacheable: true,
CacheKey: info.ServiceName,
Instances: []discovery.Instance{
discovery.NewInstance(info.Addr.Network(), info.Addr.String(), info.Weight, info.Tags),
},
}
require.Equal(t, expected, result)
prefix := serviceKeyPrefix(info.ServiceName)
println(prefix)
require.Equal(t, fmt.Sprintf("kitex/registry-etcd/%v/", info.ServiceName), prefix)
}
}

// test deregister service
{
for _, info := range infoList {
err = rg.Deregister(&info)
require.Nil(t, err)
desc := rs.Target(context.TODO(), rpcinfo.NewEndpointInfo(info.ServiceName, "", nil, nil))
_, err = rs.Resolve(context.TODO(), desc)
require.NotNil(t, err)
}
}

teardownEmbedEtcd(s)
}
2 changes: 1 addition & 1 deletion example/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func main() {
r, err := etcd.NewEtcdResolver([]string{"127.0.0.1:2379"})
r, err := etcd.NewEtcdResolver([]string{"127.0.0.1:2379"}, etcd.WithEtcdPrefixNewTpl("etcd/v2"))
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 7 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func WithTLSOpt(certFile, keyFile, caFile string) Option {
}
}

// WithEtcdPrefixNewTpl returns a option prefix
func WithEtcdPrefixNewTpl(etcdPrefixNewTpl string) Option {
return func(cfg *clientv3.Config) {
etcdPrefixTpl = etcdPrefixNewTpl + "/%v/"
}
}

// WithAuthOpt returns a option that authentication by usernane and password.
func WithAuthOpt(username, password string) Option {
return func(cfg *clientv3.Config) {
Expand Down

0 comments on commit bb52900

Please sign in to comment.