type Socket interface{Recv(*Message)errorSend(*Message)errorClose()error}type Client interface{
Socket
}type Listener interface{Addr()stringClose()errorAccept(func(Socket))error}type Transport interface{Dial(addr string, opts ...DialOption)(Client,error)Listen(addr string, opts ...ListenOption)(Listener,error)String()string}
1.1.2. Codec编码接口
编解码,底层也是protobuf
type Codec interface{ReadHeader(*Message, MessageType)errorReadBody(interface{})errorWrite(*Message,interface{})errorClose()errorString()string}1.1.3. Registry注册接口
服务注册发现的实现:etcd、consul、mdns、kube-DNS、zk
type Registry interface{Register(*Service,...RegisterOption)errorDeregister(*Service)errorGetService(string)([]*Service,error)ListServices()([]*Service,error)Watch(...WatchOption)(Watcher,error)String()stringOptions() Options
}
1.1.4. Selector负载均衡
根据不同算法请求主机列表
type Selector interface{Init(opts ...Option)errorOptions() Options
// Select returns a function which should return the next nodeSelect(service string, opts ...SelectOption)(Next,error)// Mark sets the success/error against a nodeMark(service string, node *registry.Node, err error)// Reset returns state back to zero for a serviceReset(service string)// Close renders the selector unusableClose()error// Name of the selectorString()string}
1.1.5. Broker发布订阅接口
pull push watch
type Broker interface{Options() Options
Address()stringConnect()errorDisconnect()errorInit(...Option)errorPublish(string,*Message,...PublishOption)errorSubscribe(string, Handler,...SubscribeOption)(Subscriber,error)String()string}
type Server interface{Options() Options
Init(...Option)errorHandle(Handler)errorNewHandler(interface{},...HandlerOption) Handler
NewSubscriber(string,interface{},...SubscriberOption) Subscriber
Subscribe(Subscriber)errorRegister()errorDeregister()errorStart()errorStop()errorString()string}
1.1.8. Serveice接口
type Service interface{Init(...Option)Options() Options
Client() client.Client
Server() server.Server
Run()errorString()string}
1.1.1. go-micro简介 Go Micro是一个插件化的基础框架,基于此可以构建微服务,Micro的设计哲学是可插拔的插件化架构在架构之外,它默认实现了consul作为服务发现(2019年源码修改了默认使用mdns),通过http进行通信,通过protobuf和json进行编解码1.1.2. go-micro的主要功能服务发现:自动服务注册和名称解析。服务发现是微服务开发的核心。当服务A需要与服务B通话时,它需要该服务的位置。默认发现机制是多播DNS(mdns),一种零配置系统。您可以选