Skip to main content

Introduction

go-zero is an integrated web and rpc framework for a variety of engineering practices. The elastic design guarantees the stability of the large concurrent server and has been fully tested in the field.

go-zero includes the minimal API definition and generation tool goctl, which can generate Go, iOS, Android, Kotlin, Dart, TypeScript, JavaScript code based on the defined api file with one click and can be run directly.

Benefits of using go-zero:

  • โœ… Easy to get the stability to support 10 million daily service.
  • โœ… Built-in cascade timeout control, current limiting, adaptive fusing, adaptive load shedding and other microservice governance capabilities without configuration and additional code.
  • โœ… Microservice governance middleware can be seamlessly integrated with other existing frameworks.
  • โœ… Minimal API description, one-click code generation for each end.
  • โœ… Automatic verification of the legitimacy of client request parameters.
  • โœ… Extensive microservice governance and concurrency toolkit.
ๆžถๆž„ๅ›พ

go-zero framework backgroundโ€‹

In early 18, we decided to migrate from a Java+MongoDB monolithic architecture to a microservices architecture, and after careful thought and comparison, we decided that:

  • Go-based language
    • Efficient performance
    • Simple syntax
    • Extensively proven engineering efficiency
    • Ultimate deployment experience
    • Extremely low server-side resource costs
  • Self-developed microservices framework
    • A lot of experience in self-researching microservices frameworks
    • Need to have faster problem location
    • Easier to add new features

go-zero framework design thinkingโ€‹

For the design of the microservice framework, we expect to guarantee the stability of microservices while paying special attention to R&D efficiency. So at the beginning of the design, we have some guidelines as follows.

  • Keep it simple, the first principle
  • resilient design, fault-oriented programming
  • Tools over conventions and documentation
  • High availability
  • Highly concurrent
  • Easy to scale
  • Business development friendly, encapsulating complexity
  • Constraints do one thing only one way

In less than half a year, we completely completed the migration from Java+MongoDB to Golang+MySQL as the main microservice system, and it was fully online at the end of August 18, which has guaranteed the subsequent rapid growth of business and ensured the high availability of the whole service.

go-zero project implementation and featuresโ€‹

go-zero is an integrated web and rpc framework with various engineering practices, with the following key features.

  • powerful tool support, as little code as possible to write
  • minimalist interface
  • fully compatible with net/http
  • middleware support for easy extensions
  • high performance
  • Fault-oriented programming, resilient design
  • Built-in service discovery, load balancing
  • Built-in flow limiting, meltdown, load shedding, and auto-trigger, auto-recovery
  • Automatic API parameter validation
  • Timeout cascade control
  • Automatic cache control
  • Link tracking, statistical alarms, etc.
  • High concurrency support, stable to ensure the daily traffic flood during the epidemic

In the figure below, we guarantee high availability of the overall service on several levels.

Flexible design

If you think it's good, don't forget to star ๐Ÿ‘

Quick Startโ€‹

For the full example, please seeโ€‹

Quick Build Highly Concurrent Microservices

Quick Build Highly Concurrent Microservices - Multi RPC Edition

Install the goctl toolโ€‹

goctl is pronounced as go control, not as go C-T-L. goctl means not to be controlled by the code, but to control it. Where go does not mean golang. When I designed goctl, I wanted to use `her to free our hands ๐Ÿ‘ˆ

GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero/tools/goctl

If you are using go1.16, you can install it with the go install command

GOPROXY=https://goproxy.cn/,direct go install github.com/tal-tech/go-zero/tools/goctl@latest

Ensure that goctl is executable

Quick Generate api Serviceโ€‹

goctl api new greet
cd greet
go mod init
go mod tidy
go run greet.go -f etc/greet-api.yaml

The default listener is on port 8888 (which can be changed in the configuration file) and can be requested via curl at

curl -i http://localhost:8888/from/you

Returns the following:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 22 Oct 2020 14:03:18 GMT
Content-Length: 14

{"message":""}

Writing business code.

  • The api file defines the routes that the service exposes to the public
  • You can pass dependencies to the logic in servicecontext.go, such as mysql, redis, etc.
  • Add business processing logic to the logic corresponding to the get/post/put/delete requests defined in the api

can generate Java, TypeScript, Dart, JavaScript code needed for front-end based on api fileโ€‹

goctl api java -api greet.api -dir greet
goctl api dart -api greet.api -dir greet
...

Benchmarkโ€‹

benchmark

The test code is available here

Plugin Application
goctl-swagger One Click Generation api of swagger Documentation
goctl-android Generation java (android) End http client Request codes
goctl-go-compact Merge api the same group Inside the handler to a go file
## WeChat public number

go-zero related articles will be presented in microservices practice public number, welcome to scan the code to pay attention to, also can through the public number private message me ๐Ÿ‘

wechat

WeChat Exchange Groupโ€‹

If there are any queries that are not covered in the documentation, you are welcome to ask in the group and we will reply as soon as possible.

You can suggest any improvement in use in the group, and we will consider the reasonableness and modify it as soon as possible.

If you find bug please mention issue in time, we will confirm and modify as soon as possible.

In order to prevent advertising users, identify technical peers, please star after adding me specify github current star number, I then pull into the go-zero group, thanks!

Before adding me, please click star, a small star is the motivation for the authors to answer a lot of questions ๐Ÿค

wechat