gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/registry/service/service.go (about)

     1  // Package service uses the registry service
     2  package service
     3  
     4  import (
     5  	"context"
     6  	"time"
     7  
     8  	"gitee.com/liuxuezhan/go-micro-v1.18.0/client"
     9  	"gitee.com/liuxuezhan/go-micro-v1.18.0/registry"
    10  	pb "gitee.com/liuxuezhan/go-micro-v1.18.0/registry/service/proto"
    11  )
    12  
    13  var (
    14  	// The default service name
    15  	DefaultService = "go.micro.service"
    16  )
    17  
    18  type serviceRegistry struct {
    19  	opts registry.Options
    20  	// name of the registry
    21  	name string
    22  	// address
    23  	address []string
    24  	// client to call registry
    25  	client pb.RegistryService
    26  }
    27  
    28  func (s *serviceRegistry) callOpts() []client.CallOption {
    29  	var opts []client.CallOption
    30  
    31  	// set registry address
    32  	if len(s.address) > 0 {
    33  		opts = append(opts, client.WithAddress(s.address...))
    34  	}
    35  
    36  	// set timeout
    37  	if s.opts.Timeout > time.Duration(0) {
    38  		opts = append(opts, client.WithRequestTimeout(s.opts.Timeout))
    39  	}
    40  
    41  	return opts
    42  }
    43  
    44  func (s *serviceRegistry) Init(opts ...registry.Option) error {
    45  	for _, o := range opts {
    46  		o(&s.opts)
    47  	}
    48  	return nil
    49  }
    50  
    51  func (s *serviceRegistry) Options() registry.Options {
    52  	return s.opts
    53  }
    54  
    55  func (s *serviceRegistry) Register(srv *registry.Service, opts ...registry.RegisterOption) error {
    56  	var options registry.RegisterOptions
    57  	for _, o := range opts {
    58  		o(&options)
    59  	}
    60  
    61  	// encode srv into protobuf and pack Register TTL into it
    62  	pbSrv := ToProto(srv)
    63  	pbSrv.Options.Ttl = int64(options.TTL.Seconds())
    64  
    65  	// register the service
    66  	_, err := s.client.Register(context.TODO(), pbSrv, s.callOpts()...)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func (s *serviceRegistry) Deregister(srv *registry.Service) error {
    75  	// deregister the service
    76  	_, err := s.client.Deregister(context.TODO(), ToProto(srv), s.callOpts()...)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	return nil
    81  }
    82  
    83  func (s *serviceRegistry) GetService(name string) ([]*registry.Service, error) {
    84  	rsp, err := s.client.GetService(context.TODO(), &pb.GetRequest{
    85  		Service: name,
    86  	}, s.callOpts()...)
    87  
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	services := make([]*registry.Service, 0, len(rsp.Services))
    93  	for _, service := range rsp.Services {
    94  		services = append(services, ToService(service))
    95  	}
    96  	return services, nil
    97  }
    98  
    99  func (s *serviceRegistry) ListServices() ([]*registry.Service, error) {
   100  	rsp, err := s.client.ListServices(context.TODO(), &pb.ListRequest{}, s.callOpts()...)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	services := make([]*registry.Service, 0, len(rsp.Services))
   106  	for _, service := range rsp.Services {
   107  		services = append(services, ToService(service))
   108  	}
   109  
   110  	return services, nil
   111  }
   112  
   113  func (s *serviceRegistry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
   114  	var options registry.WatchOptions
   115  	for _, o := range opts {
   116  		o(&options)
   117  	}
   118  
   119  	stream, err := s.client.Watch(context.TODO(), &pb.WatchRequest{
   120  		Service: options.Service,
   121  	}, s.callOpts()...)
   122  
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	return newWatcher(stream), nil
   128  }
   129  
   130  func (s *serviceRegistry) String() string {
   131  	return s.name
   132  }
   133  
   134  // NewRegistry returns a new registry service client
   135  func NewRegistry(opts ...registry.Option) registry.Registry {
   136  	var options registry.Options
   137  	for _, o := range opts {
   138  		o(&options)
   139  	}
   140  
   141  	// the registry address
   142  	addrs := options.Addrs
   143  
   144  	if len(addrs) == 0 {
   145  		addrs = []string{"127.0.0.1:8000"}
   146  	}
   147  
   148  	// use mdns as a fall back in case its used
   149  	mReg := registry.NewRegistry()
   150  
   151  	// create new client with mdns
   152  	cli := client.NewClient(
   153  		client.Registry(mReg),
   154  	)
   155  
   156  	// service name
   157  	// TODO: accept option
   158  	name := DefaultService
   159  
   160  	return &serviceRegistry{
   161  		opts:    options,
   162  		name:    name,
   163  		address: addrs,
   164  		client:  pb.NewRegistryService(name, cli),
   165  	}
   166  }