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:

  1. GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro/go-zero

5. Quick Start

  1. Full example please view

    Quick Build High & Microservice

    Quick Build High & Microservice - Multi-RPC

  2. Install goctl tools

    goctl read go control, don’t read go C-T-L.goctl means not being controlled by code but to control it.The go does not mean golang.

    1. 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 command

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

    Make sure goctl is executable

  3. Quickly generate api service

    1. goctl api new greet
    2. cd greet
    3. go mod init
    4. go mod tidy
    5. go run greet.go -f etc/greet-api.yaml

    Default listener at 8888 port (can be modified in configuration file), can request by curl:

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

    Return to the following:

    1. HTTP/1.1 200 OK
    2. Content-Type: application/json; charset=utf-8
    3. Traceparent: 00-45fa9e7a7c505bad3a53a024e425ace9-eb5787234cf3e308-00
    4. Date: Thu, 22 Oct 2020 14:03:18 GMT
    5. Content-Length: 14

    Write business code:

    1. * 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)
    2. * Can pass dependencies to logic, such as mysql, redis and so on in servicecontext.go
    3. * Add business processing logic to the get/post/put/delete request
  4. Java, TypeScript, Dart, JavaScript code can be generated from api files

    1. goctl api java -api greet.api -dir greet
    2. goctl api dart -api greet.api -dir greet
    3. ...

6. Benchmark

Test code is here

Documentation

PluginUsage
goctl-swaggerGenerate api document swagger
goctl-androidGenerate java (android) http client request code
goctl-go-compactMerge 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: