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

     1  # go-zero
     2  
     3  <p align="center">
     4  <img align="center" width="150px" src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/go-zero.png">
     5  </p>
     6  
     7  go-zero is a web and rpc framework with lots of builtin engineering practices. Itโ€™s born to ensure the stability of the busy services with resilience design and has been serving sites with tens of millions of users for years.
     8  
     9  <div align=center>
    10  
    11  [![Go](https://github.com/lingyao2333/mo-zero/workflows/Go/badge.svg?branch=master)](https://github.com/lingyao2333/mo-zero/actions)
    12  [![codecov](https://codecov.io/gh/lingyao2333/mo-zero/branch/master/graph/badge.svg)](https://codecov.io/gh/lingyao2333/mo-zero)
    13  [![Go Report Card](https://goreportcard.com/badge/github.com/lingyao2333/mo-zero)](https://goreportcard.com/report/github.com/lingyao2333/mo-zero)
    14  [![Release](https://img.shields.io/github/v/release/lingyao2333/mo-zero.svg?style=flat-square)](https://github.com/lingyao2333/mo-zero)
    15  [![Go Reference](https://pkg.go.dev/badge/github.com/lingyao2333/mo-zero.svg)](https://pkg.go.dev/github.com/lingyao2333/mo-zero)
    16  [![Awesome Go](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go)
    17  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
    18  [![Discord](https://img.shields.io/discord/794530774463414292?label=chat&logo=discord)](https://discord.gg/4JQvC5A4Fe)
    19  
    20  </div>
    21  
    22  ## ๐Ÿคทโ€ What is go-zero?
    23  
    24  English | [็ฎ€ไฝ“ไธญๆ–‡](readme-cn.md)
    25  
    26  <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>
    27  
    28  go-zero (listed in CNCF Landscape: [https://landscape.cncf.io/?selected=go-zero](https://landscape.cncf.io/?selected=go-zero)) is a web and rpc framework with lots of builtin engineering practices. Itโ€™s born to ensure the stability of the busy services with resilience design and has been serving sites with tens of millions of users for years.
    29  
    30  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`.
    31  
    32  #### Advantages of go-zero:
    33  
    34  - improve the stability of the services with tens of millions of daily active users
    35  - builtin chained timeout control, concurrency control, rate limit, adaptive circuit breaker, adaptive load shedding, even no configuration needed
    36  - builtin middlewares also can be integrated into your frameworks
    37  - simple API syntax, one command to generate a couple of different languages
    38  - auto validate the request parameters from clients
    39  - plenty of builtin microservice management and concurrent toolkits
    40  
    41  <img src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/architecture-en.png" alt="Architecture" width="1500" />
    42  
    43  ## Backgrounds of go-zero
    44  
    45  At the beginning of 2018, we decided to re-design our system, from monolithic architecture with Java+MongoDB to microservice architecture. After research and comparison, we chose to:
    46  
    47  - Golang based
    48    - great performance
    49    - simple syntax
    50    - proven engineering efficiency
    51    - extreme deployment experience
    52    - less server resource consumption
    53  - Self-designed microservice architecture
    54    - I have rich experience in designing microservice architectures
    55    - easy to locate the problems
    56    - easy to extend the features
    57  
    58  ## Design considerations on go-zero
    59  
    60  By designing the microservice architecture, we expected to ensure stability, as well as productivity. And from just the beginning, we have the following design principles:
    61  
    62  - keep it simple
    63  - high availability
    64  - stable on high concurrency
    65  - easy to extend
    66  - resilience design, failure-oriented programming
    67  - try best to be friendly to the business logic development, encapsulate the complexity
    68  - one thing, one way
    69  
    70  After almost half a year, we finished the transfer from a monolithic system to microservice system and deployed on August 2018. The new system guaranteed business growth and system stability.
    71  
    72  ## The implementation and features of go-zero
    73  
    74  go-zero is a web and rpc framework that integrates lots of engineering practices. The features are mainly listed below:
    75  
    76  - powerful tool included, less code to write
    77  - simple interfaces
    78  - fully compatible with net/http
    79  - middlewares are supported, easy to extend
    80  - high performance
    81  - failure-oriented programming, resilience design
    82  - builtin service discovery, load balancing
    83  - builtin concurrency control, adaptive circuit breaker, adaptive load shedding, auto-trigger, auto recover
    84  - auto validation of API request parameters
    85  - chained timeout control
    86  - auto management of data caching
    87  - call tracing, metrics, and monitoring
    88  - high concurrency protected
    89  
    90  As below, go-zero protects the system with a couple of layers and mechanisms:
    91  
    92  ![Resilience](https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/resilience-en.png)
    93  
    94  ## The simplified architecture that we use with go-zero
    95  
    96  <img width="1067" alt="image" src="https://user-images.githubusercontent.com/1918356/171880372-5010d846-e8b1-4942-8fe2-e2bbb584f762.png">
    97  
    98  ## Installation
    99  
   100  Run the following command under your project:
   101  
   102  ```shell
   103  go get -u github.com/lingyao2333/mo-zero
   104  ```
   105  
   106  ## Upgrade
   107  
   108  To upgrade from versions eariler than v1.3.0, run the following commands.
   109  
   110  ```shell
   111  go install github.com/lingyao2333/mo-zero/tools/goctl@latest
   112  ```
   113  
   114  ```shell
   115  goctl migrate โ€”verbose โ€”version v1.4.0
   116  ```
   117  
   118  ## Quick Start
   119  
   120  1. full examples can be checked out from below:
   121  
   122     [Rapid development of microservice systems](https://github.com/zeromicro/zero-doc/blob/main/doc/shorturl-en.md)
   123  
   124     [Rapid development of microservice systems - multiple RPCs](https://github.com/zeromicro/zero-doc/blob/main/docs/zero/bookstore-en.md)
   125  
   126  2. install goctl
   127  
   128     `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 productivity, and make our lives easier.
   129  
   130     ```shell
   131     # for Go 1.15 and earlier
   132     GO111MODULE=on go get -u github.com/lingyao2333/mo-zero/tools/goctl@latest
   133  
   134     # for Go 1.16 and later
   135     go install github.com/lingyao2333/mo-zero/tools/goctl@latest
   136  
   137     # For Mac
   138     brew install goctl
   139  
   140     # docker for amd64 architecture
   141     docker pull kevinwan/goctl
   142     # run goctl like
   143     docker run --rm -it -v `pwd`:/app kevinwan/goctl goctl --help
   144  
   145     # docker for arm64 (M1) architecture
   146     docker pull kevinwan/goctl:latest-arm64
   147     # run goctl like
   148     docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 goctl --help
   149     ```
   150  
   151     make sure goctl is executable.
   152  
   153  3. create the API file, like greet.api, you can install the plugin of goctl in vs code, api syntax is supported.
   154  
   155     ```go
   156     type (
   157       Request {
   158         Name string `path:"name,options=you|me"` // parameters are auto validated
   159       }
   160  
   161       Response {
   162         Message string `json:"message"`
   163       }
   164     )
   165  
   166     service greet-api {
   167       @handler GreetHandler
   168       get /greet/from/:name(Request) returns (Response)
   169     }
   170     ```
   171  
   172     the .api files also can be generated by goctl, like below:
   173  
   174     ```shell
   175     goctl api -o greet.api
   176     ```
   177  
   178  4. generate the go server-side code
   179  
   180     ```shell
   181     goctl api go -api greet.api -dir greet
   182     ```
   183  
   184     the generated files look like:
   185  
   186     ```Plain Text
   187     โ”œโ”€โ”€ greet
   188     โ”‚ย ย  โ”œโ”€โ”€ etc
   189     โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ greet-api.yaml        // configuration file
   190     โ”‚ย ย  โ”œโ”€โ”€ greet.go                  // main file
   191     โ”‚ย ย  โ””โ”€โ”€ internal
   192     โ”‚ย ย      โ”œโ”€โ”€ config
   193     โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ config.go         // configuration definition
   194     โ”‚ย ย      โ”œโ”€โ”€ handler
   195     โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ greethandler.go   // get/put/post/delete routes are defined here
   196     โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ routes.go         // routes list
   197     โ”‚ย ย      โ”œโ”€โ”€ logic
   198     โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ greetlogic.go     // request logic can be written here
   199     โ”‚ย ย      โ”œโ”€โ”€ svc
   200     โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ servicecontext.go // service context, mysql/redis can be passed in here
   201     โ”‚ย ย      โ””โ”€โ”€ types
   202     โ”‚ย ย          โ””โ”€โ”€ types.go          // request/response defined here
   203     โ””โ”€โ”€ greet.api                     // api description file
   204     ```
   205  
   206     the generated code can be run directly:
   207  
   208     ```shell
   209     cd greet
   210     go mod init
   211     go mod tidy
   212     go run greet.go -f etc/greet-api.yaml
   213     ```
   214  
   215     by default, itโ€™s listening on port 8888, while it can be changed in the configuration file.
   216  
   217     you can check it by curl:
   218  
   219     ```shell
   220     curl -i http://localhost:8888/greet/from/you
   221     ```
   222  
   223     the response looks like below:
   224  
   225     ```http
   226     HTTP/1.1 200 OK
   227     Date: Sun, 30 Aug 2020 15:32:35 GMT
   228     Content-Length: 0
   229     ```
   230  
   231  5. Write the business logic code
   232  
   233     - the dependencies can be passed into the logic within servicecontext.go, like mysql, reds, etc.
   234     - add the logic code in a logic package according to .api file
   235  
   236  6. Generate code like Java, TypeScript, Dart, JavaScript, etc. just from the api file
   237  
   238     ```shell
   239     goctl api java -api greet.api -dir greet
   240     goctl api dart -api greet.api -dir greet
   241     ...
   242     ```
   243  
   244  ## Benchmark
   245  
   246  ![benchmark](https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/benchmark.png)
   247  
   248  [Checkout the test code](https://github.com/smallnest/go-web-framework-benchmark)
   249  
   250  ## Documents
   251  
   252  - [Documents](https://go-zero.dev/)
   253  - [Rapid development of microservice systems](https://github.com/zeromicro/zero-doc/blob/main/doc/shorturl-en.md)
   254  - [Rapid development of microservice systems - multiple RPCs](https://github.com/zeromicro/zero-doc/blob/main/docs/zero/bookstore-en.md)
   255  - [Examples](https://github.com/zeromicro/zero-examples)
   256  
   257  ## Chat group
   258  
   259  Join the chat via https://discord.gg/4JQvC5A4Fe
   260  
   261  ## Cloud Native Landscape
   262  
   263  <p float="left">
   264  <img src="https://landscape.cncf.io/images/left-logo.svg" width="150"/>&nbsp;&nbsp;&nbsp;
   265  <img src="https://landscape.cncf.io/images/right-logo.svg" width="200"/>
   266  </p>
   267  
   268  go-zero enlisted in the [CNCF Cloud Native Landscape](https://landscape.cncf.io/?selected=go-zero).
   269  
   270  ## Give a Star! โญ
   271  
   272  If you like or are using this project to learn or start your solution, please give it a star. Thanks!
   273  
   274  [![Star History Chart](https://api.star-history.com/svg?repos=lingyao2333/mo-zero&type=Date)](#go-zero)
   275  
   276  ## Buy me a coffee
   277  
   278  <a href="https://www.buymeacoffee.com/kevwan" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>