Skip to content

Commit

Permalink
integration config
Browse files Browse the repository at this point in the history
  • Loading branch information
smx-Morgan committed Sep 12, 2024
1 parent 7ea5238 commit 09b99e7
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 157 deletions.
3 changes: 2 additions & 1 deletion registry/consul/consulhertz/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package consulhertz
import (
"context"
"fmt"
"github.com/cloudwego-contrib/cwgo-pkg/registry/consul/options"
"log"
"net"
"testing"
Expand Down Expand Up @@ -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)
}

Expand Down
21 changes: 11 additions & 10 deletions registry/consul/consulhertz/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 14 additions & 12 deletions registry/consul/consulkitex/consul_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,9 +28,10 @@ import (

type consulRegistry struct {
consulClient *api.Client
opts options
opts options.Options
}

/*
type options struct {
check *api.AgentServiceCheck
}
Expand All @@ -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 = ":"

Expand All @@ -49,16 +51,16 @@ 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)
if err != nil {
return nil, err
}

op := options{
check: internal.DefaultCheck(),
op := options.Options{
Check: internal.DefaultCheck(),
}

for _, option := range opts {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion registry/consul/consulkitex/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"github.com/cloudwego-contrib/cwgo-pkg/registry/consul/options"
"log"
"net"
"strconv"
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions registry/consul/options/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
7 changes: 4 additions & 3 deletions registry/nacos/nacoshertz/nacos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package nacos

import (
"context"
"github.com/cloudwego-contrib/cwgo-pkg/registry/nacos/options"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -377,7 +378,7 @@ func TestResolverDifferentGroup(t *testing.T) {
ctx.String(200, "pong1")
})

opts2 = append(opts2, server.WithRegistry(NewNacosRegistry(namingClient, WithRegistryGroup("OTHER")), &registry.Info{
opts2 = append(opts2, server.WithRegistry(NewNacosRegistry(namingClient, options.WithGroup("OTHER")), &registry.Info{
ServiceName: "demo.etcdhertz-contrib.test1",
Addr: utils.NewNetAddr("tcp", "127.0.0.1:7001"),
Weight: 10,
Expand Down
49 changes: 13 additions & 36 deletions registry/nacos/nacoshertz/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package nacos

import (
"fmt"
"github.com/cloudwego-contrib/cwgo-pkg/registry/nacos/options"
"net"
"strconv"

Expand All @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand Down
39 changes: 11 additions & 28 deletions registry/nacos/nacoskitex/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package registry
import (
"errors"
"fmt"
"github.com/cloudwego-contrib/cwgo-pkg/registry/nacos/options"
"net"
"strconv"

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 09b99e7

Please sign in to comment.