2021-01-29 15:01:10 +03:00
|
|
|
// Package service uses the register service
|
2019-11-10 22:54:23 +03:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2021-01-19 22:08:06 +03:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-03-22 09:06:35 +03:00
|
|
|
pbmicro "github.com/unistack-org/micro-register-service/v3/micro"
|
2021-01-29 15:01:10 +03:00
|
|
|
pb "github.com/unistack-org/micro-register-service/v3/proto"
|
2021-01-19 22:08:06 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
2021-01-29 15:01:10 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2019-11-10 22:54:23 +03:00
|
|
|
)
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
type serviceRegister struct {
|
|
|
|
opts register.Options
|
|
|
|
// name of the register
|
2021-01-19 22:08:06 +03:00
|
|
|
service string
|
|
|
|
// address
|
|
|
|
address []string
|
2021-01-29 15:01:10 +03:00
|
|
|
// client to call register
|
2021-03-22 09:06:35 +03:00
|
|
|
client pbmicro.RegisterClient
|
2021-01-19 22:08:06 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) callOpts() []client.CallOption {
|
2021-01-19 22:08:06 +03:00
|
|
|
var opts []client.CallOption
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
// set register address
|
2021-01-19 22:08:06 +03:00
|
|
|
if len(s.address) > 0 {
|
|
|
|
opts = append(opts, client.WithAddress(s.address...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// set timeout
|
|
|
|
if s.opts.Timeout > time.Duration(0) {
|
|
|
|
opts = append(opts, client.WithRequestTimeout(s.opts.Timeout))
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) Init(opts ...register.Option) error {
|
2021-01-19 22:08:06 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.opts.Addrs) > 0 {
|
|
|
|
s.address = s.opts.Addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
var cli client.Client
|
|
|
|
if s.opts.Context != nil {
|
|
|
|
if v, ok := s.opts.Context.Value(clientKey{}).(string); ok && v != "" {
|
|
|
|
s.service = v
|
|
|
|
}
|
|
|
|
if v, ok := s.opts.Context.Value(clientKey{}).(client.Client); ok && v != nil {
|
|
|
|
cli = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli == nil {
|
|
|
|
return fmt.Errorf("missing Client option")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.service == "" {
|
|
|
|
return fmt.Errorf("missing Service option")
|
|
|
|
}
|
|
|
|
|
2021-03-22 09:06:35 +03:00
|
|
|
s.client = pbmicro.NewRegisterClient(s.service, cli)
|
2021-01-19 22:08:06 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) Options() register.Options {
|
2021-01-19 22:08:06 +03:00
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) Connect(ctx context.Context) error {
|
2021-01-19 22:08:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) Disconnect(ctx context.Context) error {
|
2021-01-19 22:08:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) Register(ctx context.Context, srv *register.Service, opts ...register.RegisterOption) error {
|
|
|
|
options := register.NewRegisterOptions(opts...)
|
2021-01-19 22:08:06 +03:00
|
|
|
|
|
|
|
// encode srv into protobuf and pack TTL and domain into it
|
|
|
|
pbSrv := ToProto(srv)
|
|
|
|
pbSrv.Options.Ttl = int64(options.TTL.Seconds())
|
|
|
|
pbSrv.Options.Domain = options.Domain
|
|
|
|
|
|
|
|
// register the service
|
|
|
|
_, err := s.client.Register(ctx, pbSrv, s.callOpts()...)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) Deregister(ctx context.Context, srv *register.Service, opts ...register.DeregisterOption) error {
|
|
|
|
options := register.NewDeregisterOptions(opts...)
|
2021-01-19 22:08:06 +03:00
|
|
|
|
|
|
|
// encode srv into protobuf and pack domain into it
|
|
|
|
pbSrv := ToProto(srv)
|
|
|
|
pbSrv.Options.Domain = options.Domain
|
|
|
|
|
|
|
|
// deregister the service
|
|
|
|
_, err := s.client.Deregister(ctx, pbSrv, s.callOpts()...)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) LookupService(ctx context.Context, name string, opts ...register.LookupOption) ([]*register.Service, error) {
|
|
|
|
options := register.NewLookupOptions(opts...)
|
2021-01-19 22:08:06 +03:00
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
rsp, err := s.client.LookupService(ctx, &pb.LookupRequest{
|
2021-01-19 22:08:06 +03:00
|
|
|
Service: name, Options: &pb.Options{Domain: options.Domain},
|
|
|
|
}, s.callOpts()...)
|
|
|
|
|
|
|
|
if verr, ok := err.(*errors.Error); ok && verr.Code == 404 {
|
2021-01-29 15:01:10 +03:00
|
|
|
return nil, register.ErrNotFound
|
2021-01-19 22:08:06 +03:00
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
services := make([]*register.Service, 0, len(rsp.Services))
|
2021-01-19 22:08:06 +03:00
|
|
|
for _, service := range rsp.Services {
|
|
|
|
services = append(services, ToService(service))
|
|
|
|
}
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) ListServices(ctx context.Context, opts ...register.ListOption) ([]*register.Service, error) {
|
|
|
|
options := register.NewListOptions(opts...)
|
2021-01-19 22:08:06 +03:00
|
|
|
|
|
|
|
req := &pb.ListRequest{Options: &pb.Options{Domain: options.Domain}}
|
|
|
|
rsp, err := s.client.ListServices(ctx, req, s.callOpts()...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
services := make([]*register.Service, 0, len(rsp.Services))
|
2021-01-19 22:08:06 +03:00
|
|
|
for _, service := range rsp.Services {
|
|
|
|
services = append(services, ToService(service))
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) Watch(ctx context.Context, opts ...register.WatchOption) (register.Watcher, error) {
|
|
|
|
options := register.NewWatchOptions(opts...)
|
2021-01-19 22:08:06 +03:00
|
|
|
|
|
|
|
stream, err := s.client.Watch(ctx, &pb.WatchRequest{
|
|
|
|
Service: options.Service, Options: &pb.Options{Domain: options.Domain},
|
|
|
|
}, s.callOpts()...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newWatcher(stream), nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) String() string {
|
2021-01-19 22:08:06 +03:00
|
|
|
return "service"
|
2019-11-10 22:54:23 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
func (s *serviceRegister) Name() string {
|
|
|
|
return s.opts.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegister returns a new register service client
|
|
|
|
func NewRegister(opts ...register.Option) register.Register {
|
|
|
|
options := register.NewOptions(opts...)
|
2021-01-19 22:08:06 +03:00
|
|
|
|
|
|
|
addrs := options.Addrs
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
addrs = []string{"127.0.0.1:8000"}
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:01:10 +03:00
|
|
|
return &serviceRegister{
|
2021-01-19 22:08:06 +03:00
|
|
|
opts: options,
|
|
|
|
address: addrs,
|
|
|
|
}
|
2019-11-10 22:54:23 +03:00
|
|
|
}
|