From 09b99e76887d697e60cac9b140efb3b73cdcf791 Mon Sep 17 00:00:00 2001 From: smx_Moragn <1048492656@qq.com> Date: Thu, 12 Sep 2024 22:07:44 +0800 Subject: [PATCH] integration config --- registry/consul/consulhertz/consul_test.go | 3 +- registry/consul/consulhertz/registry.go | 21 ++--- .../consul/consulkitex/consul_registry.go | 26 +++--- registry/consul/consulkitex/consul_test.go | 3 +- registry/consul/options/option.go | 8 +- registry/nacos/nacoshertz/nacos_test.go | 7 +- registry/nacos/nacoshertz/registry.go | 49 +++-------- .../nacos/nacoskitex/registry/registry.go | 39 +++------ .../nacoskitex/registry/registry_test.go | 11 +-- registry/nacos/options/options.go | 35 ++++++++ registry/servicecomb/options/options.go | 54 ++++++++++++ .../servicecomb/servicecombhertz/registry.go | 84 ++++++++++--------- .../servicecombkitex/registry/registry.go | 37 ++++---- 13 files changed, 220 insertions(+), 157 deletions(-) create mode 100644 registry/nacos/options/options.go create mode 100644 registry/servicecomb/options/options.go diff --git a/registry/consul/consulhertz/consul_test.go b/registry/consul/consulhertz/consul_test.go index 229975f..430ecd3 100644 --- a/registry/consul/consulhertz/consul_test.go +++ b/registry/consul/consulhertz/consul_test.go @@ -17,6 +17,7 @@ package consulhertz import ( "context" "fmt" + "github.com/cloudwego-contrib/cwgo-pkg/registry/consul/options" "log" "net" "testing" @@ -122,7 +123,7 @@ func TestNewConsulRegisterWithCheckOption(t *testing.T) { check.Interval = "10s" check.DeregisterCriticalServiceAfter = "1m" - consulResolver := NewConsulRegister(cli, WithCheck(check)) + consulResolver := NewConsulRegister(cli, options.WithCheck(check)) assert.NotNil(t, consulResolver) } diff --git a/registry/consul/consulhertz/registry.go b/registry/consul/consulhertz/registry.go index 25bb0b8..4e04b3e 100644 --- a/registry/consul/consulhertz/registry.go +++ b/registry/consul/consulhertz/registry.go @@ -17,6 +17,7 @@ package consulhertz import ( "errors" "fmt" + "github.com/cloudwego-contrib/cwgo-pkg/registry/consul/options" "net" "github.com/cloudwego-contrib/cwgo-pkg/registry/consul/internal" @@ -38,12 +39,12 @@ var ( type consulRegistry struct { consulClient *api.Client - opts options + opts options.Options } var _ registry.Registry = (*consulRegistry)(nil) -type options struct { +/*type options struct { check *api.AgentServiceCheck } @@ -53,12 +54,12 @@ type Option func(o *options) // WithCheck is consul registry-etcdhertz option to set AgentServiceCheck. func WithCheck(check *api.AgentServiceCheck) Option { return func(o *options) { o.check = check } -} +}*/ // NewConsulRegister create a new registry-etcdhertz using consul. -func NewConsulRegister(consulClient *api.Client, opts ...Option) registry.Registry { - op := options{ - check: internal.DefaultCheck(), +func NewConsulRegister(consulClient *api.Client, opts ...options.Option) registry.Registry { + op := options.Options{ + Check: internal.DefaultCheck(), } for _, opt := range opts { @@ -98,11 +99,11 @@ func (c *consulRegistry) Register(info *registry.Info) error { Passing: info.Weight, Warning: info.Weight, }, - Check: c.opts.check, + Check: c.opts.Check, } - if c.opts.check != nil { - c.opts.check.TCP = net.JoinHostPort(host, fmt.Sprintf("%d", port)) - svcInfo.Check = c.opts.check + if c.opts.Check != nil { + c.opts.Check.TCP = net.JoinHostPort(host, fmt.Sprintf("%d", port)) + svcInfo.Check = c.opts.Check } return c.consulClient.Agent().ServiceRegister(svcInfo) diff --git a/registry/consul/consulkitex/consul_registry.go b/registry/consul/consulkitex/consul_registry.go index f7f63d3..1e6d170 100644 --- a/registry/consul/consulkitex/consul_registry.go +++ b/registry/consul/consulkitex/consul_registry.go @@ -19,6 +19,7 @@ package consul import ( "errors" "fmt" + "github.com/cloudwego-contrib/cwgo-pkg/registry/consul/options" "github.com/cloudwego-contrib/cwgo-pkg/registry/consul/internal" "github.com/cloudwego/kitex/pkg/registry" @@ -27,9 +28,10 @@ import ( type consulRegistry struct { consulClient *api.Client - opts options + opts options.Options } +/* type options struct { check *api.AgentServiceCheck } @@ -40,7 +42,7 @@ type Option func(o *options) // WithCheck is consul registry option to set AgentServiceCheck. func WithCheck(check *api.AgentServiceCheck) Option { return func(o *options) { o.check = check } -} +}*/ const kvJoinChar = ":" @@ -49,7 +51,7 @@ var _ registry.Registry = (*consulRegistry)(nil) var errIllegalTagChar = errors.New("illegal tag character") // NewConsulRegister create a new registry using consul. -func NewConsulRegister(address string, opts ...Option) (registry.Registry, error) { +func NewConsulRegister(address string, opts ...options.Option) (registry.Registry, error) { config := api.DefaultConfig() config.Address = address client, err := api.NewClient(config) @@ -57,8 +59,8 @@ func NewConsulRegister(address string, opts ...Option) (registry.Registry, error return nil, err } - op := options{ - check: internal.DefaultCheck(), + op := options.Options{ + Check: internal.DefaultCheck(), } for _, option := range opts { @@ -69,14 +71,14 @@ func NewConsulRegister(address string, opts ...Option) (registry.Registry, error } // NewConsulRegisterWithConfig create a new registry using consul, with a custom config. -func NewConsulRegisterWithConfig(config *api.Config, opts ...Option) (*consulRegistry, error) { +func NewConsulRegisterWithConfig(config *api.Config, opts ...options.Option) (*consulRegistry, error) { client, err := api.NewClient(config) if err != nil { return nil, err } - op := options{ - check: internal.DefaultCheck(), + op := options.Options{ + Check: internal.DefaultCheck(), } for _, option := range opts { @@ -118,12 +120,12 @@ func (c *consulRegistry) Register(info *registry.Info) error { Passing: info.Weight, Warning: info.Weight, }, - Check: c.opts.check, + Check: c.opts.Check, } - if c.opts.check != nil { - c.opts.check.TCP = fmt.Sprintf("%s:%d", host, port) - svcInfo.Check = c.opts.check + if c.opts.Check != nil { + c.opts.Check.TCP = fmt.Sprintf("%s:%d", host, port) + svcInfo.Check = c.opts.Check } return c.consulClient.Agent().ServiceRegister(svcInfo) diff --git a/registry/consul/consulkitex/consul_test.go b/registry/consul/consulkitex/consul_test.go index 68a9b00..43904e1 100644 --- a/registry/consul/consulkitex/consul_test.go +++ b/registry/consul/consulkitex/consul_test.go @@ -20,6 +20,7 @@ import ( "context" "errors" "fmt" + "github.com/cloudwego-contrib/cwgo-pkg/registry/consul/options" "log" "net" "strconv" @@ -83,7 +84,7 @@ func TestNewConsulRegisterWithConfig(t *testing.T) { Address: consulAddr, WaitTime: 5 * time.Second, Namespace: "TEST-NS", - }, WithCheck(&consulapi.AgentServiceCheck{ + }, options.WithCheck(&consulapi.AgentServiceCheck{ Interval: "7s", Timeout: "5s", DeregisterCriticalServiceAfter: "15s", diff --git a/registry/consul/options/option.go b/registry/consul/options/option.go index e4cffa9..9817309 100644 --- a/registry/consul/options/option.go +++ b/registry/consul/options/option.go @@ -18,14 +18,14 @@ package options import "github.com/hashicorp/consul/api" -type options struct { - check *api.AgentServiceCheck +type Options struct { + Check *api.AgentServiceCheck } // Option is the option of Consul. -type Option func(o *options) +type Option func(o *Options) // WithCheck is consul registry-etcdhertz option to set AgentServiceCheck. func WithCheck(check *api.AgentServiceCheck) Option { - return func(o *options) { o.check = check } + return func(o *Options) { o.Check = check } } diff --git a/registry/nacos/nacoshertz/nacos_test.go b/registry/nacos/nacoshertz/nacos_test.go index 297d869..796e125 100644 --- a/registry/nacos/nacoshertz/nacos_test.go +++ b/registry/nacos/nacoshertz/nacos_test.go @@ -16,6 +16,7 @@ package nacos import ( "context" + "github.com/cloudwego-contrib/cwgo-pkg/registry/nacos/options" "strings" "testing" "time" @@ -153,7 +154,7 @@ func TestMultiInstancesWithDefRegistry(t *testing.T) { clusterName = "TheCluster" groupName = "TheGroup" ) - got, err := NewDefaultNacosRegistry(WithRegistryCluster(clusterName), WithRegistryGroup(groupName)) + got, err := NewDefaultNacosRegistry(options.WithCluster(clusterName), options.WithGroup(groupName)) assert.Nil(t, err) time.Sleep(time.Second) @@ -216,7 +217,7 @@ func TestMultipleInstances(t *testing.T) { ) time.Sleep(time.Second) - got := NewNacosRegistry(namingClient, WithRegistryCluster(clusterName), WithRegistryGroup(groupName)) + got := NewNacosRegistry(namingClient, options.WithCluster(clusterName), options.WithGroup(groupName)) if !assert.NotNil(t, got) { t.Errorf("err: new registry-etcdhertz fail") return @@ -377,7 +378,7 @@ func TestResolverDifferentGroup(t *testing.T) { ctx.String(200, "pong1") }) - opts2 = append(opts2, server.WithRegistry(NewNacosRegistry(namingClient, WithRegistryGroup("OTHER")), ®istry.Info{ + opts2 = append(opts2, server.WithRegistry(NewNacosRegistry(namingClient, options.WithGroup("OTHER")), ®istry.Info{ ServiceName: "demo.etcdhertz-contrib.test1", Addr: utils.NewNetAddr("tcp", "127.0.0.1:7001"), Weight: 10, diff --git a/registry/nacos/nacoshertz/registry.go b/registry/nacos/nacoshertz/registry.go index c08f412..23213e5 100644 --- a/registry/nacos/nacoshertz/registry.go +++ b/registry/nacos/nacoshertz/registry.go @@ -16,6 +16,7 @@ package nacos import ( "fmt" + "github.com/cloudwego-contrib/cwgo-pkg/registry/nacos/options" "net" "strconv" @@ -29,33 +30,9 @@ import ( var _ registry.Registry = (*nacosRegistry)(nil) -type ( - nacosRegistry struct { - client naming_client.INamingClient - opts registryOptions - } - - registryOptions struct { - cluster string - group string - } - - // RegistryOption Option is nacos registry-etcdhertz option. - RegistryOption func(o *registryOptions) -) - -// WithRegistryCluster with cluster option. -func WithRegistryCluster(cluster string) RegistryOption { - return func(o *registryOptions) { - o.cluster = cluster - } -} - -// WithRegistryGroup with group option. -func WithRegistryGroup(group string) RegistryOption { - return func(o *registryOptions) { - o.group = group - } +type nacosRegistry struct { + client naming_client.INamingClient + opts options.Options } func (n *nacosRegistry) Register(info *registry.Info) error { @@ -78,8 +55,8 @@ func (n *nacosRegistry) Register(info *registry.Info) error { Ip: host, Port: uint64(p), ServiceName: info.ServiceName, - GroupName: n.opts.group, - ClusterName: n.opts.cluster, + GroupName: n.opts.Group, + ClusterName: n.opts.Cluster, Weight: float64(info.Weight), Enable: true, Healthy: true, @@ -128,8 +105,8 @@ func (n *nacosRegistry) Deregister(info *registry.Info) error { Ip: host, Port: uint64(portInt), ServiceName: info.ServiceName, - GroupName: n.opts.group, - Cluster: n.opts.cluster, + GroupName: n.opts.Group, + Cluster: n.opts.Cluster, Ephemeral: true, }) if success { @@ -142,7 +119,7 @@ func (n *nacosRegistry) Deregister(info *registry.Info) error { } // NewDefaultNacosRegistry create a default service registry-etcdhertz using nacos. -func NewDefaultNacosRegistry(opts ...RegistryOption) (registry.Registry, error) { +func NewDefaultNacosRegistry(opts ...options.Option) (registry.Registry, error) { client, err := common.NewDefaultNacosConfig() if err != nil { return nil, err @@ -151,10 +128,10 @@ func NewDefaultNacosRegistry(opts ...RegistryOption) (registry.Registry, error) } // NewNacosRegistry create a new registry-etcdhertz using nacos. -func NewNacosRegistry(client naming_client.INamingClient, opts ...RegistryOption) registry.Registry { - opt := registryOptions{ - cluster: "DEFAULT", - group: "DEFAULT_GROUP", +func NewNacosRegistry(client naming_client.INamingClient, opts ...options.Option) registry.Registry { + opt := options.Options{ + Cluster: "DEFAULT", + Group: "DEFAULT_GROUP", } for _, option := range opts { option(&opt) diff --git a/registry/nacos/nacoskitex/registry/registry.go b/registry/nacos/nacoskitex/registry/registry.go index 9a8c347..c771a5d 100644 --- a/registry/nacos/nacoskitex/registry/registry.go +++ b/registry/nacos/nacoskitex/registry/registry.go @@ -17,6 +17,7 @@ package registry import ( "errors" "fmt" + "github.com/cloudwego-contrib/cwgo-pkg/registry/nacos/options" "net" "strconv" @@ -26,31 +27,13 @@ import ( "github.com/nacos-group/nacos-sdk-go/vo" ) -type options struct { - cluster string - group string -} - -// Option is nacos option. -type Option func(o *options) - -// WithCluster with cluster option. -func WithCluster(cluster string) Option { - return func(o *options) { o.cluster = cluster } -} - -// WithGroup with group option. -func WithGroup(group string) Option { - return func(o *options) { o.group = group } -} - type nacosRegistry struct { cli naming_client.INamingClient - opts options + opts options.Options } // NewDefaultNacosRegistry create a default service registry using nacos. -func NewDefaultNacosRegistry(opts ...Option) (registry.Registry, error) { +func NewDefaultNacosRegistry(opts ...options.Option) (registry.Registry, error) { cli, err := nacos.NewDefaultNacosClient() if err != nil { return nil, err @@ -59,10 +42,10 @@ func NewDefaultNacosRegistry(opts ...Option) (registry.Registry, error) { } // NewNacosRegistry create a new registry using nacos. -func NewNacosRegistry(cli naming_client.INamingClient, opts ...Option) registry.Registry { - op := options{ - cluster: "DEFAULT", - group: "DEFAULT_GROUP", +func NewNacosRegistry(cli naming_client.INamingClient, opts ...options.Option) registry.Registry { + op := options.Options{ + Cluster: "DEFAULT", + Group: "DEFAULT_GROUP", } for _, option := range opts { option(&op) @@ -100,8 +83,8 @@ func (n *nacosRegistry) Register(info *registry.Info) error { Enable: true, Healthy: true, Metadata: mergeTags(info.Tags, nacos.Tags), - GroupName: n.opts.group, - ClusterName: n.opts.cluster, + GroupName: n.opts.Group, + ClusterName: n.opts.Cluster, Ephemeral: true, }) if e != nil { @@ -165,8 +148,8 @@ func (n *nacosRegistry) Deregister(info *registry.Info) error { Port: uint64(p), ServiceName: info.ServiceName, Ephemeral: true, - GroupName: n.opts.group, - Cluster: n.opts.cluster, + GroupName: n.opts.Group, + Cluster: n.opts.Cluster, }); err != nil { return err } diff --git a/registry/nacos/nacoskitex/registry/registry_test.go b/registry/nacos/nacoskitex/registry/registry_test.go index 4443088..b5d8bbb 100644 --- a/registry/nacos/nacoskitex/registry/registry_test.go +++ b/registry/nacos/nacoskitex/registry/registry_test.go @@ -15,6 +15,7 @@ package registry import ( + "github.com/cloudwego-contrib/cwgo-pkg/registry/nacos/options" "net" "testing" "time" @@ -56,7 +57,7 @@ func TestNewNacosRegistry(t *testing.T) { t.Errorf("err:%v", err) return } - got := NewNacosRegistry(client, WithCluster("DEFAULT"), WithGroup("DEFAULT_GROUP")) + got := NewNacosRegistry(client, options.WithCluster("DEFAULT"), options.WithGroup("DEFAULT_GROUP")) assert.NotNil(t, got) } @@ -94,7 +95,7 @@ func TestNacosRegistryRegister(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - n := NewNacosRegistry(tt.fields.cli, WithCluster("DEFAULT"), WithGroup("DEFAULT_GROUP")) + n := NewNacosRegistry(tt.fields.cli, options.WithCluster("DEFAULT"), options.WithGroup("DEFAULT_GROUP")) if err := n.Register(tt.args.info); (err != nil) != tt.wantErr { t.Errorf("Register() cwerror = %v, wantErr %v", err, tt.wantErr) } @@ -136,7 +137,7 @@ func TestNacosRegistryDeregister(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - n := NewNacosRegistry(tt.fields.cli, WithCluster("DEFAULT"), WithGroup("DEFAULT_GROUP")) + n := NewNacosRegistry(tt.fields.cli, options.WithCluster("DEFAULT"), options.WithGroup("DEFAULT_GROUP")) if err := n.Deregister(tt.args.info); (err != nil) != tt.wantErr { t.Errorf("Deregister() cwerror = %v, wantErr %v", err, tt.wantErr) } @@ -157,7 +158,7 @@ func TestNacosMultipleInstances(t *testing.T) { return } time.Sleep(time.Second) - got := NewNacosRegistry(client, WithCluster(clusterName), WithGroup(groupName)) + got := NewNacosRegistry(client, options.WithCluster(clusterName), options.WithGroup(groupName)) if !assert.NotNil(t, got) { t.Errorf("err: new registry fail") return @@ -234,7 +235,7 @@ func TestNacosMultipleInstancesWithDefaultNacosRegistry(t *testing.T) { clusterName = "TheCluster" groupName = "TheGroup" ) - got, err := NewDefaultNacosRegistry(WithCluster(clusterName), WithGroup(groupName)) + got, err := NewDefaultNacosRegistry(options.WithCluster(clusterName), options.WithGroup(groupName)) assert.Nil(t, err) if !assert.NotNil(t, got) { t.Errorf("err: new registry fail") diff --git a/registry/nacos/options/options.go b/registry/nacos/options/options.go new file mode 100644 index 0000000..320a405 --- /dev/null +++ b/registry/nacos/options/options.go @@ -0,0 +1,35 @@ +/* + * Copyright 2024 CloudWeGo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package options + +type Options struct { + Cluster string + Group string +} + +// Option is nacos option. +type Option func(o *Options) + +// WithCluster with cluster option. +func WithCluster(cluster string) Option { + return func(o *Options) { o.Cluster = cluster } +} + +// WithGroup with group option. +func WithGroup(group string) Option { + return func(o *Options) { o.Group = group } +} diff --git a/registry/servicecomb/options/options.go b/registry/servicecomb/options/options.go new file mode 100644 index 0000000..467e2e8 --- /dev/null +++ b/registry/servicecomb/options/options.go @@ -0,0 +1,54 @@ +/* + * Copyright 2024 CloudWeGo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package options + +type Options struct { + AppId string + VersionRule string + HostName string + HeartbeatInterval int32 +} + +// Option is ServiceComb option. +type Option func(o *Options) + +// WithAppId with app id option +func WithAppId(appId string) Option { + return func(o *Options) { + o.AppId = appId + } +} + +// WithVersionRule with version rule option +func WithVersionRule(versionRule string) Option { + return func(o *Options) { + o.VersionRule = versionRule + } +} + +// WithHostName with host name option +func WithHostName(hostName string) Option { + return func(o *Options) { + o.HostName = hostName + } +} + +func WithHeartbeatInterval(second int32) Option { + return func(o *Options) { + o.HeartbeatInterval = second + } +} diff --git a/registry/servicecomb/servicecombhertz/registry.go b/registry/servicecomb/servicecombhertz/registry.go index 166bb60..d98e1e3 100644 --- a/registry/servicecomb/servicecombhertz/registry.go +++ b/registry/servicecomb/servicecombhertz/registry.go @@ -18,6 +18,7 @@ import ( "context" "errors" "fmt" + "github.com/cloudwego-contrib/cwgo-pkg/registry/servicecomb/options" "net" "sync" "time" @@ -38,53 +39,58 @@ type scHeartbeat struct { instanceKey string } -type registryOptions struct { - appId string - versionRule string - hostName string - heartbeatInterval int32 -} +/* + type registryOptions struct { + appId string + versionRule string + hostName string + heartbeatInterval int32 + } // RegistryOption is ServiceComb option. type RegistryOption func(o *registryOptions) // WithAppId with app id option -func WithAppId(appId string) RegistryOption { - return func(o *registryOptions) { - o.appId = appId + + func WithAppId(appId string) RegistryOption { + return func(o *registryOptions) { + o.appId = appId + } } -} // WithRegistryVersionRule with version rule option -func WithRegistryVersionRule(versionRule string) RegistryOption { - return func(o *registryOptions) { - o.versionRule = versionRule + + func WithRegistryVersionRule(versionRule string) RegistryOption { + return func(o *registryOptions) { + o.versionRule = versionRule + } } -} // WithRegistryHostName with host name option -func WithRegistryHostName(hostName string) RegistryOption { - return func(o *registryOptions) { - o.hostName = hostName + + func WithRegistryHostName(hostName string) RegistryOption { + return func(o *registryOptions) { + o.hostName = hostName + } } -} // WithRegistryHeartbeatInterval with heart beat second -func WithRegistryHeartbeatInterval(second int32) RegistryOption { - return func(o *registryOptions) { - o.heartbeatInterval = second - } -} + func WithRegistryHeartbeatInterval(second int32) RegistryOption { + return func(o *registryOptions) { + o.heartbeatInterval = second + } + } +*/ type serviceCombRegistry struct { cli *sc.Client - opts registryOptions + opts options.Options lock *sync.RWMutex registryIns map[string]*scHeartbeat } // NewDefaultSCRegistry create a new default ServiceComb registry-etcdhertz -func NewDefaultSCRegistry(endPoints []string, opts ...RegistryOption) (registry.Registry, error) { +func NewDefaultSCRegistry(endPoints []string, opts ...options.Option) (registry.Registry, error) { client, err := sc.NewClient(sc.Options{ Endpoints: endPoints, }) @@ -95,12 +101,12 @@ func NewDefaultSCRegistry(endPoints []string, opts ...RegistryOption) (registry. } // NewSCRegistry create a new ServiceComb registry-etcdhertz -func NewSCRegistry(client *sc.Client, opts ...RegistryOption) registry.Registry { - op := registryOptions{ - appId: "DEFAULT", - versionRule: "1.0.0", - hostName: "DEFAULT", - heartbeatInterval: 5, +func NewSCRegistry(client *sc.Client, opts ...options.Option) registry.Registry { + op := options.Options{ + AppId: "DEFAULT", + VersionRule: "1.0.0", + HostName: "DEFAULT", + HeartbeatInterval: 5, } for _, opt := range opts { opt(&op) @@ -133,8 +139,8 @@ func (scr *serviceCombRegistry) Register(info *registry.Info) error { serviceID, err := scr.cli.RegisterService(&discovery.MicroService{ ServiceName: info.ServiceName, - AppId: scr.opts.appId, - Version: scr.opts.versionRule, + AppId: scr.opts.AppId, + Version: scr.opts.VersionRule, Status: sc.MSInstanceUP, }) if err != nil { @@ -146,14 +152,14 @@ func (scr *serviceCombRegistry) Register(info *registry.Info) error { Interval: 30, Times: 3, } - if scr.opts.heartbeatInterval > 0 { - healthCheck.Interval = scr.opts.heartbeatInterval + if scr.opts.HeartbeatInterval > 0 { + healthCheck.Interval = scr.opts.HeartbeatInterval } instanceId, err := scr.cli.RegisterMicroServiceInstance(&discovery.MicroServiceInstance{ ServiceId: serviceID, Endpoints: []string{addr}, - HostName: scr.opts.hostName, + HostName: scr.opts.HostName, HealthCheck: healthCheck, Status: sc.MSInstanceUP, Properties: info.Tags, @@ -187,7 +193,7 @@ func (scr *serviceCombRegistry) Deregister(info *registry.Info) error { return err } - serviceId, err := scr.cli.GetMicroServiceID(scr.opts.appId, info.ServiceName, scr.opts.versionRule, "") + serviceId, err := scr.cli.GetMicroServiceID(scr.opts.AppId, info.ServiceName, scr.opts.VersionRule, "") if err != nil { return fmt.Errorf("get service-id cwerror: %w", err) } @@ -201,7 +207,7 @@ func (scr *serviceCombRegistry) Deregister(info *registry.Info) error { } instanceId := "" - instances, err := scr.cli.FindMicroServiceInstances("", scr.opts.appId, info.ServiceName, scr.opts.versionRule, sc.WithoutRevision()) + instances, err := scr.cli.FindMicroServiceInstances("", scr.opts.AppId, info.ServiceName, scr.opts.VersionRule, sc.WithoutRevision()) if err != nil { return fmt.Errorf("get instances cwerror: %w", err) } @@ -230,7 +236,7 @@ func (scr *serviceCombRegistry) Deregister(info *registry.Info) error { } func (scr *serviceCombRegistry) heartBeat(ctx context.Context, serviceId, instanceId string) { - ticker := time.NewTicker(time.Second * time.Duration(scr.opts.heartbeatInterval)) + ticker := time.NewTicker(time.Second * time.Duration(scr.opts.HeartbeatInterval)) for { select { case <-ctx.Done(): diff --git a/registry/servicecomb/servicecombkitex/registry/registry.go b/registry/servicecomb/servicecombkitex/registry/registry.go index 7f5c791..d109262 100644 --- a/registry/servicecomb/servicecombkitex/registry/registry.go +++ b/registry/servicecomb/servicecombkitex/registry/registry.go @@ -18,6 +18,7 @@ import ( "context" "errors" "fmt" + "github.com/cloudwego-contrib/cwgo-pkg/registry/servicecomb/options" "net" "sync" "time" @@ -35,7 +36,7 @@ type scHeartbeat struct { instanceKey string } -type options struct { +/*type options struct { appId string versionRule string hostName string @@ -70,17 +71,17 @@ func WithHeartbeatInterval(second int32) Option { return func(o *options) { o.heartbeatInterval = second } -} +}*/ type serviceCombRegistry struct { cli *sc.Client - opts options + opts options.Options lock *sync.RWMutex registryIns map[string]*scHeartbeat } // NewDefaultSCRegistry create a new default ServiceComb registry -func NewDefaultSCRegistry(opts ...Option) (registry.Registry, error) { +func NewDefaultSCRegistry(opts ...options.Option) (registry.Registry, error) { client, err := servicecomb.NewDefaultSCClient() if err != nil { return nil, err @@ -89,12 +90,12 @@ func NewDefaultSCRegistry(opts ...Option) (registry.Registry, error) { } // NewSCRegistry create a new ServiceComb registry -func NewSCRegistry(client *sc.Client, opts ...Option) registry.Registry { - op := options{ - appId: "DEFAULT", - versionRule: "1.0.0", - hostName: "DEFAULT", - heartbeatInterval: 5, +func NewSCRegistry(client *sc.Client, opts ...options.Option) registry.Registry { + op := options.Options{ + AppId: "DEFAULT", + VersionRule: "1.0.0", + HostName: "DEFAULT", + HeartbeatInterval: 5, } for _, opt := range opts { opt(&op) @@ -139,8 +140,8 @@ func (scr *serviceCombRegistry) Register(info *registry.Info) error { serviceID, err := scr.cli.RegisterService(&discovery.MicroService{ ServiceName: info.ServiceName, - AppId: scr.opts.appId, - Version: scr.opts.versionRule, + AppId: scr.opts.AppId, + Version: scr.opts.VersionRule, Status: sc.MSInstanceUP, }) if err != nil { @@ -152,14 +153,14 @@ func (scr *serviceCombRegistry) Register(info *registry.Info) error { Interval: 30, Times: 3, } - if scr.opts.heartbeatInterval > 0 { - healthCheck.Interval = scr.opts.heartbeatInterval + if scr.opts.HeartbeatInterval > 0 { + healthCheck.Interval = scr.opts.HeartbeatInterval } instanceId, err := scr.cli.RegisterMicroServiceInstance(&discovery.MicroServiceInstance{ ServiceId: serviceID, Endpoints: []string{host + ":" + port}, - HostName: scr.opts.hostName, + HostName: scr.opts.HostName, HealthCheck: healthCheck, Status: sc.MSInstanceUP, }) @@ -182,7 +183,7 @@ func (scr *serviceCombRegistry) Register(info *registry.Info) error { // Deregister a service or an instance func (scr *serviceCombRegistry) Deregister(info *registry.Info) error { - serviceId, err := scr.cli.GetMicroServiceID(scr.opts.appId, info.ServiceName, scr.opts.versionRule, "") + serviceId, err := scr.cli.GetMicroServiceID(scr.opts.AppId, info.ServiceName, scr.opts.VersionRule, "") if err != nil { return fmt.Errorf("get service-id cwerror: %w", err) } @@ -214,7 +215,7 @@ func (scr *serviceCombRegistry) Deregister(info *registry.Info) error { addr := host + ":" + port instanceId := "" - instances, err := scr.cli.FindMicroServiceInstances("", scr.opts.appId, info.ServiceName, scr.opts.versionRule, sc.WithoutRevision()) + instances, err := scr.cli.FindMicroServiceInstances("", scr.opts.AppId, info.ServiceName, scr.opts.VersionRule, sc.WithoutRevision()) if err != nil { return fmt.Errorf("get instances cwerror: %w", err) } @@ -240,7 +241,7 @@ func (scr *serviceCombRegistry) Deregister(info *registry.Info) error { } func (scr *serviceCombRegistry) heartBeat(ctx context.Context, serviceId, instanceId string) { - ticker := time.NewTicker(time.Second * time.Duration(scr.opts.heartbeatInterval)) + ticker := time.NewTicker(time.Second * time.Duration(scr.opts.HeartbeatInterval)) for { select { case <-ctx.Done():