Go Micro 整体设计

Go-micro 是什么

Go-micro框架是一套微服务分布式的框架,能够大幅度的提升开发效率。
图片描述
源码地址:https://github.com/micro/go-micro
Go-micro拥有不少特性:node

  • 服务注册、发现
  • 负载均衡
  • 消息解码,并默认支持json以及protobuf
  • 基于rpc的请求响应
  • 异步的消息通信
  • 接口可插拔

其中最值得一提的是最后一个特性,接口可插拔。只要实现上图的8个关键interface,就能够随意的根据需求从新时间这8个接口的功能。 这8个接口一实现了go-micro的总体架构。
这些接口都有默认的实现方式,意味着你不须要写任何的插件就可使用这个微服务架构。
另外还有一个图也能够很清楚的看清整个Micro的总体流程:
图片描述
能够看到在整个框架中各个模块的做用和相互之间的调用关系。
在服务发现的基础上,经过broker或者是Transport来进行服务之间的通讯。简单来讲就是Client经过Selector模块来进行负载均衡,从注册中心拿到服务节点而后经过节点信息经过Transport定义的通讯协议来进行通讯。git

主要interface

整个Go Micro 都是有这8个interface构成的,换而言之只要理解了这8个接口,并仔细研究其中一个实现基本就能了解整个框架的实现和架构。下面先来看看这8个接口github

Transort

服务之间通讯的接口。也就是服务发送和接收的最终实现方式,是由这些接口定制的。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
}

Codec

有了传输方式,下面要解决的就是传输编码和解码问题,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是解码过程。缓存

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
}
相关文章
相关标签/搜索