Home > Backend Development > Golang > Introduction to the overall architecture of go microservice framework go-micro

Introduction to the overall architecture of go microservice framework go-micro

Release: 2019-11-26 13:19:53
forward
4327 people have browsed it

Introduction to the overall architecture of go microservice framework go-micro

A small project in the product mouth, from project establishment to development and launch, will become more and more complex as time and demand continue to surge, turning into a large project. If the early project If the architecture is not designed well, the code will become more and more bloated and difficult to maintain. Each subsequent product iteration will affect the whole system.

Microservice-based projects and loosely coupled relationships between modules are a good choice. Although it increases maintenance costs, it is still worth it.

Introduction to the overall architecture of go microservice framework go-micro

In addition to the stability of microservice projects, I am also personally concerned about several issues:

1: The efficiency and security of data transmission between services.

2: Dynamic expansion of services, that is, service registration and discovery, and service clustering.

3: Microservice functions can be customized, because not all functions will meet your needs. It is inevitable that you need to develop some functions according to your own needs.

go-micro is a good rpc microservice framework under go language. It has perfect functions and has also solved several problems that I am concerned about:

1: The transmission format between services is protobuf, which is extremely efficient, very fast and safe.

2: go-micro's service registration and discovery are diverse. I personally prefer etcdv3's service discovery and registration.

3: The main functions have corresponding interfaces. As long as the corresponding interfaces are implemented, you can customize the plug-in according to your own needs.

In my spare time, I systematically read the source code of go-micro. The more I read, the more I felt that this framework was well written, and I learned a lot from it. I just want to compile a series of posts and share my experience of learning go-micro with everyone.

The communication process of go-micro is as follows

Introduction to the overall architecture of go microservice framework go-micro

Server monitors the client’s calls and processes the information pushed by Brocker. And the server needs to register its existence or death with the Register so that the client can know its status.

Register service registration discovery.

The client gets the Server information from the Register, and then selects a Server for communication based on the algorithm for each call. Of course, communication requires a series of processes such as encoding/decoding and selecting a transmission protocol.

If you need to notify all servers, you can use Brocker to push information.

Brocker information queue receives and publishes information.

The reason why go-micro can be highly customized is inseparable from its framework structure. go-micro consists of 8 key interfaces. Each interface can be re-implemented according to its own needs. This The eight main intefaces also constitute the framework structure of go-micro.

Introduction to the overall architecture of go microservice framework go-micro

These interfaces go-micir has its own default implementation, and there is also a go-plugins that is an alternative to the implementation of these interfaces. You can also implement your own plug-ins according to your needs.

Introduction to the overall architecture of go microservice framework go-micro

This post is mainly to introduce you to the main structure of go-micro and the functions of these interfaces. We will talk about the specific details in future articles:

Transort

Interface for communication between services. That is, the final implementation of service sending and receiving is customized by these interfaces.

Source code:

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
}
Copy after login

Transport's Listen method is generally called by the server. It listens to a port and waits for the client to call.

Transport's Dial is the method used by the client to connect to the service. It returns a Client interface, which returns a Client interface. This Client is embedded in the Socket interface. The methods of this interface are to specifically send and receive communication information.

http transmission is the default synchronous communication mechanism of go-micro. Of course, there are many other plug-ins: grpc, nats, tcp, udp, rabbitmq, nats, all of which have been implemented so far. You can find it in go-plugins.

Codec

With the transmission method, the next thing to solve is the transmission encoding and decoding problem. go-micro has many encoding and decoding methods. The default implementation is protobuf, of course, there are other implementation methods, such as json, protobuf, jsonrpc, mercury, etc.

Source code

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
}
Copy after login

The Write method of the Codec interface is the encoding process, and the two Reads are the decoding process.

Registry

Service registration and discovery, currently implemented consul, mdns, etcd, etcdv3, zookeeper, kubernetes., etc.,

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
}
Copy after login

To put it simply, the Service registers and the Client uses the watch method for monitoring. When a service is added or deleted, this method will be triggered to remind the client to update the Service information.

The default service registration and discovery is consul, but I personally do not recommend it because you cannot use consul cluster directly

Introduction to the overall architecture of go microservice framework go-micro

我个人比较喜欢etcdv3集群。大家可以根据自己的喜好选择。

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
}
Copy after login

默认的是实现是本地缓存,当前实现的有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
}
Copy after login

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
}
Copy after login

当然他也支持双工通信 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
}
Copy after login

 默认的是rpc实现方式,他还有grpc和http方式,在go-plugins里可以找到

Service

Service是Client和Server的封装,他包含了一系列的方法使用初始值去初始化Service和Client,使我们可以很简单的创建一个rpc服务。

源码:

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

The above is the detailed content of Introduction to the overall architecture of go microservice framework go-micro. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template