Go Micro 總體設計

Go-micro 是什麼

Go-micro框架是一套微服務分佈式的框架,可以大幅度的提高開發效率。
圖片描述
源碼地址:https://github.com/micro/go-micro
Go-micro擁有很多特性:

  • 服務註冊、發現
  • 負載均衡
  • 消息解碼,並默認支持json以及protobuf
  • 基於rpc的請求響應
  • 異步的消息通訊
  • 接口可插拔

其中最值得一提的是最後一個特性,接口可插拔。只要實現上圖的8個關鍵interface,就可以隨意的根據需求重新時間這8個接口的功能。 這8個接口一實現了go-micro的整體架構。
這些接口都有默認的實現方式,意味着你不需要寫任何的插件就可以使用這個微服務架構。

主要interface

整個Go Micro 都是有這8個interface構成的,換而言之只要理解了這8個接口,並仔細研究其中一個實現基本就能瞭解整個框架的實現和架構。下面先來看看這8個接口

Transort

服務之間通信的接口。也就是服務發送和接收的最終實現方式,是由這些接口定製的。

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
}

Codec

有了傳輸方式,下面要解決的就是傳輸編碼和解碼問題,go-micro有很多種編碼解碼方式,默認的實現方式是protobuf,當然也有其他的實現方式,json、protobuf、jsonrpc、mercury等等。

源碼


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是解碼過程。

Registry

服務的註冊和發現,目前實現的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
}

Selector

以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

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

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

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

Service是Client和Server的封裝,他包含了一系列的方法使用初始值去初始化Service和Client,使我們可以很簡單的創建一個rpc服務。

type Service interface {
    Init(...Option)
    Options() Options
    Client() client.Client
    Server() server.Server
    Run() error
    String() string
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章