1. Go Micro接口详解

1.1.1. Transort通信接口

通信相关接口

  1. type Socket interface {
  2. Recv(*Message) error
  3. Send(*Message) error
  4. Close() error
  5. }
  6. type Client interface {
  7. Socket
  8. }
  9. type Listener interface {
  10. Addr() string
  11. Close() error
  12. Accept(func(Socket)) error
  13. }
  14. type Transport interface {
  15. Dial(addr string, opts ...DialOption) (Client, error)
  16. Listen(addr string, opts ...ListenOption) (Listener, error)
  17. String() string
  18. }

1.1.2. Codec编码接口

编解码,底层也是protobuf

  1. type Codec interface {
  2. ReadHeader(*Message, MessageType) error
  3. ReadBody(interface{}) error
  4. Write(*Message, interface{}) error
  5. Close() error
  6. String() string
  7. }

1.1.3. Registry注册接口

服务注册发现的实现:etcd、consul、mdns、kube-DNS、zk

  1. type Registry interface {
  2. Register(*Service, ...RegisterOption) error
  3. Deregister(*Service) error
  4. GetService(string) ([]*Service, error)
  5. ListServices() ([]*Service, error)
  6. Watch(...WatchOption) (Watcher, error)
  7. String() string
  8. Options() Options
  9. }

1.1.4. Selector负载均衡

根据不同算法请求主机列表

  1. type Selector interface {
  2. Init(opts ...Option) error
  3. Options() Options
  4. // Select returns a function which should return the next node
  5. Select(service string, opts ...SelectOption) (Next, error)
  6. // Mark sets the success/error against a node
  7. Mark(service string, node *registry.Node, err error)
  8. // Reset returns state back to zero for a service
  9. Reset(service string)
  10. // Close renders the selector unusable
  11. Close() error
  12. // Name of the selector
  13. String() string
  14. }

1.1.5. Broker发布订阅接口

pull push watch

  1. type Broker interface {
  2. Options() Options
  3. Address() string
  4. Connect() error
  5. Disconnect() error
  6. Init(...Option) error
  7. Publish(string, *Message, ...PublishOption) error
  8. Subscribe(string, Handler, ...SubscribeOption) (Subscriber, error)
  9. String() string
  10. }

1.1.6. Client客户端接口

  1. type Client interface {
  2. Init(...Option) error
  3. Options() Options
  4. NewMessage(topic string, msg interface{}, opts ...MessageOption) Message
  5. NewRequest(service, method string, req interface{}, reqOpts ...RequestOption) Request
  6. Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error
  7. Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error)
  8. Publish(ctx context.Context, msg Message, opts ...PublishOption) error
  9. String() string
  10. }

1.1.7. Server服务端接口

  1. type Server interface {
  2. Options() Options
  3. Init(...Option) error
  4. Handle(Handler) error
  5. NewHandler(interface{}, ...HandlerOption) Handler
  6. NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
  7. Subscribe(Subscriber) error
  8. Register() error
  9. Deregister() error
  10. Start() error
  11. Stop() error
  12. String() string
  13. }

1.1.8. Serveice接口

  1. type Service interface {
  2. Init(...Option)
  3. Options() Options
  4. Client() client.Client
  5. Server() server.Server
  6. Run() error
  7. String() string
  8. }