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

     1  // Package micro is a pluggable framework for microservices
     2  package micro
     3  
     4  import (
     5  	"context"
     6  
     7  	"gitee.com/liuxuezhan/go-micro-v1.18.0/client"
     8  	"gitee.com/liuxuezhan/go-micro-v1.18.0/server"
     9  )
    10  
    11  type serviceKey struct{}
    12  
    13  // Service is an interface that wraps the lower level libraries
    14  // within go-micro. Its a convenience method for building
    15  // and initialising services.
    16  type Service interface {
    17  	// The service name
    18  	Name() string
    19  	// Init initialises options
    20  	Init(...Option)
    21  	// Options returns the current options
    22  	Options() Options
    23  	// Client is used to call services
    24  	Client() client.Client
    25  	// Server is for handling requests and events
    26  	Server() server.Server
    27  	// Run the service
    28  	Run() error
    29  	// The service implementation
    30  	String() string
    31  }
    32  
    33  // Function is a one time executing Service
    34  type Function interface {
    35  	// Inherits Service interface
    36  	Service
    37  	// Done signals to complete execution
    38  	Done() error
    39  	// Handle registers an RPC handler
    40  	Handle(v interface{}) error
    41  	// Subscribe registers a subscriber
    42  	Subscribe(topic string, v interface{}) error
    43  }
    44  
    45  // Publisher is syntactic sugar for publishing
    46  type Publisher interface {
    47  	Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error
    48  }
    49  
    50  type Option func(*Options)
    51  
    52  var (
    53  	HeaderPrefix = "Micro-"
    54  )
    55  
    56  // NewService creates and returns a new Service based on the packages within.
    57  func NewService(opts ...Option) Service {
    58  	return newService(opts...)
    59  }
    60  
    61  // FromContext retrieves a Service from the Context.
    62  func FromContext(ctx context.Context) (Service, bool) {
    63  	s, ok := ctx.Value(serviceKey{}).(Service)
    64  	return s, ok
    65  }
    66  
    67  // NewContext returns a new Context with the Service embedded within it.
    68  func NewContext(ctx context.Context, s Service) context.Context {
    69  	return context.WithValue(ctx, serviceKey{}, s)
    70  }
    71  
    72  // NewFunction returns a new Function for a one time executing Service
    73  func NewFunction(opts ...Option) Function {
    74  	return newFunction(opts...)
    75  }
    76  
    77  // NewPublisher returns a new Publisher
    78  func NewPublisher(topic string, c client.Client) Publisher {
    79  	if c == nil {
    80  		c = client.NewClient()
    81  	}
    82  	return &publisher{c, topic}
    83  }
    84  
    85  // RegisterHandler is syntactic sugar for registering a handler
    86  func RegisterHandler(s server.Server, h interface{}, opts ...server.HandlerOption) error {
    87  	return s.Handle(s.NewHandler(h, opts...))
    88  }
    89  
    90  // RegisterSubscriber is syntactic sugar for registering a subscriber
    91  func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...server.SubscriberOption) error {
    92  	return s.Subscribe(s.NewSubscriber(topic, h, opts...))
    93  }