From bb5290064d433932b1777fdc6d6c2d9718e136c2 Mon Sep 17 00:00:00 2001 From: "logic.wu0" <605524858@qq.com> Date: Thu, 9 May 2024 01:39:21 +0800 Subject: [PATCH 1/7] Support modification of prefix --- common.go | 2 +- etcd_resolver_test.go | 121 +++++++++++++++++++++++++++++++++++++++++ example/client/main.go | 2 +- option.go | 7 +++ 4 files changed, 130 insertions(+), 2 deletions(-) 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) { From f449109a623a0c1b98088ac3bfb2bb05e5fb2246 Mon Sep 17 00:00:00 2001 From: "logic.wu0" <605524858@qq.com> Date: Thu, 9 May 2024 11:13:27 +0800 Subject: [PATCH 2/7] Support modification of prefix --- etcd_resolver_test.go | 5 ++--- go.mod | 2 +- option.go | 5 +++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/etcd_resolver_test.go b/etcd_resolver_test.go index e702b9f..050bd94 100644 --- a/etcd_resolver_test.go +++ b/etcd_resolver_test.go @@ -23,7 +23,6 @@ import ( "crypto/x509/pkix" "encoding/pem" "fmt" - "io/ioutil" "math/big" "net" "net/url" @@ -365,7 +364,7 @@ func setupEmbedEtcd(t *testing.T) (*embed.Etcd, string) { endpoint := fmt.Sprintf("unix://localhost:%06d", os.Getpid()) u, err := url.Parse(endpoint) require.Nil(t, err) - dir, err := ioutil.TempDir("", "etcd_resolver_test") + dir, err := os.MkdirTemp("", "etcd_resolver_test") require.Nil(t, err) cfg := embed.NewConfig() @@ -385,7 +384,7 @@ func setupEmbedEtcdWithTLS(t *testing.T, caFile, certFile, keyFile string) (*emb endpoint := fmt.Sprintf("unixs://localhost:%06d", os.Getpid()) u, err := url.Parse(endpoint) require.Nil(t, err) - dir, err := ioutil.TempDir("", "etcd_resolver_test") + dir, err := os.MkdirTemp("", "etcd_resolver_test") require.Nil(t, err) cfg := embed.NewConfig() diff --git a/go.mod b/go.mod index 170b1ee..c25c252 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/kitex-contrib/registry-etcd go 1.21 -toolchain go1.21.4 +//toolchain go1.21.4 replace github.com/apache/thrift => github.com/apache/thrift v0.13.0 diff --git a/option.go b/option.go index 2dd37be..2176e19 100644 --- a/option.go +++ b/option.go @@ -18,7 +18,8 @@ import ( "crypto/tls" "crypto/x509" "errors" - "io/ioutil" + "os" + _ "os" "time" "github.com/cloudwego/kitex/pkg/klog" @@ -66,7 +67,7 @@ func newTLSConfig(certFile, keyFile, caFile, serverName string) (*tls.Config, er if err != nil { return nil, err } - caCert, err := ioutil.ReadFile(caFile) + caCert, err := os.ReadFile(caFile) if err != nil { return nil, err } From 5434458c2d2e9fbcf48b73454adabf4ed027401f Mon Sep 17 00:00:00 2001 From: "logic.wu0" <605524858@qq.com> Date: Thu, 9 May 2024 11:55:42 +0800 Subject: [PATCH 3/7] Support modification of prefix --- etcd_resolver_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etcd_resolver_test.go b/etcd_resolver_test.go index 050bd94..4e6fdba 100644 --- a/etcd_resolver_test.go +++ b/etcd_resolver_test.go @@ -573,8 +573,8 @@ func TestEtcdResolverWithEtcdPrefix(t *testing.T) { } func TestEtcdResolverWithEtcdPrefix2(t *testing.T) { + etcdPrefixTpl = "kitex/registry-etcd/%v/" s, endpoint := setupEmbedEtcd(t) - rg, err := NewEtcdRegistry([]string{endpoint}) require.Nil(t, err) rs, err := NewEtcdResolver([]string{endpoint}) From c99aabb07ad01218ecb2f34ddea5b63cfa136257 Mon Sep 17 00:00:00 2001 From: "logic.wu0" <605524858@qq.com> Date: Mon, 13 May 2024 03:14:02 +0800 Subject: [PATCH 4/7] Support modification of prefix --- common.go | 13 +++++-------- etcd_registry.go | 30 ++++++++++++++++++----------- etcd_resolver.go | 18 +++++++++++++----- etcd_resolver_test.go | 18 ++++++++---------- example/client/main.go | 2 +- option.go | 43 ++++++++++++++++++++++-------------------- 6 files changed, 69 insertions(+), 55 deletions(-) diff --git a/common.go b/common.go index d4d761d..533c38d 100644 --- a/common.go +++ b/common.go @@ -16,17 +16,14 @@ package etcd import "fmt" -var ( - etcdPrefixTpl = "kitex/registry-etcd/%v/" -) - -func serviceKeyPrefix(serviceName string) string { - return fmt.Sprintf(etcdPrefixTpl, serviceName) +func serviceKeyPrefix(prefix string, serviceName string) string { + prefix = prefix + "/%v/" + return fmt.Sprintf(prefix, serviceName) } // serviceKey generates the key used to stored in etcd. -func serviceKey(serviceName, addr string) string { - return serviceKeyPrefix(serviceName) + addr +func serviceKey(prefix string, serviceName, addr string) string { + return serviceKeyPrefix(prefix, serviceName) + addr } // instanceInfo used to stored service basic info in etcd. diff --git a/etcd_registry.go b/etcd_registry.go index 5e3fa78..473068b 100644 --- a/etcd_registry.go +++ b/etcd_registry.go @@ -45,6 +45,7 @@ type etcdRegistry struct { retryConfig *retry.Config stop chan struct{} address net.Addr + prefix string } type registerMeta struct { @@ -55,13 +56,16 @@ type registerMeta struct { // NewEtcdRegistry creates an etcd based registry. func NewEtcdRegistry(endpoints []string, opts ...Option) (registry.Registry, error) { - cfg := clientv3.Config{ - Endpoints: endpoints, + cfg := &ConfigWithPrefix{ + Configs: &clientv3.Config{ // 注意这里添加了 & 来获取 Config 的地址 + Endpoints: endpoints, + }, + Prefix: "kitex/registry-etcd", // 默认前缀 } for _, opt := range opts { - opt(&cfg) + opt(cfg) } - etcdClient, err := clientv3.New(cfg) + etcdClient, err := clientv3.New(*cfg.Configs) if err != nil { return nil, err } @@ -71,6 +75,7 @@ func NewEtcdRegistry(endpoints []string, opts ...Option) (registry.Registry, err leaseTTL: getTTL(), retryConfig: retryConfig, stop: make(chan struct{}, 1), + prefix: cfg.Prefix, }, nil } @@ -86,13 +91,16 @@ func SetFixedAddress(r registry.Registry, address net.Addr) { // NewEtcdRegistryWithRetry creates an etcd based registry with given custom retry configs func NewEtcdRegistryWithRetry(endpoints []string, retryConfig *retry.Config, opts ...Option) (registry.Registry, error) { - cfg := clientv3.Config{ - Endpoints: endpoints, + cfg := &ConfigWithPrefix{ + Configs: &clientv3.Config{ // 注意这里添加了 & 来获取 Config 的地址 + Endpoints: endpoints, + }, + Prefix: "defaultPrefix", // 默认前缀 } for _, opt := range opts { - opt(&cfg) + opt(cfg) } - etcdClient, err := clientv3.New(cfg) + etcdClient, err := clientv3.New(*cfg.Configs) if err != nil { return nil, err } @@ -181,14 +189,14 @@ func (e *etcdRegistry) register(info *registry.Info, leaseID clientv3.LeaseID) e } ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() - _, err = e.etcdClient.Put(ctx, serviceKey(info.ServiceName, addr), string(val), clientv3.WithLease(leaseID)) + _, err = e.etcdClient.Put(ctx, serviceKey(e.prefix, info.ServiceName, addr), string(val), clientv3.WithLease(leaseID)) if err != nil { return err } go func(key, val string) { e.keepRegister(key, val, e.retryConfig) - }(serviceKey(info.ServiceName, addr), string(val)) + }(serviceKey(e.prefix, info.ServiceName, addr), string(val)) return nil } @@ -262,7 +270,7 @@ func (e *etcdRegistry) deregister(info *registry.Info) error { if err != nil { return err } - _, err = e.etcdClient.Delete(ctx, serviceKey(info.ServiceName, addr)) + _, err = e.etcdClient.Delete(ctx, serviceKey(e.prefix, info.ServiceName, addr)) if err != nil { return err } diff --git a/etcd_resolver.go b/etcd_resolver.go index 91e53b4..e56c525 100644 --- a/etcd_resolver.go +++ b/etcd_resolver.go @@ -33,22 +33,27 @@ const ( // etcdResolver is a resolver using etcd. type etcdResolver struct { etcdClient *clientv3.Client + prefix string } // NewEtcdResolver creates a etcd based resolver. func NewEtcdResolver(endpoints []string, opts ...Option) (discovery.Resolver, error) { - cfg := clientv3.Config{ - Endpoints: endpoints, + cfg := &ConfigWithPrefix{ + Configs: &clientv3.Config{ // 注意这里添加了 & 来获取 Config 的地址 + Endpoints: endpoints, + }, + Prefix: "kitex/registry-etcd", // 默认前缀 } for _, opt := range opts { - opt(&cfg) + opt(cfg) } - etcdClient, err := clientv3.New(cfg) + etcdClient, err := clientv3.New(*cfg.Configs) if err != nil { return nil, err } return &etcdResolver{ etcdClient: etcdClient, + prefix: cfg.Prefix, }, nil } @@ -75,7 +80,7 @@ func (e *etcdResolver) Target(ctx context.Context, target rpcinfo.EndpointInfo) // Resolve implements the Resolver interface. func (e *etcdResolver) Resolve(ctx context.Context, desc string) (discovery.Result, error) { - prefix := serviceKeyPrefix(desc) + prefix := serviceKeyPrefix(e.prefix, desc) resp, err := e.etcdClient.Get(ctx, prefix, clientv3.WithPrefix()) if err != nil { return discovery.Result{}, err @@ -113,3 +118,6 @@ func (e *etcdResolver) Diff(cacheKey string, prev, next discovery.Result) (disco func (e *etcdResolver) Name() string { return "etcd" } +func (e *etcdResolver) GetPrefix() string { + return e.prefix +} diff --git a/etcd_resolver_test.go b/etcd_resolver_test.go index 4e6fdba..270f759 100644 --- a/etcd_resolver_test.go +++ b/etcd_resolver_test.go @@ -23,6 +23,7 @@ import ( "crypto/x509/pkix" "encoding/pem" "fmt" + "io/ioutil" "math/big" "net" "net/url" @@ -364,7 +365,7 @@ func setupEmbedEtcd(t *testing.T) (*embed.Etcd, string) { endpoint := fmt.Sprintf("unix://localhost:%06d", os.Getpid()) u, err := url.Parse(endpoint) require.Nil(t, err) - dir, err := os.MkdirTemp("", "etcd_resolver_test") + dir, err := ioutil.TempDir("", "etcd_resolver_test") require.Nil(t, err) cfg := embed.NewConfig() @@ -384,7 +385,7 @@ func setupEmbedEtcdWithTLS(t *testing.T, caFile, certFile, keyFile string) (*emb endpoint := fmt.Sprintf("unixs://localhost:%06d", os.Getpid()) u, err := url.Parse(endpoint) require.Nil(t, err) - dir, err := os.MkdirTemp("", "etcd_resolver_test") + dir, err := ioutil.TempDir("", "etcd_resolver_test") require.Nil(t, err) cfg := embed.NewConfig() @@ -510,14 +511,12 @@ 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)) + rg, err := NewEtcdRegistry([]string{endpoint}, WithEtcdConfigAndPrefix(tpl)) + require.Nil(t, err) + rs, err := NewEtcdResolver([]string{endpoint}, WithEtcdConfigAndPrefix(tpl)) require.Nil(t, err) infoList := []registry.Info{ @@ -552,7 +551,7 @@ func TestEtcdResolverWithEtcdPrefix(t *testing.T) { }, } require.Equal(t, expected, result) - prefix := serviceKeyPrefix(info.ServiceName) + prefix := serviceKeyPrefix(rs.(*etcdResolver).GetPrefix(), info.ServiceName) println(prefix) require.Equal(t, fmt.Sprintf(tpl+"/%v/", info.ServiceName), prefix) } @@ -573,7 +572,6 @@ func TestEtcdResolverWithEtcdPrefix(t *testing.T) { } func TestEtcdResolverWithEtcdPrefix2(t *testing.T) { - etcdPrefixTpl = "kitex/registry-etcd/%v/" s, endpoint := setupEmbedEtcd(t) rg, err := NewEtcdRegistry([]string{endpoint}) require.Nil(t, err) @@ -612,7 +610,7 @@ func TestEtcdResolverWithEtcdPrefix2(t *testing.T) { }, } require.Equal(t, expected, result) - prefix := serviceKeyPrefix(info.ServiceName) + prefix := serviceKeyPrefix(rs.(*etcdResolver).GetPrefix(), info.ServiceName) println(prefix) require.Equal(t, fmt.Sprintf("kitex/registry-etcd/%v/", info.ServiceName), prefix) } diff --git a/example/client/main.go b/example/client/main.go index 1707340..aa2b7ef 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"}, etcd.WithEtcdPrefixNewTpl("etcd/v2")) + r, err := etcd.NewEtcdResolver([]string{"127.0.0.1:2379"}) if err != nil { log.Fatal(err) } diff --git a/option.go b/option.go index 2176e19..fab3621 100644 --- a/option.go +++ b/option.go @@ -18,47 +18,38 @@ import ( "crypto/tls" "crypto/x509" "errors" - "os" - _ "os" - "time" - "github.com/cloudwego/kitex/pkg/klog" clientv3 "go.etcd.io/etcd/client/v3" + "io/ioutil" + "time" ) // Option sets options such as username, tls etc. -type Option func(cfg *clientv3.Config) +type Option func(cfg *ConfigWithPrefix) // WithTLSOpt returns a option that authentication by tls/ssl. func WithTLSOpt(certFile, keyFile, caFile string) Option { - return func(cfg *clientv3.Config) { + return func(cfg *ConfigWithPrefix) { tlsCfg, err := newTLSConfig(certFile, keyFile, caFile, "") if err != nil { klog.Errorf("tls failed with err: %v , skipping tls.", err) } - cfg.TLS = tlsCfg - } -} - -// WithEtcdPrefixNewTpl returns a option prefix -func WithEtcdPrefixNewTpl(etcdPrefixNewTpl string) Option { - return func(cfg *clientv3.Config) { - etcdPrefixTpl = etcdPrefixNewTpl + "/%v/" + cfg.Configs.TLS = tlsCfg } } // WithAuthOpt returns a option that authentication by usernane and password. func WithAuthOpt(username, password string) Option { - return func(cfg *clientv3.Config) { - cfg.Username = username - cfg.Password = password + return func(cfg *ConfigWithPrefix) { + cfg.Configs.Username = username + cfg.Configs.Password = password } } // WithDialTimeoutOpt returns a option set dialTimeout func WithDialTimeoutOpt(dialTimeout time.Duration) Option { - return func(cfg *clientv3.Config) { - cfg.DialTimeout = dialTimeout + return func(cfg *ConfigWithPrefix) { + cfg.Configs.DialTimeout = dialTimeout } } @@ -67,7 +58,7 @@ func newTLSConfig(certFile, keyFile, caFile, serverName string) (*tls.Config, er if err != nil { return nil, err } - caCert, err := os.ReadFile(caFile) + caCert, err := ioutil.ReadFile(caFile) if err != nil { return nil, err } @@ -82,3 +73,15 @@ func newTLSConfig(certFile, keyFile, caFile, serverName string) (*tls.Config, er } return cfg, nil } + +type ConfigWithPrefix struct { + Configs *clientv3.Config + Prefix string +} +type EtcdOption func(cfg *ConfigWithPrefix) + +func WithEtcdConfigAndPrefix(prefix string) Option { + return func(c *ConfigWithPrefix) { + c.Prefix = prefix + } +} From cad3c1ef12ef79d84ab9f29e28d9249a2a825648 Mon Sep 17 00:00:00 2001 From: "logic.wu0" <605524858@qq.com> Date: Mon, 13 May 2024 03:17:41 +0800 Subject: [PATCH 5/7] Support modification of prefix --- etcd_registry.go | 8 ++++---- etcd_resolver.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/etcd_registry.go b/etcd_registry.go index 473068b..62bf22d 100644 --- a/etcd_registry.go +++ b/etcd_registry.go @@ -57,10 +57,10 @@ type registerMeta struct { // NewEtcdRegistry creates an etcd based registry. func NewEtcdRegistry(endpoints []string, opts ...Option) (registry.Registry, error) { cfg := &ConfigWithPrefix{ - Configs: &clientv3.Config{ // 注意这里添加了 & 来获取 Config 的地址 + Configs: &clientv3.Config{ Endpoints: endpoints, }, - Prefix: "kitex/registry-etcd", // 默认前缀 + Prefix: "kitex/registry-etcd", } for _, opt := range opts { opt(cfg) @@ -92,10 +92,10 @@ func SetFixedAddress(r registry.Registry, address net.Addr) { // NewEtcdRegistryWithRetry creates an etcd based registry with given custom retry configs func NewEtcdRegistryWithRetry(endpoints []string, retryConfig *retry.Config, opts ...Option) (registry.Registry, error) { cfg := &ConfigWithPrefix{ - Configs: &clientv3.Config{ // 注意这里添加了 & 来获取 Config 的地址 + Configs: &clientv3.Config{ Endpoints: endpoints, }, - Prefix: "defaultPrefix", // 默认前缀 + Prefix: "kitex/registry-etcd", } for _, opt := range opts { opt(cfg) diff --git a/etcd_resolver.go b/etcd_resolver.go index e56c525..f5c4aaf 100644 --- a/etcd_resolver.go +++ b/etcd_resolver.go @@ -39,10 +39,10 @@ type etcdResolver struct { // NewEtcdResolver creates a etcd based resolver. func NewEtcdResolver(endpoints []string, opts ...Option) (discovery.Resolver, error) { cfg := &ConfigWithPrefix{ - Configs: &clientv3.Config{ // 注意这里添加了 & 来获取 Config 的地址 + Configs: &clientv3.Config{ Endpoints: endpoints, }, - Prefix: "kitex/registry-etcd", // 默认前缀 + Prefix: "kitex/registry-etcd", } for _, opt := range opts { opt(cfg) From 80a7c5748c82ab89d183d73867b8f09b2e494984 Mon Sep 17 00:00:00 2001 From: "logic.wu0" <605524858@qq.com> Date: Mon, 24 Jun 2024 01:58:21 +0800 Subject: [PATCH 6/7] Support modification of prefix --- etcd_registry.go | 12 ++++++------ etcd_resolver.go | 6 +++--- etcd_resolver_test.go | 2 +- go.mod | 2 +- option.go | 32 ++++++++++++++++---------------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/etcd_registry.go b/etcd_registry.go index 62bf22d..79522aa 100644 --- a/etcd_registry.go +++ b/etcd_registry.go @@ -56,8 +56,8 @@ type registerMeta struct { // NewEtcdRegistry creates an etcd based registry. func NewEtcdRegistry(endpoints []string, opts ...Option) (registry.Registry, error) { - cfg := &ConfigWithPrefix{ - Configs: &clientv3.Config{ + cfg := &EtcdConfig{ + Config: &clientv3.Config{ Endpoints: endpoints, }, Prefix: "kitex/registry-etcd", @@ -65,7 +65,7 @@ func NewEtcdRegistry(endpoints []string, opts ...Option) (registry.Registry, err for _, opt := range opts { opt(cfg) } - etcdClient, err := clientv3.New(*cfg.Configs) + etcdClient, err := clientv3.New(*cfg.Config) if err != nil { return nil, err } @@ -91,8 +91,8 @@ func SetFixedAddress(r registry.Registry, address net.Addr) { // NewEtcdRegistryWithRetry creates an etcd based registry with given custom retry configs func NewEtcdRegistryWithRetry(endpoints []string, retryConfig *retry.Config, opts ...Option) (registry.Registry, error) { - cfg := &ConfigWithPrefix{ - Configs: &clientv3.Config{ + cfg := &EtcdConfig{ + Config: &clientv3.Config{ Endpoints: endpoints, }, Prefix: "kitex/registry-etcd", @@ -100,7 +100,7 @@ func NewEtcdRegistryWithRetry(endpoints []string, retryConfig *retry.Config, opt for _, opt := range opts { opt(cfg) } - etcdClient, err := clientv3.New(*cfg.Configs) + etcdClient, err := clientv3.New(*cfg.Config) if err != nil { return nil, err } diff --git a/etcd_resolver.go b/etcd_resolver.go index f5c4aaf..a9fe58a 100644 --- a/etcd_resolver.go +++ b/etcd_resolver.go @@ -38,8 +38,8 @@ type etcdResolver struct { // NewEtcdResolver creates a etcd based resolver. func NewEtcdResolver(endpoints []string, opts ...Option) (discovery.Resolver, error) { - cfg := &ConfigWithPrefix{ - Configs: &clientv3.Config{ + cfg := &EtcdConfig{ + Config: &clientv3.Config{ Endpoints: endpoints, }, Prefix: "kitex/registry-etcd", @@ -47,7 +47,7 @@ func NewEtcdResolver(endpoints []string, opts ...Option) (discovery.Resolver, er for _, opt := range opts { opt(cfg) } - etcdClient, err := clientv3.New(*cfg.Configs) + etcdClient, err := clientv3.New(*cfg.Config) if err != nil { return nil, err } diff --git a/etcd_resolver_test.go b/etcd_resolver_test.go index 270f759..35ded9a 100644 --- a/etcd_resolver_test.go +++ b/etcd_resolver_test.go @@ -23,7 +23,7 @@ import ( "crypto/x509/pkix" "encoding/pem" "fmt" - "io/ioutil" + "io/ioutil" //nolint "math/big" "net" "net/url" diff --git a/go.mod b/go.mod index c25c252..170b1ee 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/kitex-contrib/registry-etcd go 1.21 -//toolchain go1.21.4 +toolchain go1.21.4 replace github.com/apache/thrift => github.com/apache/thrift v0.13.0 diff --git a/option.go b/option.go index fab3621..dd6d6cc 100644 --- a/option.go +++ b/option.go @@ -20,36 +20,42 @@ import ( "errors" "github.com/cloudwego/kitex/pkg/klog" clientv3 "go.etcd.io/etcd/client/v3" - "io/ioutil" + "io/ioutil" //nolint "time" ) // Option sets options such as username, tls etc. -type Option func(cfg *ConfigWithPrefix) +type Option func(cfg *EtcdConfig) + +type EtcdConfig struct { + Config *clientv3.Config + Prefix string +} +type EtcdOption func(cfg *EtcdConfig) // WithTLSOpt returns a option that authentication by tls/ssl. func WithTLSOpt(certFile, keyFile, caFile string) Option { - return func(cfg *ConfigWithPrefix) { + return func(cfg *EtcdConfig) { tlsCfg, err := newTLSConfig(certFile, keyFile, caFile, "") if err != nil { klog.Errorf("tls failed with err: %v , skipping tls.", err) } - cfg.Configs.TLS = tlsCfg + cfg.Config.TLS = tlsCfg } } // WithAuthOpt returns a option that authentication by usernane and password. func WithAuthOpt(username, password string) Option { - return func(cfg *ConfigWithPrefix) { - cfg.Configs.Username = username - cfg.Configs.Password = password + return func(cfg *EtcdConfig) { + cfg.Config.Username = username + cfg.Config.Password = password } } // WithDialTimeoutOpt returns a option set dialTimeout func WithDialTimeoutOpt(dialTimeout time.Duration) Option { - return func(cfg *ConfigWithPrefix) { - cfg.Configs.DialTimeout = dialTimeout + return func(cfg *EtcdConfig) { + cfg.Config.DialTimeout = dialTimeout } } @@ -74,14 +80,8 @@ func newTLSConfig(certFile, keyFile, caFile, serverName string) (*tls.Config, er return cfg, nil } -type ConfigWithPrefix struct { - Configs *clientv3.Config - Prefix string -} -type EtcdOption func(cfg *ConfigWithPrefix) - func WithEtcdConfigAndPrefix(prefix string) Option { - return func(c *ConfigWithPrefix) { + return func(c *EtcdConfig) { c.Prefix = prefix } } From 06dbfbfd156217b5c831ad370af8b7ba99d37103 Mon Sep 17 00:00:00 2001 From: "logic.wu0" <605524858@qq.com> Date: Tue, 25 Jun 2024 09:18:32 +0800 Subject: [PATCH 7/7] Modify naming, add comments --- etcd_registry.go | 12 ++++++------ etcd_resolver.go | 6 +++--- option.go | 26 +++++++++++++------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/etcd_registry.go b/etcd_registry.go index 79522aa..09b0d4c 100644 --- a/etcd_registry.go +++ b/etcd_registry.go @@ -56,8 +56,8 @@ type registerMeta struct { // NewEtcdRegistry creates an etcd based registry. func NewEtcdRegistry(endpoints []string, opts ...Option) (registry.Registry, error) { - cfg := &EtcdConfig{ - Config: &clientv3.Config{ + cfg := &Config{ + EtcdConfig: &clientv3.Config{ Endpoints: endpoints, }, Prefix: "kitex/registry-etcd", @@ -65,7 +65,7 @@ func NewEtcdRegistry(endpoints []string, opts ...Option) (registry.Registry, err for _, opt := range opts { opt(cfg) } - etcdClient, err := clientv3.New(*cfg.Config) + etcdClient, err := clientv3.New(*cfg.EtcdConfig) if err != nil { return nil, err } @@ -91,8 +91,8 @@ func SetFixedAddress(r registry.Registry, address net.Addr) { // NewEtcdRegistryWithRetry creates an etcd based registry with given custom retry configs func NewEtcdRegistryWithRetry(endpoints []string, retryConfig *retry.Config, opts ...Option) (registry.Registry, error) { - cfg := &EtcdConfig{ - Config: &clientv3.Config{ + cfg := &Config{ + EtcdConfig: &clientv3.Config{ Endpoints: endpoints, }, Prefix: "kitex/registry-etcd", @@ -100,7 +100,7 @@ func NewEtcdRegistryWithRetry(endpoints []string, retryConfig *retry.Config, opt for _, opt := range opts { opt(cfg) } - etcdClient, err := clientv3.New(*cfg.Config) + etcdClient, err := clientv3.New(*cfg.EtcdConfig) if err != nil { return nil, err } diff --git a/etcd_resolver.go b/etcd_resolver.go index a9fe58a..2fd2a57 100644 --- a/etcd_resolver.go +++ b/etcd_resolver.go @@ -38,8 +38,8 @@ type etcdResolver struct { // NewEtcdResolver creates a etcd based resolver. func NewEtcdResolver(endpoints []string, opts ...Option) (discovery.Resolver, error) { - cfg := &EtcdConfig{ - Config: &clientv3.Config{ + cfg := &Config{ + EtcdConfig: &clientv3.Config{ Endpoints: endpoints, }, Prefix: "kitex/registry-etcd", @@ -47,7 +47,7 @@ func NewEtcdResolver(endpoints []string, opts ...Option) (discovery.Resolver, er for _, opt := range opts { opt(cfg) } - etcdClient, err := clientv3.New(*cfg.Config) + etcdClient, err := clientv3.New(*cfg.EtcdConfig) if err != nil { return nil, err } diff --git a/option.go b/option.go index dd6d6cc..dcce768 100644 --- a/option.go +++ b/option.go @@ -25,37 +25,36 @@ import ( ) // Option sets options such as username, tls etc. -type Option func(cfg *EtcdConfig) +type Option func(cfg *Config) -type EtcdConfig struct { - Config *clientv3.Config - Prefix string +type Config struct { + EtcdConfig *clientv3.Config + Prefix string } -type EtcdOption func(cfg *EtcdConfig) // WithTLSOpt returns a option that authentication by tls/ssl. func WithTLSOpt(certFile, keyFile, caFile string) Option { - return func(cfg *EtcdConfig) { + return func(cfg *Config) { tlsCfg, err := newTLSConfig(certFile, keyFile, caFile, "") if err != nil { klog.Errorf("tls failed with err: %v , skipping tls.", err) } - cfg.Config.TLS = tlsCfg + cfg.EtcdConfig.TLS = tlsCfg } } // WithAuthOpt returns a option that authentication by usernane and password. func WithAuthOpt(username, password string) Option { - return func(cfg *EtcdConfig) { - cfg.Config.Username = username - cfg.Config.Password = password + return func(cfg *Config) { + cfg.EtcdConfig.Username = username + cfg.EtcdConfig.Password = password } } // WithDialTimeoutOpt returns a option set dialTimeout func WithDialTimeoutOpt(dialTimeout time.Duration) Option { - return func(cfg *EtcdConfig) { - cfg.Config.DialTimeout = dialTimeout + return func(cfg *Config) { + cfg.EtcdConfig.DialTimeout = dialTimeout } } @@ -80,8 +79,9 @@ func newTLSConfig(certFile, keyFile, caFile, serverName string) (*tls.Config, er return cfg, nil } +// WithEtcdConfigAndPrefix returns an option that sets the Prefix field in the Config struct func WithEtcdConfigAndPrefix(prefix string) Option { - return func(c *EtcdConfig) { + return func(c *Config) { c.Prefix = prefix } }