|
|
|
@ -1,199 +1,214 @@
|
|
|
|
|
<img align="right" width="150px" src="https://gitee.com/kevwan/static/raw/master/doc/images/go-zero.png">
|
|
|
|
|
<img align="right" width="150px" src="doc/images/go-zero.png">
|
|
|
|
|
|
|
|
|
|
# go-zero
|
|
|
|
|
|
|
|
|
|
[English](readme-en.md) | 简体中文
|
|
|
|
|
English | [简体中文](readme.md)
|
|
|
|
|
|
|
|
|
|
[![Go](https://github.com/tal-tech/go-zero/workflows/Go/badge.svg?branch=master)](https://github.com/tal-tech/go-zero/actions)
|
|
|
|
|
[![Go Report Card](https://goreportcard.com/badge/github.com/tal-tech/go-zero)](https://goreportcard.com/report/github.com/tal-tech/go-zero)
|
|
|
|
|
[![goproxy](https://goproxy.cn/stats/github.com/tal-tech/go-zero/badges/download-count.svg)](https://goproxy.cn/stats/github.com/tal-tech/go-zero/badges/download-count.svg)
|
|
|
|
|
[![codecov](https://codecov.io/gh/tal-tech/go-zero/branch/master/graph/badge.svg)](https://codecov.io/gh/tal-tech/go-zero)
|
|
|
|
|
[![Go Report Card](https://goreportcard.com/badge/github.com/tal-tech/go-zero)](https://goreportcard.com/report/github.com/tal-tech/go-zero)
|
|
|
|
|
[![Release](https://img.shields.io/github/v/release/tal-tech/go-zero.svg?style=flat-square)](https://github.com/tal-tech/go-zero)
|
|
|
|
|
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
|
|
|
|
|
|
|
|
|
## 0. go-zero 介绍
|
|
|
|
|
## 0. what is go-zero
|
|
|
|
|
|
|
|
|
|
go-zero 是一个集成了各种工程实践的 web 和 rpc 框架。通过弹性设计保障了大并发服务端的稳定性,经受了充分的实战检验。
|
|
|
|
|
go-zero is a web and rpc framework that with lots of engineering practices builtin. It’s born to ensure the stability of the busy services with resilience design, and has been serving sites with tens of millions users for years.
|
|
|
|
|
|
|
|
|
|
go-zero 包含极简的 API 定义和生成工具 goctl,可以根据定义的 api 文件一键生成 Go, iOS, Android, Kotlin, Dart, TypeScript, JavaScript 代码,并可直接运行。
|
|
|
|
|
go-zero contains simple API description syntax and code generation tool called `goctl`. You can generate Go, iOS, Android, Kotlin, Dart, TypeScript, JavaScript from .api files with `goctl`.
|
|
|
|
|
|
|
|
|
|
使用 go-zero 的好处:
|
|
|
|
|
Advantages of go-zero:
|
|
|
|
|
|
|
|
|
|
* 轻松获得支撑千万日活服务的稳定性
|
|
|
|
|
* 内建级联超时控制、限流、自适应熔断、自适应降载等微服务治理能力,无需配置和额外代码
|
|
|
|
|
* 微服务治理中间件可无缝集成到其它现有框架使用
|
|
|
|
|
* 极简的 API 描述,一键生成各端代码
|
|
|
|
|
* 自动校验客户端请求参数合法性
|
|
|
|
|
* 大量微服务治理和并发工具包
|
|
|
|
|
* improve the stability of the services with tens of millions of daily active users
|
|
|
|
|
* builtin chained timeout control, concurrency control, rate limit, adaptive circuit breaker, adaptive load shedding, even no configuration needed
|
|
|
|
|
* builtin middlewares also can be integrated into your frameworks
|
|
|
|
|
* simple API syntax, one command to generate couple of different languages
|
|
|
|
|
* auto validate the request parameters from clients
|
|
|
|
|
* plenty of builtin microservice management and concurrent toolkits
|
|
|
|
|
|
|
|
|
|
<img src="https://gitee.com/kevwan/static/raw/master/doc/images/architecture.png" alt="架构图" width="1500" />
|
|
|
|
|
<img src="doc/images/architecture-en.png" alt="Architecture" width="1500" />
|
|
|
|
|
|
|
|
|
|
## 1. go-zero 框架背景
|
|
|
|
|
## 1. Backgrounds of go-zero
|
|
|
|
|
|
|
|
|
|
18 年初,我们决定从 `Java+MongoDB` 的单体架构迁移到微服务架构,经过仔细思考和对比,我们决定:
|
|
|
|
|
At the beginning of 2018, we decided to re-design our system, from monolithic architecture with Java+MongoDB to microservice architecture. After researches and comparison, we chose to:
|
|
|
|
|
|
|
|
|
|
* 基于 Go 语言
|
|
|
|
|
* 高效的性能
|
|
|
|
|
* 简洁的语法
|
|
|
|
|
* 广泛验证的工程效率
|
|
|
|
|
* 极致的部署体验
|
|
|
|
|
* 极低的服务端资源成本
|
|
|
|
|
* 自研微服务框架
|
|
|
|
|
* 有过很多微服务框架自研经验
|
|
|
|
|
* 需要有更快速的问题定位能力
|
|
|
|
|
* 更便捷的增加新特性
|
|
|
|
|
* Golang based
|
|
|
|
|
* great performance
|
|
|
|
|
* simple syntax
|
|
|
|
|
* proven engineering efficiency
|
|
|
|
|
* extreme deployment experience
|
|
|
|
|
* less server resource consumption
|
|
|
|
|
* Self-designed microservice architecture
|
|
|
|
|
* I have rich experience on designing microservice architectures
|
|
|
|
|
* easy to location the problems
|
|
|
|
|
* easy to extend the features
|
|
|
|
|
|
|
|
|
|
## 2. go-zero 框架设计思考
|
|
|
|
|
## 2. Design considerations on go-zero
|
|
|
|
|
|
|
|
|
|
对于微服务框架的设计,我们期望保障微服务稳定性的同时,也要特别注重研发效率。所以设计之初,我们就有如下一些准则:
|
|
|
|
|
By designing the microservice architecture, we expected to ensure the stability, as well as the productivity. And from just the beginning, we have the following design principles:
|
|
|
|
|
|
|
|
|
|
* 保持简单,第一原则
|
|
|
|
|
* 弹性设计,面向故障编程
|
|
|
|
|
* 工具大于约定和文档
|
|
|
|
|
* 高可用
|
|
|
|
|
* 高并发
|
|
|
|
|
* 易扩展
|
|
|
|
|
* 对业务开发友好,封装复杂度
|
|
|
|
|
* 约束做一件事只有一种方式
|
|
|
|
|
* keep it simple
|
|
|
|
|
* high availability
|
|
|
|
|
* stable on high concurrency
|
|
|
|
|
* easy to extend
|
|
|
|
|
* resilience design, failure-oriented programming
|
|
|
|
|
* try best to be friendly to the business logic development, encapsulate the complexity
|
|
|
|
|
* one thing, one way
|
|
|
|
|
|
|
|
|
|
我们经历不到半年时间,彻底完成了从 `Java+MongoDB` 到 `Golang+MySQL` 为主的微服务体系迁移,并于 18 年 8 月底完全上线,稳定保障了业务后续迅速增长,确保了整个服务的高可用。
|
|
|
|
|
After almost half a year, we finished the transfer from monolithic system to microservice system, and deployed on August 2018. The new system guaranteed the business growth, and the system stability.
|
|
|
|
|
|
|
|
|
|
## 3. go-zero 项目实现和特点
|
|
|
|
|
## 3. The implementation and features of go-zero
|
|
|
|
|
|
|
|
|
|
go-zero 是一个集成了各种工程实践的包含 web 和 rpc 框架,有如下主要特点:
|
|
|
|
|
go-zero is a web and rpc framework that integrates lots of engineering practices. The features are mainly listed below:
|
|
|
|
|
|
|
|
|
|
* 强大的工具支持,尽可能少的代码编写
|
|
|
|
|
* 极简的接口
|
|
|
|
|
* 完全兼容 net/http
|
|
|
|
|
* 支持中间件,方便扩展
|
|
|
|
|
* 高性能
|
|
|
|
|
* 面向故障编程,弹性设计
|
|
|
|
|
* 内建服务发现、负载均衡
|
|
|
|
|
* 内建限流、熔断、降载,且自动触发,自动恢复
|
|
|
|
|
* API 参数自动校验
|
|
|
|
|
* 超时级联控制
|
|
|
|
|
* 自动缓存控制
|
|
|
|
|
* 链路跟踪、统计报警等
|
|
|
|
|
* 高并发支撑,稳定保障了疫情期间每天的流量洪峰
|
|
|
|
|
* powerful tool included, less code to write
|
|
|
|
|
* simple interfaces
|
|
|
|
|
* fully compatible with net/http
|
|
|
|
|
* middlewares are supported, easy to extend
|
|
|
|
|
* high performance
|
|
|
|
|
* failure-oriented programming, resilience design
|
|
|
|
|
* builtin service discovery, load balancing
|
|
|
|
|
* builtin concurrency control, adaptive circuit breaker, adaptive load shedding, auto trigger, auto recover
|
|
|
|
|
* auto validation of API request parameters
|
|
|
|
|
* chained timeout control
|
|
|
|
|
* auto management of data caching
|
|
|
|
|
* call tracing, metrics and monitoring
|
|
|
|
|
* high concurrency protected
|
|
|
|
|
|
|
|
|
|
如下图,我们从多个层面保障了整体服务的高可用:
|
|
|
|
|
As below, go-zero protects the system with couple layers and mechanisms:
|
|
|
|
|
|
|
|
|
|
![弹性设计](https://gitee.com/kevwan/static/raw/master/doc/images/resilience.jpg)
|
|
|
|
|
![Resilience](doc/images/resilience-en.png)
|
|
|
|
|
|
|
|
|
|
觉得不错的话,别忘 **star** 👏
|
|
|
|
|
## 4. Future development plans of go-zero
|
|
|
|
|
|
|
|
|
|
## 4. Installation
|
|
|
|
|
* auto generate API mock server, make the client debugging easier
|
|
|
|
|
* auto generate the simple integration test for the server side just from the .api files
|
|
|
|
|
|
|
|
|
|
在项目目录下通过如下命令安装:
|
|
|
|
|
## 5. Installation
|
|
|
|
|
|
|
|
|
|
Run the following command under your project:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero
|
|
|
|
|
go get -u github.com/tal-tech/go-zero
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 5. Quick Start
|
|
|
|
|
|
|
|
|
|
0. 完整示例请查看
|
|
|
|
|
|
|
|
|
|
[快速构建高并发微服务](https://github.com/tal-tech/zero-doc/blob/main/doc/shorturl.md)
|
|
|
|
|
|
|
|
|
|
[快速构建高并发微服务 - 多 RPC 版](https://github.com/tal-tech/zero-doc/blob/main/docs/zero/bookstore.md)
|
|
|
|
|
|
|
|
|
|
1. 安装 goctl 工具
|
|
|
|
|
|
|
|
|
|
`goctl` 读作 `go control`,不要读成 `go C-T-L`。`goctl` 的意思是不要被代码控制,而是要去控制它。其中的 `go` 不是指 `golang`。在设计 `goctl` 之初,我就希望通过 ` 她 ` 来解放我们的双手👈
|
|
|
|
|
## 6. Quick Start
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero/tools/goctl
|
|
|
|
|
```
|
|
|
|
|
0. full examples can be checked out from below:
|
|
|
|
|
|
|
|
|
|
确保 goctl 可执行
|
|
|
|
|
[Rapid development of microservice systems](https://github.com/tal-tech/zero-doc/blob/main/doc/shorturl-en.md)
|
|
|
|
|
|
|
|
|
|
2. 快速生成 api 服务
|
|
|
|
|
[Rapid development of microservice systems - multiple RPCs](https://github.com/tal-tech/zero-doc/blob/main/doc/bookstore-en.md)
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
goctl api new greet
|
|
|
|
|
cd greet
|
|
|
|
|
go mod init
|
|
|
|
|
go mod tidy
|
|
|
|
|
go run greet.go -f etc/greet-api.yaml
|
|
|
|
|
```
|
|
|
|
|
1. install goctl
|
|
|
|
|
|
|
|
|
|
默认侦听在 8888 端口(可以在配置文件里修改),可以通过 curl 请求:
|
|
|
|
|
`goctl`can be read as `go control`. `goctl` means not to be controlled by code, instead, we control it. The inside `go` is not `golang`. At the very beginning, I was expecting it to help us improve the productivity, and make our lives easier.
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
curl -i http://localhost:8888/from/you
|
|
|
|
|
```
|
|
|
|
|
```shell
|
|
|
|
|
GO111MODULE=on go get -u github.com/tal-tech/go-zero/tools/goctl
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
返回如下:
|
|
|
|
|
make sure goctl is executable.
|
|
|
|
|
|
|
|
|
|
```http
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
|
Content-Type: application/json
|
|
|
|
|
Date: Thu, 22 Oct 2020 14:03:18 GMT
|
|
|
|
|
Content-Length: 14
|
|
|
|
|
2. create the API file, like greet.api, you can install the plugin of goctl in vs code, api syntax is supported.
|
|
|
|
|
|
|
|
|
|
{"message":""}
|
|
|
|
|
```
|
|
|
|
|
```go
|
|
|
|
|
type Request struct {
|
|
|
|
|
Name string `path:"name,options=you|me"` // parameters are auto validated
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
编写业务代码:
|
|
|
|
|
type Response struct {
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
* api 文件定义了服务对外暴露的路由,可参考 [api 规范](https://github.com/tal-tech/zero-doc/blob/main/doc/goctl.md)
|
|
|
|
|
* 可以在 servicecontext.go 里面传递依赖给 logic,比如 mysql, redis 等
|
|
|
|
|
* 在 api 定义的 get/post/put/delete 等请求对应的 logic 里增加业务处理逻辑
|
|
|
|
|
service greet-api {
|
|
|
|
|
@handler GreetHandler
|
|
|
|
|
get /greet/from/:name(Request) returns (Response);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
the .api files also can be generate by goctl, like below:
|
|
|
|
|
|
|
|
|
|
3. 可以根据 api 文件生成前端需要的 Java, TypeScript, Dart, JavaScript 代码
|
|
|
|
|
```shell
|
|
|
|
|
goctl api -o greet.api
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
3. generate the go server side code
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
goctl api java -api greet.api -dir greet
|
|
|
|
|
goctl api dart -api greet.api -dir greet
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
```shell
|
|
|
|
|
goctl api go -api greet.api -dir greet
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 6. Benchmark
|
|
|
|
|
the generated files look like:
|
|
|
|
|
|
|
|
|
|
![benchmark](https://gitee.com/kevwan/static/raw/master/doc/images/benchmark.png)
|
|
|
|
|
```Plain Text
|
|
|
|
|
├── greet
|
|
|
|
|
│ ├── etc
|
|
|
|
|
│ │ └── greet-api.yaml // configuration file
|
|
|
|
|
│ ├── greet.go // main file
|
|
|
|
|
│ └── internal
|
|
|
|
|
│ ├── config
|
|
|
|
|
│ │ └── config.go // configuration definition
|
|
|
|
|
│ ├── handler
|
|
|
|
|
│ │ ├── greethandler.go // get/put/post/delete routes are defined here
|
|
|
|
|
│ │ └── routes.go // routes list
|
|
|
|
|
│ ├── logic
|
|
|
|
|
│ │ └── greetlogic.go // request logic can be written here
|
|
|
|
|
│ ├── svc
|
|
|
|
|
│ │ └── servicecontext.go // service context, mysql/redis can be passed in here
|
|
|
|
|
│ └── types
|
|
|
|
|
│ └── types.go // request/response defined here
|
|
|
|
|
└── greet.api // api description file
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[测试代码见这里](https://github.com/smallnest/go-web-framework-benchmark)
|
|
|
|
|
the generated code can be run directly:
|
|
|
|
|
|
|
|
|
|
## 7. 文档
|
|
|
|
|
```shell
|
|
|
|
|
cd greet
|
|
|
|
|
go mod init
|
|
|
|
|
go mod tidy
|
|
|
|
|
go run greet.go -f etc/greet-api.yaml
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* API 文档
|
|
|
|
|
by default, it’s listening on port 8888, while it can be changed in configuration file.
|
|
|
|
|
|
|
|
|
|
[https://www.yuque.com/tal-tech/go-zero](https://www.yuque.com/tal-tech/go-zero)
|
|
|
|
|
you can check it by curl:
|
|
|
|
|
|
|
|
|
|
* awesome 系列(更多文章见『微服务实践』公众号)
|
|
|
|
|
* [快速构建高并发微服务](https://github.com/tal-tech/zero-doc/blob/main/doc/shorturl.md)
|
|
|
|
|
* [快速构建高并发微服务 - 多 RPC 版](https://github.com/tal-tech/zero-doc/blob/main/docs/zero/bookstore.md)
|
|
|
|
|
* [goctl 使用帮助](https://github.com/tal-tech/zero-doc/blob/main/doc/goctl.md)
|
|
|
|
|
|
|
|
|
|
* 精选 `goctl` 插件
|
|
|
|
|
```shell
|
|
|
|
|
curl -i http://localhost:8888/from/you
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
| 插件 | 用途 |
|
|
|
|
|
| ------------- |:-------------|
|
|
|
|
|
| [goctl-swagger](https://github.com/zeromicro/goctl-swagger) | 一键生成 `api` 的 `swagger` 文档 |
|
|
|
|
|
| [goctl-android](https://github.com/zeromicro/goctl-android) | 生成 `java (android)` 端 `http client` 请求代码 |
|
|
|
|
|
| [goctl-go-compact](https://github.com/zeromicro/goctl-go-compact) | 合并 `api` 里同一个 `group` 里的 `handler` 到一个 `go` 文件 |
|
|
|
|
|
the response looks like:
|
|
|
|
|
|
|
|
|
|
## 8. 微信公众号
|
|
|
|
|
```http
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
|
Date: Sun, 30 Aug 2020 15:32:35 GMT
|
|
|
|
|
Content-Length: 0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`go-zero` 相关文章都会在 `微服务实践` 公众号整理呈现,欢迎扫码关注,也可以通过公众号私信我 👏
|
|
|
|
|
4. Write the business logic code
|
|
|
|
|
|
|
|
|
|
<img src="https://gitee.com/kevwan/static/raw/master/images/wechat-micro.jpg" alt="wechat" width="300" />
|
|
|
|
|
* the dependencies can be passed into the logic within servicecontext.go, like mysql, reds etc.
|
|
|
|
|
* add the logic code in logic package according to .api file
|
|
|
|
|
|
|
|
|
|
## 9. 微信交流群
|
|
|
|
|
5. Generate code like Java, TypeScript, Dart, JavaScript etc. just from the api file
|
|
|
|
|
|
|
|
|
|
如果文档中未能覆盖的任何疑问,欢迎您在群里提出,我们会尽快答复。
|
|
|
|
|
```shell
|
|
|
|
|
goctl api java -api greet.api -dir greet
|
|
|
|
|
goctl api dart -api greet.api -dir greet
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
您可以在群内提出使用中需要改进的地方,我们会考虑合理性并尽快修改。
|
|
|
|
|
## 7. Benchmark
|
|
|
|
|
|
|
|
|
|
如果您发现 ***bug*** 请及时提 ***issue***,我们会尽快确认并修改。
|
|
|
|
|
![benchmark](doc/images/benchmark.png)
|
|
|
|
|
|
|
|
|
|
为了防止广告用户、识别技术同行,请 ***star*** 后加我时注明 **github** 当前 ***star*** 数,我再拉进 **go-zero** 群,感谢!
|
|
|
|
|
[Checkout the test code](https://github.com/smallnest/go-web-framework-benchmark)
|
|
|
|
|
|
|
|
|
|
加我之前有劳点一下 ***star***,一个小小的 ***star*** 是作者们回答海量问题的动力🤝
|
|
|
|
|
## 8. Documents (adding)
|
|
|
|
|
|
|
|
|
|
<img src="https://gitee.com/kevwan/static/raw/master/images/wechat.jpg" alt="wechat" width="300" />
|
|
|
|
|
* [Rapid development of microservice systems](https://github.com/tal-tech/zero-doc/blob/main/doc/shorturl-en.md)
|
|
|
|
|
* [Rapid development of microservice systems - multiple RPCs](https://github.com/tal-tech/zero-doc/blob/main/doc/bookstore-en.md)
|
|
|
|
|
|
|
|
|
|
项目地址:[https://github.com/tal-tech/go-zero](https://github.com/tal-tech/go-zero)
|
|
|
|
|
## 9. Chat group
|
|
|
|
|
|
|
|
|
|
码云地址:[https://gitee.com/kevwan/go-zero](https://gitee.com/kevwan/go-zero) (国内用户可访问gitee,每日自动从github同步代码)
|
|
|
|
|
Join the chat via https://discord.gg/4JQvC5A4Fe
|
|
|
|
|