feat: registry.
This commit is contained in:
@@ -1,31 +1,245 @@
|
||||
package consul
|
||||
|
||||
import (
|
||||
consulKratos "github.com/go-kratos/kratos/contrib/registry/consul/v2"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
|
||||
consulClient "github.com/hashicorp/consul/api"
|
||||
|
||||
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
|
||||
"github.com/hashicorp/consul/api"
|
||||
)
|
||||
|
||||
// NewRegistry 创建一个注册发现客户端 - Consul
|
||||
func NewRegistry(c *conf.Registry) *consulKratos.Registry {
|
||||
if c == nil || c.Consul == nil {
|
||||
return nil
|
||||
}
|
||||
type Datacenter string
|
||||
|
||||
cfg := consulClient.DefaultConfig()
|
||||
cfg.Address = c.Consul.GetAddress()
|
||||
cfg.Scheme = c.Consul.GetScheme()
|
||||
const (
|
||||
SingleDatacenter Datacenter = "SINGLE"
|
||||
MultiDatacenter Datacenter = "MULTI"
|
||||
)
|
||||
|
||||
var cli *consulClient.Client
|
||||
var err error
|
||||
if cli, err = consulClient.NewClient(cfg); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Client is consul client config
|
||||
type Client struct {
|
||||
dc Datacenter
|
||||
cli *api.Client
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
|
||||
reg := consulKratos.New(cli, consulKratos.WithHealthCheck(c.Consul.GetHealthCheck()))
|
||||
|
||||
return reg
|
||||
// resolve service entry endpoints
|
||||
resolver ServiceResolver
|
||||
// healthcheck time interval in seconds
|
||||
healthcheckInterval int
|
||||
// heartbeat enable heartbeat
|
||||
heartbeat bool
|
||||
// deregisterCriticalServiceAfter time interval in seconds
|
||||
deregisterCriticalServiceAfter int
|
||||
// serviceChecks user custom checks
|
||||
serviceChecks api.AgentServiceChecks
|
||||
}
|
||||
|
||||
func defaultResolver(_ context.Context, entries []*api.ServiceEntry) []*registry.ServiceInstance {
|
||||
services := make([]*registry.ServiceInstance, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
var version string
|
||||
for _, tag := range entry.Service.Tags {
|
||||
ss := strings.SplitN(tag, "=", 2)
|
||||
if len(ss) == 2 && ss[0] == "version" {
|
||||
version = ss[1]
|
||||
}
|
||||
}
|
||||
endpoints := make([]string, 0)
|
||||
for scheme, addr := range entry.Service.TaggedAddresses {
|
||||
if scheme == "lan_ipv4" || scheme == "wan_ipv4" || scheme == "lan_ipv6" || scheme == "wan_ipv6" {
|
||||
continue
|
||||
}
|
||||
endpoints = append(endpoints, addr.Address)
|
||||
}
|
||||
if len(endpoints) == 0 && entry.Service.Address != "" && entry.Service.Port != 0 {
|
||||
endpoints = append(endpoints, fmt.Sprintf("http://%s:%d", entry.Service.Address, entry.Service.Port))
|
||||
}
|
||||
services = append(services, ®istry.ServiceInstance{
|
||||
ID: entry.Service.ID,
|
||||
Name: entry.Service.Service,
|
||||
Metadata: entry.Service.Meta,
|
||||
Version: version,
|
||||
Endpoints: endpoints,
|
||||
})
|
||||
}
|
||||
|
||||
return services
|
||||
}
|
||||
|
||||
// ServiceResolver is used to resolve service endpoints
|
||||
type ServiceResolver func(ctx context.Context, entries []*api.ServiceEntry) []*registry.ServiceInstance
|
||||
|
||||
// Service get services from consul
|
||||
func (c *Client) Service(ctx context.Context, service string, index uint64, passingOnly bool) ([]*registry.ServiceInstance, uint64, error) {
|
||||
if c.dc == MultiDatacenter {
|
||||
return c.multiDCService(ctx, service, index, passingOnly)
|
||||
}
|
||||
|
||||
opts := &api.QueryOptions{
|
||||
WaitIndex: index,
|
||||
WaitTime: time.Second * 55,
|
||||
Datacenter: string(c.dc),
|
||||
}
|
||||
opts = opts.WithContext(ctx)
|
||||
|
||||
if c.dc == SingleDatacenter {
|
||||
opts.Datacenter = ""
|
||||
}
|
||||
|
||||
entries, meta, err := c.singleDCEntries(service, "", passingOnly, opts)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return c.resolver(ctx, entries), meta.LastIndex, nil
|
||||
}
|
||||
|
||||
func (c *Client) multiDCService(ctx context.Context, service string, index uint64, passingOnly bool) ([]*registry.ServiceInstance, uint64, error) {
|
||||
opts := &api.QueryOptions{
|
||||
WaitIndex: index,
|
||||
WaitTime: time.Second * 55,
|
||||
}
|
||||
opts = opts.WithContext(ctx)
|
||||
|
||||
var instances []*registry.ServiceInstance
|
||||
|
||||
dcs, err := c.cli.Catalog().Datacenters()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
for _, dc := range dcs {
|
||||
opts.Datacenter = dc
|
||||
e, m, err := c.singleDCEntries(service, "", passingOnly, opts)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
ins := c.resolver(ctx, e)
|
||||
for _, in := range ins {
|
||||
if in.Metadata == nil {
|
||||
in.Metadata = make(map[string]string, 1)
|
||||
}
|
||||
in.Metadata["dc"] = dc
|
||||
}
|
||||
|
||||
instances = append(instances, ins...)
|
||||
opts.WaitIndex = m.LastIndex
|
||||
}
|
||||
|
||||
return instances, opts.WaitIndex, nil
|
||||
}
|
||||
|
||||
func (c *Client) singleDCEntries(service, tag string, passingOnly bool, opts *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) {
|
||||
return c.cli.Health().Service(service, tag, passingOnly, opts)
|
||||
}
|
||||
|
||||
// Register register service instance to consul
|
||||
func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enableHealthCheck bool) error {
|
||||
addresses := make(map[string]api.ServiceAddress, len(svc.Endpoints))
|
||||
checkAddresses := make([]string, 0, len(svc.Endpoints))
|
||||
for _, endpoint := range svc.Endpoints {
|
||||
raw, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
addr := raw.Hostname()
|
||||
port, _ := strconv.ParseUint(raw.Port(), 10, 16)
|
||||
|
||||
checkAddresses = append(checkAddresses, net.JoinHostPort(addr, strconv.FormatUint(port, 10)))
|
||||
addresses[raw.Scheme] = api.ServiceAddress{Address: endpoint, Port: int(port)}
|
||||
}
|
||||
asr := &api.AgentServiceRegistration{
|
||||
ID: svc.ID,
|
||||
Name: svc.Name,
|
||||
Meta: svc.Metadata,
|
||||
Tags: []string{fmt.Sprintf("version=%s", svc.Version)},
|
||||
TaggedAddresses: addresses,
|
||||
}
|
||||
if len(checkAddresses) > 0 {
|
||||
host, portRaw, _ := net.SplitHostPort(checkAddresses[0])
|
||||
port, _ := strconv.ParseInt(portRaw, 10, 32)
|
||||
asr.Address = host
|
||||
asr.Port = int(port)
|
||||
}
|
||||
if enableHealthCheck {
|
||||
for _, address := range checkAddresses {
|
||||
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{
|
||||
TCP: address,
|
||||
Interval: fmt.Sprintf("%ds", c.healthcheckInterval),
|
||||
DeregisterCriticalServiceAfter: fmt.Sprintf("%ds", c.deregisterCriticalServiceAfter),
|
||||
Timeout: "5s",
|
||||
})
|
||||
}
|
||||
// custom checks
|
||||
asr.Checks = append(asr.Checks, c.serviceChecks...)
|
||||
}
|
||||
if c.heartbeat {
|
||||
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{
|
||||
CheckID: "service:" + svc.ID,
|
||||
TTL: fmt.Sprintf("%ds", c.healthcheckInterval*2),
|
||||
DeregisterCriticalServiceAfter: fmt.Sprintf("%ds", c.deregisterCriticalServiceAfter),
|
||||
})
|
||||
}
|
||||
|
||||
err := c.cli.Agent().ServiceRegister(asr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c.heartbeat {
|
||||
go func() {
|
||||
time.Sleep(time.Second)
|
||||
err = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass")
|
||||
if err != nil {
|
||||
log.Errorf("[Consul]update ttl heartbeat to consul failed!err:=%v", err)
|
||||
}
|
||||
ticker := time.NewTicker(time.Second * time.Duration(c.healthcheckInterval))
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-c.ctx.Done():
|
||||
_ = c.cli.Agent().ServiceDeregister(svc.ID)
|
||||
return
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-c.ctx.Done():
|
||||
_ = c.cli.Agent().ServiceDeregister(svc.ID)
|
||||
return
|
||||
case <-ticker.C:
|
||||
// ensure that unregistered services will not be re-registered by mistake
|
||||
if errors.Is(c.ctx.Err(), context.Canceled) || errors.Is(c.ctx.Err(), context.DeadlineExceeded) {
|
||||
_ = c.cli.Agent().ServiceDeregister(svc.ID)
|
||||
return
|
||||
}
|
||||
err = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass")
|
||||
if err != nil {
|
||||
log.Errorf("[Consul] update ttl heartbeat to consul failed! err=%v", err)
|
||||
// when the previous report fails, try to re register the service
|
||||
time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
|
||||
if err := c.cli.Agent().ServiceRegister(asr); err != nil {
|
||||
log.Errorf("[Consul] re registry service failed!, err=%v", err)
|
||||
} else {
|
||||
log.Warn("[Consul] re registry of service occurred success")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Deregister service by service ID
|
||||
func (c *Client) Deregister(_ context.Context, serviceID string) error {
|
||||
defer c.cancel()
|
||||
return c.cli.Agent().ServiceDeregister(serviceID)
|
||||
}
|
||||
|
||||
34
registry/consul/client_creator.go
Normal file
34
registry/consul/client_creator.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package consul
|
||||
|
||||
import (
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
consulClient "github.com/hashicorp/consul/api"
|
||||
|
||||
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 注册 Consul 注册发现客户端
|
||||
//registry.RegisterDiscoveryCreator(registry.Consul, NewRegistry)
|
||||
}
|
||||
|
||||
// NewRegistry 创建一个注册发现客户端 - Consul
|
||||
func NewRegistry(c *conf.Registry) *Registry {
|
||||
if c == nil || c.Consul == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg := consulClient.DefaultConfig()
|
||||
cfg.Address = c.Consul.GetAddress()
|
||||
cfg.Scheme = c.Consul.GetScheme()
|
||||
|
||||
var cli *consulClient.Client
|
||||
var err error
|
||||
if cli, err = consulClient.NewClient(cfg); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
reg := New(cli, WithHealthCheck(c.Consul.GetHealthCheck()))
|
||||
|
||||
return reg
|
||||
}
|
||||
48
registry/consul/go.mod
Normal file
48
registry/consul/go.mod
Normal file
@@ -0,0 +1,48 @@
|
||||
module github.com/tx7do/kratos-bootstrap/registry/consul
|
||||
|
||||
go 1.24.0
|
||||
|
||||
toolchain go1.24.3
|
||||
|
||||
replace (
|
||||
github.com/armon/go-metrics => github.com/hashicorp/go-metrics v0.4.1
|
||||
|
||||
github.com/tx7do/kratos-bootstrap/api => ../../api
|
||||
github.com/tx7do/kratos-bootstrap/registry => ../registry
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-kratos/kratos/v2 v2.8.4
|
||||
github.com/hashicorp/consul/api v1.32.1
|
||||
github.com/stretchr/testify v1.10.0
|
||||
github.com/tx7do/kratos-bootstrap/api v0.0.20
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/armon/go-metrics v0.5.4 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/fatih/color v1.18.0 // indirect
|
||||
github.com/google/btree v1.1.3 // indirect
|
||||
github.com/google/go-cmp v0.7.0 // indirect
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
|
||||
github.com/hashicorp/go-hclog v1.6.3 // indirect
|
||||
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
|
||||
github.com/hashicorp/go-metrics v0.5.4 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
|
||||
github.com/hashicorp/golang-lru v1.0.2 // indirect
|
||||
github.com/hashicorp/serf v0.10.2 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/rogpeppe/go-internal v1.13.1 // indirect
|
||||
golang.org/x/exp v0.0.0-20250531010427-b6e5de432a8b // indirect
|
||||
golang.org/x/net v0.40.0 // indirect
|
||||
golang.org/x/sys v0.33.0 // indirect
|
||||
google.golang.org/protobuf v1.36.6 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
244
registry/consul/go.sum
Normal file
244
registry/consul/go.sum
Normal file
@@ -0,0 +1,244 @@
|
||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
|
||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
|
||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
||||
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
|
||||
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
||||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
||||
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
|
||||
github.com/go-kratos/kratos/v2 v2.8.4 h1:eIJLE9Qq9WSoKx+Buy2uPyrahtF/lPh+Xf4MTpxhmjs=
|
||||
github.com/go-kratos/kratos/v2 v2.8.4/go.mod h1:mq62W2101a5uYyRxe+7IdWubu7gZCGYqSNKwGFiiRcw=
|
||||
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
|
||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||
github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg=
|
||||
github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/hashicorp/consul/api v1.32.1 h1:0+osr/3t/aZNAdJX558crU3PEjVrG4x6715aZHRgceE=
|
||||
github.com/hashicorp/consul/api v1.32.1/go.mod h1:mXUWLnxftwTmDv4W3lzxYCPD199iNLLUyLfLGFJbtl4=
|
||||
github.com/hashicorp/consul/sdk v0.16.1 h1:V8TxTnImoPD5cj0U9Spl0TUxcytjcbbJeADFF07KdHg=
|
||||
github.com/hashicorp/consul/sdk v0.16.1/go.mod h1:fSXvwxB2hmh1FMZCNl6PwX0Q/1wdWtHJcZ7Ea5tns0s=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
|
||||
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
|
||||
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
|
||||
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
|
||||
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc=
|
||||
github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
|
||||
github.com/hashicorp/go-metrics v0.4.1 h1:3CTMzft9hdMnx2CKrEPjJqwdMAN/ij7bjqzNo+JpVZ4=
|
||||
github.com/hashicorp/go-metrics v0.4.1/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+3JqfkOG4=
|
||||
github.com/hashicorp/go-metrics v0.5.4 h1:8mmPiIJkTPPEbAiV97IxdAGNdRdaWwVap1BU6elejKY=
|
||||
github.com/hashicorp/go-metrics v0.5.4/go.mod h1:CG5yz4NZ/AI/aQt9Ucm/vdBnbh7fvmv4lxZ350i+QQI=
|
||||
github.com/hashicorp/go-msgpack v0.5.5 h1:i9R9JSrqIz0QVLz3sz+i3YJdT7TTSLcfLLzJi9aZTuI=
|
||||
github.com/hashicorp/go-msgpack/v2 v2.1.2 h1:4Ee8FTp834e+ewB71RDrQ0VKpyFdrKOjvYtnQ/ltVj0=
|
||||
github.com/hashicorp/go-msgpack/v2 v2.1.2/go.mod h1:upybraOAblm4S7rx0+jeNy+CWWhzywQsSRV5033mMu4=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
|
||||
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
|
||||
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
|
||||
github.com/hashicorp/go-sockaddr v1.0.5 h1:dvk7TIXCZpmfOlM+9mlcrWmWjw/wlKT+VDq2wMvfPJU=
|
||||
github.com/hashicorp/go-sockaddr v1.0.5/go.mod h1:uoUUmtwU7n9Dv3O4SNLeFvg0SxQ3lyjsj6+CCykpaxI=
|
||||
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
|
||||
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
|
||||
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
|
||||
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
|
||||
github.com/hashicorp/memberlist v0.5.2 h1:rJoNPWZ0juJBgqn48gjy59K5H4rNgvUoM1kUD7bXiuI=
|
||||
github.com/hashicorp/memberlist v0.5.2/go.mod h1:Ri9p/tRShbjYnpNf4FFPXG7wxEGY4Nrcn6E7jrVa//4=
|
||||
github.com/hashicorp/serf v0.10.2 h1:m5IORhuNSjaxeljg5DeQVDlQyVkhRIjJDimbkCa8aAc=
|
||||
github.com/hashicorp/serf v0.10.2/go.mod h1:T1CmSGfSeGfnfNy/w0odXQUR1rfECGd2Qdsp84DjOiY=
|
||||
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
|
||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/miekg/dns v1.1.56 h1:5imZaSeoRNvpM9SzWNhEcP9QliKiz20/dA2QabIGVnE=
|
||||
github.com/miekg/dns v1.1.56/go.mod h1:cRm6Oo2C8TY9ZS/TqsSrseAcncm74lfK5G+ikN2SWWY=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
|
||||
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
|
||||
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
|
||||
github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
|
||||
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
|
||||
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
|
||||
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
|
||||
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
|
||||
github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4=
|
||||
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
|
||||
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
|
||||
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
|
||||
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
|
||||
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
|
||||
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
|
||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I=
|
||||
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/exp v0.0.0-20250531010427-b6e5de432a8b h1:QoALfVG9rhQ/M7vYDScfPdWjGL9dlsVVM5VGh7aKoAA=
|
||||
golang.org/x/exp v0.0.0-20250531010427-b6e5de432a8b/go.mod h1:U6Lno4MTRCDY+Ba7aCcauB9T60gsv5s4ralQzP72ZoQ=
|
||||
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
|
||||
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
|
||||
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
|
||||
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
|
||||
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
|
||||
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
|
||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
75
registry/consul/options.go
Normal file
75
registry/consul/options.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package consul
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/consul/api"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Option is consul registry option.
|
||||
type Option func(*Registry)
|
||||
|
||||
// WithHealthCheck with registry health check option.
|
||||
func WithHealthCheck(enable bool) Option {
|
||||
return func(o *Registry) {
|
||||
o.enableHealthCheck = enable
|
||||
}
|
||||
}
|
||||
|
||||
// WithTimeout with get services timeout option.
|
||||
func WithTimeout(timeout time.Duration) Option {
|
||||
return func(o *Registry) {
|
||||
o.timeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
// WithDatacenter with registry datacenter option
|
||||
func WithDatacenter(dc Datacenter) Option {
|
||||
return func(o *Registry) {
|
||||
o.cli.dc = dc
|
||||
}
|
||||
}
|
||||
|
||||
// WithHeartbeat enable or disable heartbeat
|
||||
func WithHeartbeat(enable bool) Option {
|
||||
return func(o *Registry) {
|
||||
if o.cli != nil {
|
||||
o.cli.heartbeat = enable
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithServiceResolver with endpoint function option.
|
||||
func WithServiceResolver(fn ServiceResolver) Option {
|
||||
return func(o *Registry) {
|
||||
if o.cli != nil {
|
||||
o.cli.resolver = fn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithHealthCheckInterval with healthcheck interval in seconds.
|
||||
func WithHealthCheckInterval(interval int) Option {
|
||||
return func(o *Registry) {
|
||||
if o.cli != nil {
|
||||
o.cli.healthcheckInterval = interval
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithDeregisterCriticalServiceAfter with deregister-critical-service-after in seconds.
|
||||
func WithDeregisterCriticalServiceAfter(interval int) Option {
|
||||
return func(o *Registry) {
|
||||
if o.cli != nil {
|
||||
o.cli.deregisterCriticalServiceAfter = interval
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithServiceCheck with service checks
|
||||
func WithServiceCheck(checks ...*api.AgentServiceCheck) Option {
|
||||
return func(o *Registry) {
|
||||
if o.cli != nil {
|
||||
o.cli.serviceChecks = checks
|
||||
}
|
||||
}
|
||||
}
|
||||
189
registry/consul/registry.go
Normal file
189
registry/consul/registry.go
Normal file
@@ -0,0 +1,189 @@
|
||||
package consul
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/consul/api"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
)
|
||||
|
||||
var (
|
||||
_ registry.Registrar = (*Registry)(nil)
|
||||
_ registry.Discovery = (*Registry)(nil)
|
||||
)
|
||||
|
||||
// Config is consul registry config
|
||||
type Config struct {
|
||||
*api.Config
|
||||
}
|
||||
|
||||
// Registry is consul registry
|
||||
type Registry struct {
|
||||
cli *Client
|
||||
enableHealthCheck bool
|
||||
registry map[string]*serviceSet
|
||||
lock sync.RWMutex
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
// New creates consul registry
|
||||
func New(apiClient *api.Client, opts ...Option) *Registry {
|
||||
r := &Registry{
|
||||
registry: make(map[string]*serviceSet),
|
||||
enableHealthCheck: true,
|
||||
timeout: 10 * time.Second,
|
||||
cli: &Client{
|
||||
dc: SingleDatacenter,
|
||||
cli: apiClient,
|
||||
resolver: defaultResolver,
|
||||
healthcheckInterval: 10,
|
||||
heartbeat: true,
|
||||
deregisterCriticalServiceAfter: 600,
|
||||
},
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(r)
|
||||
}
|
||||
r.cli.ctx, r.cli.cancel = context.WithCancel(context.Background())
|
||||
return r
|
||||
}
|
||||
|
||||
// Register register service
|
||||
func (r *Registry) Register(ctx context.Context, svc *registry.ServiceInstance) error {
|
||||
return r.cli.Register(ctx, svc, r.enableHealthCheck)
|
||||
}
|
||||
|
||||
// Deregister deregister service
|
||||
func (r *Registry) Deregister(ctx context.Context, svc *registry.ServiceInstance) error {
|
||||
return r.cli.Deregister(ctx, svc.ID)
|
||||
}
|
||||
|
||||
// GetService return service by name
|
||||
func (r *Registry) GetService(ctx context.Context, name string) ([]*registry.ServiceInstance, error) {
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
set := r.registry[name]
|
||||
|
||||
getRemote := func() []*registry.ServiceInstance {
|
||||
services, _, err := r.cli.Service(ctx, name, 0, true)
|
||||
if err == nil && len(services) > 0 {
|
||||
return services
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if set == nil {
|
||||
if s := getRemote(); len(s) > 0 {
|
||||
return s, nil
|
||||
}
|
||||
return nil, fmt.Errorf("service %s not resolved in registry", name)
|
||||
}
|
||||
ss, _ := set.services.Load().([]*registry.ServiceInstance)
|
||||
if ss == nil {
|
||||
if s := getRemote(); len(s) > 0 {
|
||||
return s, nil
|
||||
}
|
||||
return nil, fmt.Errorf("service %s not found in registry", name)
|
||||
}
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
// ListServices return service list.
|
||||
func (r *Registry) ListServices() (allServices map[string][]*registry.ServiceInstance, err error) {
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
allServices = make(map[string][]*registry.ServiceInstance)
|
||||
for name, set := range r.registry {
|
||||
var services []*registry.ServiceInstance
|
||||
ss, _ := set.services.Load().([]*registry.ServiceInstance)
|
||||
if ss == nil {
|
||||
continue
|
||||
}
|
||||
services = append(services, ss...)
|
||||
allServices[name] = services
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Watch resolve service by name
|
||||
func (r *Registry) Watch(ctx context.Context, name string) (registry.Watcher, error) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
set, ok := r.registry[name]
|
||||
if !ok {
|
||||
set = &serviceSet{
|
||||
watcher: make(map[*watcher]struct{}),
|
||||
services: &atomic.Value{},
|
||||
serviceName: name,
|
||||
}
|
||||
r.registry[name] = set
|
||||
}
|
||||
|
||||
// init watcher
|
||||
w := &watcher{
|
||||
event: make(chan struct{}, 1),
|
||||
}
|
||||
w.ctx, w.cancel = context.WithCancel(ctx)
|
||||
w.set = set
|
||||
set.lock.Lock()
|
||||
set.watcher[w] = struct{}{}
|
||||
set.lock.Unlock()
|
||||
ss, _ := set.services.Load().([]*registry.ServiceInstance)
|
||||
if len(ss) > 0 {
|
||||
// If the service has a value, it needs to be pushed to the watcher,
|
||||
// otherwise the initial data may be blocked forever during the watch.
|
||||
w.event <- struct{}{}
|
||||
}
|
||||
|
||||
if !ok {
|
||||
err := r.resolve(ctx, set)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func (r *Registry) resolve(ctx context.Context, ss *serviceSet) error {
|
||||
timeoutCtx, cancel := context.WithTimeout(ctx, r.timeout)
|
||||
defer cancel()
|
||||
|
||||
services, idx, err := r.cli.Service(timeoutCtx, ss.serviceName, 0, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(services) > 0 {
|
||||
ss.broadcast(services)
|
||||
}
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
timeoutCtx, cancel := context.WithTimeout(context.Background(), r.timeout)
|
||||
tmpService, tmpIdx, err := r.cli.Service(timeoutCtx, ss.serviceName, idx, true)
|
||||
cancel()
|
||||
if err != nil {
|
||||
time.Sleep(time.Second)
|
||||
continue
|
||||
}
|
||||
if len(tmpService) != 0 && tmpIdx != idx {
|
||||
services = tmpService
|
||||
ss.broadcast(services)
|
||||
}
|
||||
idx = tmpIdx
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
430
registry/consul/registry_test.go
Normal file
430
registry/consul/registry_test.go
Normal file
@@ -0,0 +1,430 @@
|
||||
package consul
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/consul/api"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
)
|
||||
|
||||
func tcpServer(lis net.Listener) {
|
||||
for {
|
||||
conn, err := lis.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println("get tcp")
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_Register(t *testing.T) {
|
||||
opts := []Option{
|
||||
WithHealthCheck(false),
|
||||
}
|
||||
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
serverName string
|
||||
server []*registry.ServiceInstance
|
||||
}
|
||||
|
||||
test := []struct {
|
||||
name string
|
||||
args args
|
||||
want []*registry.ServiceInstance
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "normal",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
serverName: "server-1",
|
||||
server: []*registry.ServiceInstance{
|
||||
{
|
||||
ID: "1",
|
||||
Name: "server-1",
|
||||
Version: "v0.0.1",
|
||||
Metadata: nil,
|
||||
Endpoints: []string{"http://127.0.0.1:8000"},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: []*registry.ServiceInstance{
|
||||
{
|
||||
ID: "1",
|
||||
Name: "server-1",
|
||||
Version: "v0.0.1",
|
||||
Metadata: nil,
|
||||
Endpoints: []string{"http://127.0.0.1:8000"},
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "registry new service replace old service",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
serverName: "server-1",
|
||||
server: []*registry.ServiceInstance{
|
||||
{
|
||||
ID: "2",
|
||||
Name: "server-1",
|
||||
Version: "v0.0.1",
|
||||
Metadata: nil,
|
||||
Endpoints: []string{"http://127.0.0.1:8000"},
|
||||
},
|
||||
{
|
||||
ID: "2",
|
||||
Name: "server-1",
|
||||
Version: "v0.0.2",
|
||||
Metadata: nil,
|
||||
Endpoints: []string{"http://127.0.0.1:8000"},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: []*registry.ServiceInstance{
|
||||
{
|
||||
ID: "2",
|
||||
Name: "server-1",
|
||||
Version: "v0.0.2",
|
||||
Metadata: nil,
|
||||
Endpoints: []string{"http://127.0.0.1:8000"},
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range test {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cli, err := api.NewClient(&api.Config{Address: "127.0.0.1:8500"})
|
||||
if err != nil {
|
||||
t.Fatalf("create consul client failed: %v", err)
|
||||
}
|
||||
|
||||
r := New(cli, opts...)
|
||||
|
||||
for _, instance := range tt.args.server {
|
||||
err = r.Register(tt.args.ctx, instance)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
watch, err := r.Watch(tt.args.ctx, tt.args.serverName)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
got, err := watch.Next()
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetService() error = %v, wantErr %v", err, tt.wantErr)
|
||||
t.Errorf("GetService() got = %v", got)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("GetService() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
|
||||
for _, instance := range tt.args.server {
|
||||
_ = r.Deregister(tt.args.ctx, instance)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_GetService(t *testing.T) {
|
||||
addr := fmt.Sprintf("%s:9091", getIntranetIP())
|
||||
lis, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
t.Errorf("listen tcp %s failed!", addr)
|
||||
t.Fail()
|
||||
}
|
||||
defer lis.Close()
|
||||
go tcpServer(lis)
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
cli, err := api.NewClient(&api.Config{Address: "127.0.0.1:8500"})
|
||||
if err != nil {
|
||||
t.Fatalf("create consul client failed: %v", err)
|
||||
}
|
||||
opts := []Option{
|
||||
WithHeartbeat(true),
|
||||
WithHealthCheck(true),
|
||||
WithHealthCheckInterval(5),
|
||||
}
|
||||
r := New(cli, opts...)
|
||||
|
||||
instance1 := ®istry.ServiceInstance{
|
||||
ID: "1",
|
||||
Name: "server-1",
|
||||
Version: "v0.0.1",
|
||||
Endpoints: []string{fmt.Sprintf("tcp://%s?isSecure=false", addr)},
|
||||
}
|
||||
|
||||
instance2 := ®istry.ServiceInstance{
|
||||
ID: "2",
|
||||
Name: "server-1",
|
||||
Version: "v0.0.1",
|
||||
Endpoints: []string{fmt.Sprintf("tcp://%s?isSecure=false", addr)},
|
||||
}
|
||||
|
||||
type fields struct {
|
||||
registry *Registry
|
||||
}
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
serviceName string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want []*registry.ServiceInstance
|
||||
wantErr bool
|
||||
preFunc func(t *testing.T)
|
||||
deferFunc func(t *testing.T)
|
||||
}{
|
||||
{
|
||||
name: "normal",
|
||||
fields: fields{r},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
serviceName: "server-1",
|
||||
},
|
||||
want: []*registry.ServiceInstance{instance1},
|
||||
wantErr: false,
|
||||
preFunc: func(t *testing.T) {
|
||||
if err := r.Register(context.Background(), instance1); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
watch, err := r.Watch(context.Background(), instance1.Name)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err = watch.Next()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
},
|
||||
deferFunc: func(t *testing.T) {
|
||||
err := r.Deregister(context.Background(), instance1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "can't get any",
|
||||
fields: fields{r},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
serviceName: "server-x",
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
preFunc: func(t *testing.T) {
|
||||
if err := r.Register(context.Background(), instance2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
watch, err := r.Watch(context.Background(), instance2.Name)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err = watch.Next()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
},
|
||||
deferFunc: func(t *testing.T) {
|
||||
err := r.Deregister(context.Background(), instance2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if test.preFunc != nil {
|
||||
test.preFunc(t)
|
||||
}
|
||||
if test.deferFunc != nil {
|
||||
defer test.deferFunc(t)
|
||||
}
|
||||
|
||||
service, err := test.fields.registry.GetService(context.Background(), test.args.serviceName)
|
||||
if (err != nil) != test.wantErr {
|
||||
t.Errorf("GetService() error = %v, wantErr %v", err, test.wantErr)
|
||||
t.Errorf("GetService() got = %v", service)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(service, test.want) {
|
||||
t.Errorf("GetService() got = %v, want %v", service, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_Watch(t *testing.T) {
|
||||
addr := fmt.Sprintf("%s:9091", getIntranetIP())
|
||||
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
cli, err := api.NewClient(&api.Config{Address: "127.0.0.1:8500", WaitTime: 2 * time.Second})
|
||||
if err != nil {
|
||||
t.Fatalf("create consul client failed: %v", err)
|
||||
}
|
||||
|
||||
instance1 := ®istry.ServiceInstance{
|
||||
ID: "1",
|
||||
Name: "server-1",
|
||||
Version: "v0.0.1",
|
||||
Endpoints: []string{fmt.Sprintf("tcp://%s?isSecure=false", addr)},
|
||||
}
|
||||
|
||||
instance2 := ®istry.ServiceInstance{
|
||||
ID: "2",
|
||||
Name: "server-1",
|
||||
Version: "v0.0.1",
|
||||
Endpoints: []string{fmt.Sprintf("tcp://%s?isSecure=false", addr)},
|
||||
}
|
||||
|
||||
instance3 := ®istry.ServiceInstance{
|
||||
ID: "3",
|
||||
Name: "server-1",
|
||||
Version: "v0.0.1",
|
||||
Endpoints: []string{fmt.Sprintf("tcp://%s?isSecure=false", addr)},
|
||||
}
|
||||
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
cancel func()
|
||||
opts []Option
|
||||
instance *registry.ServiceInstance
|
||||
}
|
||||
canceledCtx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []*registry.ServiceInstance
|
||||
wantErr bool
|
||||
preFunc func(t *testing.T)
|
||||
}{
|
||||
{
|
||||
name: "normal",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
instance: instance1,
|
||||
opts: []Option{
|
||||
WithHealthCheck(false),
|
||||
},
|
||||
},
|
||||
want: []*registry.ServiceInstance{instance1},
|
||||
wantErr: false,
|
||||
preFunc: func(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "ctx has been cancelled",
|
||||
args: args{
|
||||
ctx: canceledCtx,
|
||||
cancel: cancel,
|
||||
instance: instance2,
|
||||
opts: []Option{
|
||||
WithHealthCheck(false),
|
||||
},
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
preFunc: func(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "register with healthCheck",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
instance: instance3,
|
||||
opts: []Option{
|
||||
WithHeartbeat(true),
|
||||
WithHealthCheck(true),
|
||||
WithHealthCheckInterval(5),
|
||||
},
|
||||
},
|
||||
want: []*registry.ServiceInstance{instance3},
|
||||
wantErr: false,
|
||||
preFunc: func(t *testing.T) {
|
||||
lis, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
t.Errorf("listen tcp %s failed!", addr)
|
||||
return
|
||||
}
|
||||
go tcpServer(lis)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.preFunc != nil {
|
||||
tt.preFunc(t)
|
||||
}
|
||||
|
||||
r := New(cli, tt.args.opts...)
|
||||
|
||||
err := r.Register(tt.args.ctx, tt.args.instance)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
defer func() {
|
||||
err = r.Deregister(tt.args.ctx, tt.args.instance)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
|
||||
watch, err := r.Watch(tt.args.ctx, tt.args.instance.Name)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if tt.args.cancel != nil {
|
||||
tt.args.cancel()
|
||||
}
|
||||
|
||||
service, err := watch.Next()
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetService() error = %v, wantErr %v", err, tt.wantErr)
|
||||
t.Errorf("GetService() got = %v", service)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(service, tt.want) {
|
||||
t.Errorf("GetService() got = %v, want %v", service, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func getIntranetIP() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return "127.0.0.1"
|
||||
}
|
||||
|
||||
for _, address := range addrs {
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
return ipnet.IP.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return "127.0.0.1"
|
||||
}
|
||||
27
registry/consul/service.go
Normal file
27
registry/consul/service.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package consul
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
)
|
||||
|
||||
type serviceSet struct {
|
||||
serviceName string
|
||||
watcher map[*watcher]struct{}
|
||||
services *atomic.Value
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *serviceSet) broadcast(ss []*registry.ServiceInstance) {
|
||||
s.services.Store(ss)
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
for k := range s.watcher {
|
||||
select {
|
||||
case k.event <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
40
registry/consul/watcher.go
Normal file
40
registry/consul/watcher.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package consul
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
)
|
||||
|
||||
type watcher struct {
|
||||
event chan struct{}
|
||||
set *serviceSet
|
||||
|
||||
// for cancel
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (w *watcher) Next() (services []*registry.ServiceInstance, err error) {
|
||||
select {
|
||||
case <-w.ctx.Done():
|
||||
err = w.ctx.Err()
|
||||
return
|
||||
case <-w.event:
|
||||
}
|
||||
|
||||
ss, ok := w.set.services.Load().([]*registry.ServiceInstance)
|
||||
|
||||
if ok {
|
||||
services = append(services, ss...)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (w *watcher) Stop() error {
|
||||
w.cancel()
|
||||
w.set.lock.Lock()
|
||||
defer w.set.lock.Unlock()
|
||||
delete(w.set.watcher, w)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user