github.com/lingyao2333/mo-zero@v1.4.1/readme-cn.md (about)

     1  <img align="right" width="150px" src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/go-zero.png">
     2  
     3  # go-zero
     4  
     5  **_缩短从需求到上线的距离_**
     6  
     7  [English](readme.md) | 简体中文
     8  
     9  [![Go](https://github.com/lingyao2333/mo-zero/workflows/Go/badge.svg?branch=master)](https://github.com/lingyao2333/mo-zero/actions)
    10  [![Go Report Card](https://goreportcard.com/badge/github.com/lingyao2333/mo-zero)](https://goreportcard.com/report/github.com/lingyao2333/mo-zero)
    11  [![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)
    12  [![codecov](https://codecov.io/gh/lingyao2333/mo-zero/branch/master/graph/badge.svg)](https://codecov.io/gh/lingyao2333/mo-zero)
    13  [![Release](https://img.shields.io/github/v/release/lingyao2333/mo-zero.svg?style=flat-square)](https://github.com/lingyao2333/mo-zero)
    14  [![Go Reference](https://pkg.go.dev/badge/github.com/lingyao2333/mo-zero.svg)](https://pkg.go.dev/github.com/lingyao2333/mo-zero)
    15  [![Awesome Go](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go)
    16  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
    17  
    18  <a href="https://www.producthunt.com/posts/go-zero?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-go&#0045;zero" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=334030&theme=light" alt="go&#0045;zero - A&#0032;web&#0032;&#0038;&#0032;rpc&#0032;framework&#0032;written&#0032;in&#0032;Go&#0046; | Product Hunt" style="width: 250px; height: 54px;" width="250" height="54" /></a>
    19  
    20  > **_注意:_**
    21  >
    22  > 从 v1.3.0 之前版本升级请执行以下命令:
    23  >
    24  > `GOPROXY=https://goproxy.cn/,direct go install github.com/lingyao2333/mo-zero/tools/goctl@latest`
    25  >
    26  > `goctl migrate —verbose —version v1.4.0`
    27  
    28  ## 0. go-zero 介绍
    29  
    30  go-zero(收录于 CNCF 云原生技术全景图:[https://landscape.cncf.io/?selected=go-zero](https://landscape.cncf.io/?selected=go-zero))是一个集成了各种工程实践的 web 和 rpc 框架。通过弹性设计保障了大并发服务端的稳定性,经受了充分的实战检验。
    31  
    32  go-zero 包含极简的 API 定义和生成工具 goctl,可以根据定义的 api 文件一键生成 Go, iOS, Android, Kotlin, Dart, TypeScript, JavaScript 代码,并可直接运行。
    33  
    34  使用 go-zero 的好处:
    35  
    36  - 轻松获得支撑千万日活服务的稳定性
    37  - 内建级联超时控制、限流、自适应熔断、自适应降载等微服务治理能力,无需配置和额外代码
    38  - 微服务治理中间件可无缝集成到其它现有框架使用
    39  - 极简的 API 描述,一键生成各端代码
    40  - 自动校验客户端请求参数合法性
    41  - 大量微服务治理和并发工具包
    42  
    43  ![架构图](https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/architecture.png)
    44  
    45  ## 1. go-zero 框架背景
    46  
    47  18 年初,我们决定从 `Java+MongoDB` 的单体架构迁移到微服务架构,经过仔细思考和对比,我们决定:
    48  
    49  - 基于 Go 语言
    50    - 高效的性能
    51    - 简洁的语法
    52    - 广泛验证的工程效率
    53    - 极致的部署体验
    54    - 极低的服务端资源成本
    55  - 自研微服务框架
    56    - 有过很多微服务框架自研经验
    57    - 需要有更快速的问题定位能力
    58    - 更便捷的增加新特性
    59  
    60  ## 2. go-zero 框架设计思考
    61  
    62  对于微服务框架的设计,我们期望保障微服务稳定性的同时,也要特别注重研发效率。所以设计之初,我们就有如下一些准则:
    63  
    64  - 保持简单,第一原则
    65  - 弹性设计,面向故障编程
    66  - 工具大于约定和文档
    67  - 高可用、高并发、易扩展
    68  - 对业务开发友好,封装复杂度
    69  - 约束做一件事只有一种方式
    70  
    71  我们经历不到半年时间,彻底完成了从 `Java+MongoDB` 到 `Golang+MySQL` 为主的微服务体系迁移,并于 18 年 8 月底完全上线,稳定保障了业务后续迅速增长,确保了整个服务的高可用。
    72  
    73  ## 3. go-zero 项目实现和特点
    74  
    75  go-zero 是一个集成了各种工程实践的包含 web 和 rpc 框架,有如下主要特点:
    76  
    77  - 强大的工具支持,尽可能少的代码编写
    78  - 极简的接口
    79  - 完全兼容 net/http
    80  - 支持中间件,方便扩展
    81  - 高性能
    82  - 面向故障编程,弹性设计
    83  - 内建服务发现、负载均衡
    84  - 内建限流、熔断、降载,且自动触发,自动恢复
    85  - API 参数自动校验
    86  - 超时级联控制
    87  - 自动缓存控制
    88  - 链路跟踪、统计报警等
    89  - 高并发支撑,稳定保障了疫情期间每天的流量洪峰
    90  
    91  如下图,我们从多个层面保障了整体服务的高可用:
    92  
    93  ![弹性设计](https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/resilience.jpg)
    94  
    95  ## 4. 我们使用 go-zero 的基本架构图
    96  
    97  <img width="1067" alt="image" src="https://user-images.githubusercontent.com/1918356/171880582-11a86658-41c3-466c-95e7-7b1220eecc52.png">
    98  
    99  觉得不错的话,别忘 **star** 👏
   100  
   101  ## 5. Installation
   102  
   103  在项目目录下通过如下命令安装:
   104  
   105  ```shell
   106  GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/lingyao2333/mo-zero
   107  ```
   108  
   109  ## 6. Quick Start
   110  
   111  0. 完整示例请查看
   112  
   113     [快速构建高并发微服务](https://github.com/zeromicro/zero-doc/blob/main/doc/shorturl.md)
   114  
   115     [快速构建高并发微服务 - 多 RPC 版](https://github.com/zeromicro/zero-doc/blob/main/docs/zero/bookstore.md)
   116  
   117  1. 安装 goctl 工具
   118  
   119     `goctl` 读作 `go control`,不要读成 `go C-T-L`。`goctl` 的意思是不要被代码控制,而是要去控制它。其中的 `go` 不是指 `golang`。在设计 `goctl` 之初,我就希望通过 `工具` 来解放我们的双手 👈
   120  
   121     ```shell
   122     # Go 1.15 及之前版本
   123     GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/lingyao2333/mo-zero/tools/goctl@latest
   124  
   125     # Go 1.16 及以后版本
   126     GOPROXY=https://goproxy.cn/,direct go install github.com/lingyao2333/mo-zero/tools/goctl@latest
   127  
   128     # For Mac
   129     brew install goctl
   130  
   131     # docker for amd64 architecture
   132     docker pull kevinwan/goctl
   133     # run goctl like
   134     docker run --rm -it -v `pwd`:/app kevinwan/goctl goctl --help
   135  
   136     # docker for arm64 (M1) architecture
   137     docker pull kevinwan/goctl:latest-arm64
   138     # run goctl like
   139     docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 goctl --help
   140     ```
   141  
   142     确保 goctl 可执行
   143  
   144  2. 快速生成 api 服务
   145  
   146     ```shell
   147     goctl api new greet
   148     cd greet
   149     go mod init
   150     go mod tidy
   151     go run greet.go -f etc/greet-api.yaml
   152     ```
   153  
   154     默认侦听在 `8888` 端口(可以在配置文件里修改),可以通过 `curl` 请求:
   155  
   156     ```shell
   157     curl -i http://localhost:8888/from/you
   158     ```
   159  
   160     返回如下:
   161  
   162     ```http
   163     HTTP/1.1 200 OK
   164     Content-Type: application/json
   165     Date: Thu, 22 Oct 2020 14:03:18 GMT
   166     Content-Length: 14
   167  
   168     {"message":""}
   169     ```
   170  
   171     编写业务代码:
   172  
   173     - api 文件定义了服务对外 HTTP 接口,可参考 [api 规范](https://github.com/zeromicro/zero-doc/blob/main/docs/zero/goctl-api.md)
   174     - 可以在 `servicecontext.go` 里面传递依赖给 logic,比如 mysql, redis 等
   175     - 在 api 定义的 `get/post/put/delete` 等请求对应的 logic 里增加业务处理逻辑
   176  
   177  3. 可以根据 api 文件生成前端需要的 Java, TypeScript, Dart, JavaScript 代码
   178  
   179     ```shell
   180     goctl api java -api greet.api -dir greet
   181     goctl api dart -api greet.api -dir greet
   182     ...
   183     ```
   184  
   185  ## 7. Benchmark
   186  
   187  ![benchmark](https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/benchmark.png)
   188  
   189  [测试代码见这里](https://github.com/smallnest/go-web-framework-benchmark)
   190  
   191  ## 8. 文档
   192  
   193  - API 文档
   194  
   195    [https://go-zero.dev/cn/](https://go-zero.dev/cn/)
   196  
   197  - awesome 系列(更多文章见『微服务实践』公众号)
   198  
   199    - [快速构建高并发微服务](https://github.com/zeromicro/zero-doc/blob/main/doc/shorturl.md)
   200    - [快速构建高并发微服务 - 多 RPC 版](https://github.com/zeromicro/zero-doc/blob/main/docs/zero/bookstore.md)
   201    - [goctl 使用帮助](https://github.com/zeromicro/zero-doc/blob/main/doc/goctl.md)
   202    - [Examples](https://github.com/zeromicro/zero-examples)
   203  
   204  - 精选 `goctl` 插件
   205  
   206    | 插件                                                              | 用途                                                        |
   207    | ----------------------------------------------------------------- | :---------------------------------------------------------- |
   208    | [goctl-swagger](https://github.com/zeromicro/goctl-swagger)       | 一键生成 `api` 的 `swagger` 文档                            |
   209    | [goctl-android](https://github.com/zeromicro/goctl-android)       | 生成 `java (android)` 端 `http client` 请求代码             |
   210    | [goctl-go-compact](https://github.com/zeromicro/goctl-go-compact) | 合并 `api` 里同一个 `group` 里的 `handler` 到一个 `go` 文件 |
   211  
   212  ## 9. go-zero 用户
   213  
   214  go-zero 已被许多公司用于生产部署,接入场景如在线教育、电商业务、游戏、区块链等,目前为止,已使用 go-zero 的公司包括但不限于:
   215  
   216  > 1.  好未来
   217  > 2.  上海晓信信息科技有限公司(晓黑板)
   218  > 3.  上海玉数科技有限公司
   219  > 4.  常州千帆网络科技有限公司
   220  > 5.  上班族科技
   221  > 6.  英雄体育(VSPN)
   222  > 7.  githubmemory
   223  > 8.  释空(上海)品牌策划有限公司(senkoo)
   224  > 9.  鞍山三合众鑫科技有限公司
   225  > 10. 广州星梦工场网络科技有限公司
   226  > 11. 杭州复杂美科技有限公司
   227  > 12. 赛凌科技
   228  > 13. 捞月狗
   229  > 14. 浙江三合通信科技有限公司
   230  > 15. 爱克萨
   231  > 16. 郑州众合互联信息技术有限公司
   232  > 17. 三七游戏
   233  > 18. 成都创道夫科技有限公司
   234  > 19. 联想 Lenovo
   235  > 20. 云犀
   236  > 21. 高盈国际
   237  > 22. 北京中科生活服务有限公司
   238  > 23. Indochat 印尼艾希英
   239  > 24. 数赞
   240  > 25. 量冠科技
   241  > 26. 杭州又拍云科技有限公司
   242  > 27. 深圳市点购电子商务控股股份有限公司
   243  > 28. 深圳市宁克沃德科技有限公司
   244  > 29. 桂林优利特医疗电子有限公司
   245  > 30. 成都智橙互动科技有限公司
   246  > 31. 深圳市班班科技有限公司
   247  > 32. 飞视(苏州)数字技术有限公司
   248  > 33. 上海鲸思智能科技有限公司
   249  > 34. 南宁宸升计算机科技有限公司
   250  > 35. 秦皇岛 2084team
   251  > 36. 天翼云股份有限公司
   252  > 37. 南京速优云信息科技有限公司
   253  > 38. 北京小鸦科技有限公司
   254  > 39. 深圳无边界技术有限公司
   255  > 40. 马鞍山百助网络科技有限公司
   256  > 41. 上海阿莫尔科技有限公司
   257  > 42. 发明者量化
   258  > 43. 济南超级盟网络科技有限公司
   259  > 44. 苏州互盟信息存储技术有限公司
   260  > 45. 成都艾途教育科技集团有限公司
   261  > 46. 上海游族网络
   262  > 47. 深信服
   263  > 48. 中免日上科技互联有限公司
   264  > 49. ECLOUDVALLEY TECHNOLOGY (HK) LIMITED
   265  > 50. 馨科智(深圳)科技有限公司
   266  > 51. 成都松珀科技有限公司
   267  > 52. 亿景智联
   268  > 53. 上海扩博智能技术有限公司
   269  > 54. 一犀科技成都有限公司
   270  > 55. 北京术杰科技有限公司
   271  > 56. 时代脉搏网络科技(云浮市)有限公司
   272  > 57. 店有帮
   273  > 58. 七牛云
   274  > 59. 费芮网络
   275  > 60. 51CTO
   276  > 61. 聿旌科技
   277  > 62. 山东胜软科技股份有限公司
   278  > 63. 上海芯果科技有限公司(好特卖)
   279  > 64. 成都高鹿科技有限公司
   280  > 65. 飞视(苏州)数字技术有限公司
   281  > 66. 上海幻析信息科技有限公司
   282  > 67. 统信软件技术有限公司
   283  > 68. 得物
   284  > 69. 鼎翰文化股份有限公司
   285  > 70. 茶码纹化(云南)科技发展有限公司
   286  > 71. 湖南度思信息技术有限公司
   287  > 72. 深圳圆度
   288  > 73. 武汉沃柒科技有限公司(茄椒)
   289  > 74. 驭势科技
   290  > 75. 叮当跳动
   291  > 76. Keep
   292  > 77. simba innovation
   293  > 78. ZeroCMF
   294  > 79. 安徽寻梦投资发展集团
   295  > 80. 广州腾思信息科技有限公司
   296  
   297  如果贵公司也已使用 go-zero,欢迎在 [登记地址](https://github.com/lingyao2333/mo-zero/issues/602) 登记,仅仅为了推广,不做其它用途。
   298  
   299  ## 10. CNCF 云原生技术全景图
   300  
   301  <p float="left">
   302  <img src="https://landscape.cncf.io/images/left-logo.svg" width="150"/>&nbsp;&nbsp;&nbsp;
   303  <img src="https://landscape.cncf.io/images/right-logo.svg" width="200"/>
   304  </p>
   305  
   306  go-zero 收录在 [CNCF Cloud Native 云原生技术全景图](https://landscape.cncf.io/?selected=go-zero)。
   307  
   308  ## 11. 微信公众号
   309  
   310  `go-zero` 相关文章和视频都会在 `微服务实践` 公众号整理呈现,欢迎扫码关注 👏
   311  
   312  <img src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/zeromicro.jpg" alt="wechat" width="600" />
   313  
   314  ## 12. 微信交流群
   315  
   316  如果文档中未能覆盖的任何疑问,欢迎您在群里提出,我们会尽快答复。
   317  
   318  您可以在群内提出使用中需要改进的地方,我们会考虑合理性并尽快修改。
   319  
   320  如果您发现 **_bug_** 请及时提 **_issue_**,我们会尽快确认并修改。
   321  
   322  加群之前有劳点一下 **_star_**,一个小小的 **_star_** 是作者们回答海量问题的动力!🤝
   323  
   324  <img src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/wechat.jpg" alt="wechat" width="300" />
   325  
   326  ## 13. 赞助一下 👍
   327  
   328  如果觉得项目有帮助,可以请作者喝杯咖啡 🍹
   329  
   330  <img src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/sponsor.png" alt="wechat" width="300" />