2020-10-16 09:38:57 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/util/addr"
|
|
|
|
"go.unistack.org/micro/v3/util/backoff"
|
2020-10-16 09:38:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultRegisterFunc uses backoff to register service
|
2021-01-29 13:17:32 +03:00
|
|
|
DefaultRegisterFunc = func(svc *register.Service, config Options) error {
|
2020-10-16 09:38:57 +03:00
|
|
|
var err error
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
opts := []register.RegisterOption{
|
|
|
|
register.RegisterTTL(config.RegisterTTL),
|
|
|
|
register.RegisterDomain(config.Namespace),
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i <= config.RegisterAttempts; i++ {
|
2021-01-29 13:17:32 +03:00
|
|
|
err = config.Register.Register(config.Context, svc, opts...)
|
2020-10-16 09:38:57 +03:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// backoff then retry
|
|
|
|
time.Sleep(backoff.Do(i + 1))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// DefaultDeregisterFunc uses backoff to deregister service
|
2021-01-29 13:17:32 +03:00
|
|
|
DefaultDeregisterFunc = func(svc *register.Service, config Options) error {
|
2020-10-16 09:38:57 +03:00
|
|
|
var err error
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
opts := []register.DeregisterOption{
|
|
|
|
register.DeregisterDomain(config.Namespace),
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i <= config.DeregisterAttempts; i++ {
|
2021-01-29 13:17:32 +03:00
|
|
|
err = config.Register.Deregister(config.Context, svc, opts...)
|
2020-10-16 09:38:57 +03:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// backoff then retry
|
|
|
|
time.Sleep(backoff.Do(i + 1))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// NewRegisterService returns *register.Service from Server
|
|
|
|
func NewRegisterService(s Server) (*register.Service, error) {
|
2020-10-16 09:38:57 +03:00
|
|
|
opts := s.Options()
|
|
|
|
|
|
|
|
advt := opts.Address
|
|
|
|
if len(opts.Advertise) > 0 {
|
|
|
|
advt = opts.Advertise
|
|
|
|
}
|
|
|
|
|
|
|
|
host, port, err := net.SplitHostPort(advt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := addr.Extract(host)
|
|
|
|
if err != nil {
|
|
|
|
addr = host
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
node := ®ister.Node{
|
2021-04-27 08:32:47 +03:00
|
|
|
ID: opts.Name + "-" + opts.ID,
|
2020-10-16 09:38:57 +03:00
|
|
|
Address: net.JoinHostPort(addr, port),
|
|
|
|
}
|
|
|
|
node.Metadata = metadata.Copy(opts.Metadata)
|
|
|
|
|
|
|
|
node.Metadata["server"] = s.String()
|
|
|
|
node.Metadata["broker"] = opts.Broker.String()
|
2021-01-29 13:17:32 +03:00
|
|
|
node.Metadata["register"] = opts.Register.String()
|
2020-10-16 09:38:57 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
return ®ister.Service{
|
2020-10-16 09:38:57 +03:00
|
|
|
Name: opts.Name,
|
|
|
|
Version: opts.Version,
|
2021-01-29 13:17:32 +03:00
|
|
|
Nodes: []*register.Node{node},
|
2020-10-16 09:38:57 +03:00
|
|
|
Metadata: metadata.New(0),
|
|
|
|
}, nil
|
|
|
|
}
|