Go-zero is a Web and RPC framework that integrates various engineering practices. Its elastic design guarantees the stability of large concurrent servers and has been fully battle-tested.

Go-zero was designed with “tools over conventions and documentation” in mind, so Go-Zero includes a minimalist API definition and generation tool called Goctl, You can generate Go, iOS, Android, Kotlin, Dart, TypeScript, JavaScript code from defined API files in one click and run it directly.

As shown in the figure above, requests from different clients go to the API side of Go-Zero first. The main function of the API side is to forward the corresponding request to the Service side through THE gRPC protocol through ETCD. The server is responsible for querying or storing data according to the specific content of the request. For query requests, Go-Zero has a built-in API that queries the cache layer first, reducing the query pressure on the database.

As can be seen from the figure, the framework in the API side and Service side has built-in very rich functions. During the development process, we only need to fill in the corresponding business logic, which can easily realize the CRDU level requirements.

Why do we say go-Zero is a microservices architecture out of the box? Now, let’s take a look at some of the most powerful features in Go-Zero.

Go-zero is suitable for rapid development of microservices

Go-zero has a powerful project scaffolding tool called GoCTL. Goctl is as convenient as vue-CLI and react-CLI in the front end. Goctl generates API, RPC, and Model code from configuration files. At the same time, Go-Zero has a relatively complete project framework. Scaffolding generates a project framework that is adequate to meet common requirements. Requirements such as CRDU require only “fill in the blanks” to populate the generated code with the necessary business logic. Other requirements such as cache authentication are already built into the framework.

In addition, Go-Zero has a unique “progressive” framework. “Progressive” is a feature of the front-end Vue framework, loosely defined as “easy to get started and integrate with third-party libraries or existing projects.” This article uses this concept to show that Go-Zero is less intrusive and that the code generated by Go-Zero can be taken apart and used gradually to adapt older projects.

Low coupling module design, rich middleware, plug-ins and tools:

  • The coupling degree of each module in Go-Zero is low. We can find suitable middleware or self-developed middleware through the component center in the document.

  • Goctl also supports plugin commands to extend goctl if goctl does not meet requirements.

  • Many of go-Zero’s configuration files are custom syntax. Go-zero also offers intellij and vscode plug-ins that provide editing enhancements such as syntax highlighting and error checking.

Goctl introduction

Goctl is a code generation tool under the Go-Zero microservices framework. Using GoCTL can significantly improve development efficiency and allow developers to focus their time on business development.

The commands of goctl can be summarized as follows:

  • API command to quickly generate an API service

  • RPC command, support proto template generation and RPC service code generation

  • The model command currently supports model layer code generation by identifying the mysql DDL

  • Plugin command to customize plug-ins for the API

  • Other commands are currently issued related

Goctl commands are numerous, and this time only involves basic commands related to API, RPC and Model.

Basic flow using goCTL

The process of generating code using goctl can be roughly divided into four steps:

  • Use command a to generate the default configuration file.

  • Edit the configuration file based on service requirements.

  • Use command b to generate the default code file from the configuration file;

  • Populate the corresponding code file according to the business logic.

When is go-Zero not suitable for rapid microservice development?

After reading the above introduction, we must be a little bit eager to try the development of micro-services for Go-Zero. However, after some practice, I have decided that go-Zero is not the right framework for developing microservices when the following situations occur.

Current requirements conflict with goCTL’s philosophy

One of go-Zero’s selling points is the scaffolding tool GoCTL, which can conflict with the code generated by GoCTL if there are too many customization requirements. However, if you abandon goCTL and write code manually, the development efficiency will be greatly reduced.

For example, as shown above, Go-Zero currently only supports gRPC on the Service side, Mysql, MongoDB, and ClickHouse on the database layer, and ETCD on Service discovery. In this case, if you want to implement custom operations such as PostgreSQL replacing Mysql and Consul replacing ETCD, the code generated by Goctl may run abnormally.

Hopefully, the functionality provided by the framework is complete

Most of the components of Go-Zero are homegrown, such as SQLX, HTTPX, etc. These homegrown components are more than adequate for CRDU operations, but they fall far short of gorM, GIN and other open source projects that specialize in one direction or another.

So as the business needs of the company become more and more diverse, the current main contradiction from “rapid development” to “refined development”, you will find that the framework has some shortcomings. In this case, RP or fork your own magic. Personally, I find this situation to be much easier to change than a “whole family bucket” like Spring or Django.

Recommended reading

Best practices for storing time in MySQL

Ansible quick start