微服务架构攀登之路(五)之Go-micro入门

一、go-micro入门

1. go-micro 简介

⚫  Go Micro 是一个插件化的基础框架,基于此可以构建微服务,Micro 的设计哲学是可插拔的插件化架构

⚫  在架构之外,它默认实现了 consul   作为服务发现(2019   年源码修改了默认使用mdns),通过 http 进行通信,通过 protobuf 和 json 进行编解码

2. go-micro 的主要功能

⚫ 服务发现:自动服务注册和名称解析。服务发现是微服务开发的核心。当服务 A需要与服务 B 通话时,它需要该服务的位置。默认发现机制是多播 DNS(mdns),一种零配置系统。您可以选择使用 SWIM 协议为 p2p 网络设置八卦,或者为弹性云原生设置设置 consul

⚫ 负载均衡:基于服务发现构建的客户端负载均衡。一旦我们获得了服务的任意数量实例的地址,我们现在需要一种方法来决定要路由到哪个节点。我们使用随机散列负载均衡来提供跨服务的均匀分布,并在出现问题时重试不同的节点

⚫ 消息编码:基于内容类型的动态消息编码。客户端和服务器将使用编解码器和内容类型为您无缝编码和解码 Go 类型。可以编码任何种类的消息并从不同的客户端发送。客户端和服务器默认处理此问题。这包括默认的protobuf 和 json

⚫ 请求/响应:基于 RPC 的请求/响应,支持双向流。我们提供了同步通信的抽象。对服务的请求将自动解决,负载平衡,拨号和流式传输。启用 tls 时,默认传输为 http/ 1.1 或 http2

⚫ Async Messaging:PubSub 是异步通信和事件驱动架构的一流公民。事件通知是微服务开发的核心模式。启用 tls 时,默认消息传递是点对点 http / 1.1 或 http2

⚫ 可插拔接口:Go Micro 为每个分布式系统抽象使用 Go 接口,因此,这些接口是可插拔的,并允许 Go Micro 与运行时无关,可以插入任何基础技术

◼    插件地址:https://github.com/micro/go-plugins

3. go-micro 通信流程

⚫ Server 监听客户端的调用,和 Brocker 推送过来的信息进行处理。并且 Server 端需要向 Register 注册自己的存在或消亡,这样 Client 才能知道自己的状态

⚫ Register 服务的注册的发现,Client 端从 Register 中得到 Server 的信息,然后每次调用都根据算法选择一个的 Server 进行通信,当然通信是要经过编码/解码,选择传输协议等一系列过程的

⚫ 如果有需要通知所有的 Server 端可以使用 Brocker 进行信息的推送,Brocker 信息队列进行信息的接收和发布

4. go-micro 核心接口

⚫  go-micro 之所以可以高度订制和他的框架结构是分不开的,go-micro 由 8 个关键的interface 组成,每一个 interface 都可以根据自己的需求重新实现,这 8  个主要的inteface 也构成了 go-micro 的框架结构

 二、Go Micro接口详解

1. 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 
} 

2. Codec 编码接口

⚫ go-micro 有很多种编码解码方式,默认的实现方式是protobuf,当然也有其他的实现方式,json

type Codec interface { 
   ReadHeader(*Message, MessageType) error 
   ReadBody(interface{}) error 
   Write(*Message, interface{}) error 
   Close() error 
   String() string 
} 

3. Registry 注册接口

⚫  服务的注册和发现,目前实现的有 consul、mdns、etcd、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 
}  

4. 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 
}  

5. 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 
} 

6. 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 
} 

7. 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 
} 

8. Serveice 接口

⚫ Service  是 Client  和 Server 的封装,他包含了一系列的方法使用初始值去初始化

type Service interface { 
   Init(...Option) 
   Options() Options 
   Client() client.Client 
   Server() server.Server 
   Run() error 
   String() string 
} 

猜你喜欢

转载自www.cnblogs.com/zhangyafei/p/11932229.html