


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.
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
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.
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.
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 }
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 }
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 }
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
我个人比较喜欢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 }
默认的是实现是本地缓存,当前实现的有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 }
默认的是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 }
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!

Golangisidealforperformance-criticalapplicationsandconcurrentprogramming,whilePythonexcelsindatascience,rapidprototyping,andversatility.1)Forhigh-performanceneeds,chooseGolangduetoitsefficiencyandconcurrencyfeatures.2)Fordata-drivenprojects,Pythonisp

Golang achieves efficient concurrency through goroutine and channel: 1.goroutine is a lightweight thread, started with the go keyword; 2.channel is used for secure communication between goroutines to avoid race conditions; 3. The usage example shows basic and advanced usage; 4. Common errors include deadlocks and data competition, which can be detected by gorun-race; 5. Performance optimization suggests reducing the use of channel, reasonably setting the number of goroutines, and using sync.Pool to manage memory.

Golang is more suitable for system programming and high concurrency applications, while Python is more suitable for data science and rapid development. 1) Golang is developed by Google, statically typing, emphasizing simplicity and efficiency, and is suitable for high concurrency scenarios. 2) Python is created by Guidovan Rossum, dynamically typed, concise syntax, wide application, suitable for beginners and data processing.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Go language has unique advantages in concurrent programming, performance, learning curve, etc.: 1. Concurrent programming is realized through goroutine and channel, which is lightweight and efficient. 2. The compilation speed is fast and the operation performance is close to that of C language. 3. The grammar is concise, the learning curve is smooth, and the ecosystem is rich.

The main differences between Golang and Python are concurrency models, type systems, performance and execution speed. 1. Golang uses the CSP model, which is suitable for high concurrent tasks; Python relies on multi-threading and GIL, which is suitable for I/O-intensive tasks. 2. Golang is a static type, and Python is a dynamic type. 3. Golang compiled language execution speed is fast, and Python interpreted language development is fast.

Golang is usually slower than C, but Golang has more advantages in concurrent programming and development efficiency: 1) Golang's garbage collection and concurrency model makes it perform well in high concurrency scenarios; 2) C obtains higher performance through manual memory management and hardware optimization, but has higher development complexity.

Golang is widely used in cloud computing and DevOps, and its advantages lie in simplicity, efficiency and concurrent programming capabilities. 1) In cloud computing, Golang efficiently handles concurrent requests through goroutine and channel mechanisms. 2) In DevOps, Golang's fast compilation and cross-platform features make it the first choice for automation tools.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function