Framework Overview
go-zero
Shorten distance from demand to offline
go-zero is a web and rpc framework that integrates various engineering practices.Flexible design guarantees stability at the same end and undergoes a full operational test.
go-Zero contains very simple API definitions and generation tools goctl. Can generate Go, iOS, Android, Kotlin, Dart, TypeScript, JavaScript code based on defined api files and can be run directly.
Benefits of using go-zero
- Easily get stability to support millions of living services
- Intra-stage timeout control, limited flow, adaptive melting, adaptive loop and adaptive loop capability for microservice governance without configuration or extra code
- Microservice Governance Middleware can be seamlessly integrated into other existing frameworks using
- Useful API description, all end code generated by one click
- Auto-Verify Client Request Parameters
- A large number of micro-service governance and parallel development kits
1. go-zero framework background
At the beginning of 18 years, we decided to migrate from a single architecture of Java+MongoDB
to a microservice architecture, after careful thought and comparison, we decided to:
- Based on Go Language
- Efficient performance
- Simple Syntax
- Widespread validating engineering efficiency
- Extreme deployment experience
- Very low service resource cost
- Self-research micro-service frameworks
- Many micro-service framework self-research experience
- A faster problem location capability is required
- Easier add new features
2. Thinking on go-zero framework design
With regard to the design of the framework for microservices, we expect to guarantee the stability of microservices with a special focus on research and development efficiency.So at the beginning of the design, we had some guidelines as follows:
- Stay simple, first principle
- Flexible Design, Programming for Failure
- Tools are larger than contracts and documents
- High Available
- High Conversion
- High Scalability
- Development friendly, encapsulated complexity
- Only one way to bind to do one thing
In less than half a year, we have completely migrated from Java+MongoDB
to Golang+MySQL
, the primary microservice system, fully online at the end of August 18, and have stabilized the rapid growth of business follow-up and ensured high availability of the entire service.
3. Implementation and characteristics of the go-zero project
go-zero is an integrated web and rpc framework with various engineering practices, with the following key features:
- Powerful tool support, minimum code writing
- Very simple interface
- Fully compatible net/http
- Support Middleware for Extensions
- High Performance
- Programming for failure, flexible design
- Built-in Services Discovery, Load Balancer
- Internal limit flow, melt, download, and automatically trigger, restore automatically
- API Parameters Auto-Validation
- Timeout link control
- Auto Cache Control
- Link tracking, statistics alerts, etc.
- High and sustained stability, which guarantees daily flow peaks during the epidemic
As shown below, we guarantee high availability of overall services at multiple levels:
Don't note star : capping_hands:
4. Installation
Install under project directory:
GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro/go-zero
5. Quick Start
Full example please view
Install goctl tools
goctl
readgo control
, don't readgo C-T-L
.goctl
means not being controlled by code but to control it.Thego
does not meangolang
.GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro/go-zero/tools/goctl
If using version go1.16, you can install the
go install
commandGOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/go-zero/tools/goctl@latest
Make sure goctl is executable
Quickly generate api service
goctl api new greet
cd greet
go mod init
go mod tidy
go run greet.go -f etc/greet-api.yamlDefault listener at 8888 port (can be modified in configuration file), can request by curl:
curl -i http://localhost:8888/from/you
Return to the following:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Traceparent: 00-45fa9e7a7c505bad3a53a024e425ace9-eb5787234cf3e308-00
Date: Thu, 22 Oct 2020 14:03:18 GMT
Content-Length: 14Write business code:
* api file defines the routing of the service to be exposed, reference [api norms](https://github.com/zeromicro/zero-doc/blob/main/go-zero.dev/cn/api-grammar.md)
* Can pass dependencies to logic, such as mysql, redis and so on in servicecontext.go
* Add business processing logic to the get/post/put/delete requestJava, TypeScript, Dart, JavaScript code can be generated from api files
goctl api java -api greet.api -dir greet
goctl api dart -api greet.api -dir greet
...
6. Benchmark
Documentation
aweesome series
Picked
goctl
plugin
Plugin | Usage |
---|---|
goctl-swagger | Generate api document swagger |
goctl-android | Generate java (android) http client request code |
goctl-go-compact | Merge api with the same group in handler to a go file |
8. Wechat
go-zero
related articles are displayed in microservice practice
public sign up and welcome snippets, and can also be used by public contact me : capping_hands:
9. Wechat groups
If you do not cover any questions in the document, you are welcome to ask in the group and we will reply as soon as possible.
You can set out in the group where improvements are needed, and we will consider reasonableness and modify it as soon as possible.
If you find bugs please submit them in a timely manner issuewe will confirm and modify them as soon as possible.
Add my previous point star, a small star is the power of authors to answer the question of the volume: handshake: