Go-Micro框架入門教程(一)---框架結構


Go語言微服務系列文章,使用golang實現微服務,這里選用的是go-micro框架,本文主要是對該框架的一個架構簡單介紹。

1. 概述

go-microgo語言下的一個很好的微服務框架。

  • 1.服務間傳輸格式為protobuf,效率上沒的說,非常的快,也很安全。
  • 2.go-micro的服務注冊發現是多種多樣的。我個人比較喜歡etcdv3的服務服務發現和注冊。
  • 3.主要的功能都有相應的接口,只要實現相應的接口,就可以根據自己的需要訂制插件。

2. 通信流程

go-micro的通信流程大至如下

服務注冊與發現

Server端需要向Register注冊自己的存在或消亡,這樣Client才能知道自己的狀態。

同時Server監聽客戶端的調用Brocker推送過來的信息進行處理。

Client端從Register中得到Server的信息,然后每次調用都根據算法選擇一個的Server進行通信,當然通信是要經過編碼/解碼,選擇傳輸協議等一系列過程的。

3. 框架結構

go-micro 之所以可以高度訂制和他的框架結構是分不開的,go-micro 由8個關鍵的 interface組成,每一個interface 都可以根據自己的需求重新實現,這8個主要的inteface也構成了go-micro的框架結構。

整體架構

1.Transort

服務之間通信的接口

也就是服務發送和接收的最終實現方式,是由這些接口定制的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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))
}

Transport interface {
Dial(addr string, opts ...DialOption) (Client, error)
Listen(addr string, opts ...ListenOption) (Listener, error)
String() string
}

Transport 的Listen方法是一般是Server端進行調用的,他監聽一個端口,等待客戶端調用。

Transport 的Dial就是客戶端進行連接服務的方法。他返回一個Client接口,這個接口返回一個Client接口,這個Client嵌入了Socket接口,這個接口的方法就是具體發送和接收通信的信息。

是go-micro默認的同步通信機制是http傳輸。當然還有很多其他的插件:grpc,nats,tcp,udp,rabbitmq,都是目前已經實現了的方式。

2. Codec

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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是解碼過程。

3. Registry

服務的注冊和發現,目前實現的consul,mdns, etcd,etcdv3,zookeeper,kubernetes.等

1
2
3
4
5
6
7
8
9
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
}

簡單來說就是Service 進行Register,來進行注冊,Client 使用watch方法進行監控,當有服務加入或者刪除時這個方法會被觸發,以提醒客戶端更新Service信 大專欄  Go-Micro框架入門教程(一)---框架結構

默認的是服務注冊和發現是consul, 我個人比較喜歡etcdv3集群。大家可以根據自己的喜好選擇。

4. Selector

以Registry為基礎,Selector 是客戶端級別的負載均衡,當有客戶端向服務發送請求時, selector根據不同的算法從Registery中的主機列表,得到可用的Service節點,進行通信。目前實現的有循環算法隨機算法,默認的是隨機算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
type Selector interface {
Init(opts ...Option) error
Options() Options

Select(service string, opts ...SelectOption) (Next, error)

Mark(service string, node *registry.Node, err error)

Reset(service string)

Close() error
// Name of the selector
String() string
}

默認的是實現是本地緩存,當前實現的有blacklist,label,named等方式。

5. Broker

Broker是消息發布和訂閱的接口。很簡單的一個例子,因為服務的節點是不固定的,如果有需要修改所有服務行為的需求,可以使服務訂閱某個主題,當有信息發布時,所有的監聽服務都會收到信息,根據你的需要做相應的行為。

1
2
3
4
5
6
7
8
9
10
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等等。

6. Client

Client是請求服務的接口

他封裝 Transport 和 Codec 進行rpc調用,也封裝了Brocker進行信息的發布。

1
2
3
4
5
6
7
8
9
10
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里可以找到

7. Server

Server看名字大家也知道是做什么的了。監聽等待rpc請求。監聽broker的訂閱信息,等待信息隊列的推送等。

1
2
3
4
5
6
7
8
9
10
11
12
13
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
}

默認的是rpc實現方式,他還有grpc和http方式,在go-plugins里可以找到

8. Service

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

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

4. 參考

https://blog.csdn.net/mi_duo/article/details/82701732


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM