7

go-zero is a Web and rpc framework that integrates various engineering practices. Its flexible design ensures the stability of the large concurrent server, and it has been fully tested in actual combat.

go-zero follows the concept of "tools are greater than conventions and documents" when designing, so go-zero includes a minimalist API definition and generation tool goctl, which can generate Go, iOS, Android, Kotlin, and Kotlin in one click based on the defined API files. Dart, TypeScript, JavaScript code, and can be run directly.

go-zero 的架构图

As shown in the figure above, requests from different clients will enter the go-zero API first. The main function of the API side is to forward the corresponding request to the Service side through the gRPC protocol through ETCD. According to the specific content of the request, the Service end is responsible for querying or storing the data. If it is a query request, go-zero has a built-in API to query the cache layer first to reduce the query pressure on the database.

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

Why do we say that go-zero is a microservice architecture out of the box? No hurry, let's take stock of the powerful features in go-zero.

go-zero is suitable for rapid development of microservices

Go-zero has a powerful project scaffolding tool goctl. Goctl is as convenient as Vue-cli and React-cli in the front end. Goctl can generate API, rpc, model and other related codes through configuration files. At the same time, go-zero has a relatively complete project framework. The project framework generated by the scaffolding is sufficient to cope with common needs. CRDU and other requirements only need to do "fill in the blanks" to fill in the necessary business logic on the generated code. Other requirements such as cache authentication have already been built into the framework.

In addition, go-zero has a unique "progressive" framework. "Progressive" is a major feature of the front-end Vue framework, to the effect that it is "easy to use and easy to integrate with third-party libraries or existing projects." This article borrows this concept to show that go-zero is less intrusive to the project. The code generated by go-zero can be disassembled and used to gradually transform the old project.

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.
  • If you feel that goctl cannot meet your needs, goctl also supports plugin commands to extend goctl.
  • Many configuration files of go-zero are custom syntax. go-zero also provides intellij and vscode plug-ins, and provides editing enhancements such as syntax highlighting and error checking.

goctl introduction

goctl is a code generation tool under the go-zero microservice framework. Using goctl can significantly improve development efficiency, allowing developers to focus their time on business development.

The commands of goctl can be summarized into the following categories:

  • API commands to quickly generate an API service
  • rpc command, support proto template generation and rpc service code generation
  • model command, currently supports identifying mysql ddl for model layer code generation
  • plugin command, supports custom plugins for API
  • Other commands are currently related to the release

There are many commands in goctl. This time, only the basic commands related to API, rpc and model are involved.

The basic process of using goctl

The process of using goctl to generate code can be roughly divided into 4 steps:

  • Use command a to generate the default configuration file;
  • Edit the configuration file according to business requirements;
  • Use command b to generate the default code file according to the configuration file;
  • Fill in the corresponding code file according to the business logic.

Under what circumstances is it inappropriate to use go-zero for rapid development of microservices?

After reading the above introduction, you must be a little eager to try go-zero development of microservices. However, after some practice, I think it is not appropriate to use go-zero as a framework for developing microservices when the following situations occur.

Current demand conflicts with the concept of

A big selling point of go-zero is the scaffolding tool goctl. If there are too many customization requirements, it may conflict with the code generated by goctl. But if you give up goctl and write the code manually, the development efficiency will be greatly reduced.

For example, as shown in the figure above, go-zero currently only supports gRPC on the Service side, only Mysql, MongoDB and ClickHouse on the database layer, and service discovery only supports ETCD. In this case, if you want to implement custom operations such as PostgreSQL replacing Mysql and Consul replacing ETCD, the code generated by goctl is likely to be abnormal when executed.

hope that the functions provided by the framework are very complete

Most of go-zero components are self-developed, such as sqlx, httpx, etc. These self-developed components are more than enough to satisfy CRDU operations, but there is still a big gap compared with open source projects such as gorm and gin that specialize in a certain direction.

Therefore, as the company's business development needs become more and more diverse, when the current main contradiction changes from "rapid development" to "refined development", you will find that the framework has one or another deficiency. In this case, you need to mention the RP or fork a magic change yourself. Personally, I feel that this situation is less labor-intensive and worry-free than changing a "family bucket" like Spring or Django.

Recommended reading

The mental journey of configuring monitoring services for

Flink's practice in the batch processing of cloud logs


云叔_又拍云
5.9k 声望4.6k 粉丝

又拍云是专注CDN、云存储、小程序开发方案、 短视频开发方案、DDoS高防等产品的国内知名企业级云服务商。