diff --git a/common.go b/common.go index 1f30f1e..d4d761d 100644 --- a/common.go +++ b/common.go @@ -16,7 +16,7 @@ package etcd import "fmt" -const ( +var ( etcdPrefixTpl = "kitex/registry-etcd/%v/" ) diff --git a/etcd_resolver_test.go b/etcd_resolver_test.go index b58468a..e702b9f 100644 --- a/etcd_resolver_test.go +++ b/etcd_resolver_test.go @@ -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) +} diff --git a/example/client/main.go b/example/client/main.go index aa2b7ef..1707340 100644 --- a/example/client/main.go +++ b/example/client/main.go @@ -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) } diff --git a/option.go b/option.go index e004a34..2dd37be 100644 --- a/option.go +++ b/option.go @@ -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) {