Go-micro框架是一套微服務分佈式的框架,能夠大幅度的提升開發效率。
源碼地址:https://github.com/micro/go-micro
Go-micro擁有不少特性:node
其中最值得一提的是最後一個特性,接口可插拔。只要實現上圖的8個關鍵interface,就能夠隨意的根據需求從新時間這8個接口的功能。 這8個接口一實現了go-micro的總體架構。
這些接口都有默認的實現方式,意味着你不須要寫任何的插件就可使用這個微服務架構。
另外還有一個圖也能夠很清楚的看清整個Micro的總體流程:
能夠看到在整個框架中各個模塊的做用和相互之間的調用關係。
在服務發現的基礎上,經過broker或者是Transport來進行服務之間的通訊。簡單來講就是Client經過Selector模塊來進行負載均衡,從註冊中心拿到服務節點而後經過節點信息經過Transport定義的通訊協議來進行通訊。git
整個Go Micro 都是有這8個interface構成的,換而言之只要理解了這8個接口,並仔細研究其中一個實現基本就能瞭解整個框架的實現和架構。下面先來看看這8個接口github
服務之間通訊的接口。也就是服務發送和接收的最終實現方式,是由這些接口定製的。redis
type Socket interface { Recv(*Message) error Send(*Message) error Close() error } type Client interface { Socket } type Listener interface { Addr() string Close() error Accept(func(Socket)) error } type Transport interface { Dial(addr string, opts ...DialOption) (Client, error) Listen(addr string, opts ...ListenOption) (Listener, error) String() string }
有了傳輸方式,下面要解決的就是傳輸編碼和解碼問題,go-micro有不少種編碼解碼方式,默認的實現方式是protobuf,固然也有其餘的實現方式,json、protobuf、jsonrpc、mercury等等。算法
源碼json
type Codec interface { ReadHeader(*Message, MessageType) error ReadBody(interface{}) error Write(*Message, interface{}) error Close() error String() string } type Message struct { Id uint64 Type MessageType Target string Method string Error string Header map[string]string }
Codec接口的Write方法就是編碼過程,兩個Read是解碼過程。緩存
服務的註冊和發現,目前實現的consul,mdns, etcd,etcdv3,zookeeper,kubernetes.等等,架構
type Registry interface { Register(*Service, ...RegisterOption) error Deregister(*Service) error GetService(string) ([]*Service, error) ListServices() ([]*Service, error) Watch(...WatchOption) (Watcher, error) String() string Options() Options }
以Registry爲基礎,Selector 是客戶端級別的負載均衡,當有客戶端向服務發送請求時, selector根據不一樣的算法從Registery中的主機列表,獲得可用的Service節點,進行通訊。目前實現的有循環算法和隨機算法,默認的是隨機算法。負載均衡
type Selector interface { Init(opts ...Option) error Options() Options // Select returns a function which should return the next node Select(service string, opts ...SelectOption) (Next, error) // Mark sets the success/error against a node Mark(service string, node *registry.Node, err error) // Reset returns state back to zero for a service Reset(service string) // Close renders the selector unusable Close() error // Name of the selector String() string }
默認的是實現是本地緩存,當前實現的有blacklist,label,named等方式。框架
Broker是消息發佈和訂閱的接口。很簡單的一個例子,由於服務的節點是不固定的,若是有須要修改全部服務行爲的需求,可使服務訂閱某個主題,當有信息發佈時,全部的監聽服務都會收到信息,根據你的須要作相應的行爲。
type Broker interface { Options() Options Address() string Connect() error Disconnect() error Init(...Option) error Publish(string, *Message, ...PublishOption) error Subscribe(string, Handler, ...SubscribeOption) (Subscriber, error) String() string }
Broker默認的實現方式是http方式,可是這種方式不要在生產環境用。go-plugins裏有不少成熟的消息隊列實現方式,有kafka、nsq、rabbitmq、redis,等等。
Client是請求服務的接口,他封裝Transport和Codec進行rpc調用,也封裝了Brocker進行信息的發佈。
type Client interface { Init(...Option) error Options() Options NewMessage(topic string, msg interface{}, opts ...MessageOption) Message NewRequest(service, method string, req interface{}, reqOpts ...RequestOption) Request Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error) Publish(ctx context.Context, msg Message, opts ...PublishOption) error String() string }
固然他也支持雙工通訊 Stream 這些具體的實現方式和使用方式,之後會詳細解說。
默認的是rpc實現方式,他還有grpc和http方式,在go-plugins裏能夠找到
Server看名字你們也知道是作什麼的了。監聽等待rpc請求。監聽broker的訂閱信息,等待信息隊列的推送等。
type Server interface { Options() Options Init(...Option) error Handle(Handler) error NewHandler(interface{}, ...HandlerOption) Handler NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber Subscribe(Subscriber) error Register() error Deregister() error Start() error Stop() error String() string }
Service是Client和Server的封裝,他包含了一系列的方法使用初始值去初始化Service和Client,使咱們能夠很簡單的建立一個rpc服務。
type Service interface { Init(...Option) Options() Options Client() client.Client Server() server.Server Run() error String() string }