# Rapid development of microservices - multiple RPCs
## 0. Why building microservices are so difficult?
English | [简体中文](bookstore.md)
## 0. Why building microservices are so difficult
To build a well working microservice, we need lots of knowledges from different aspects.
@ -25,7 +25,7 @@ As well, we always adhere to the idea that **prefer tools over conventions and d
Let’s take the shorturl microservice as a quick example to demonstrate how to quickly create microservices by using [go-zero](https://github.com/tal-tech/go-zero). After finishing this tutorial, you’ll find that it’s so easy to write microservices!
## 1. What is a bookstore service?
## 1. What is a bookstore service
For simplicity, the bookstore service only contains two functionalities, adding books and quering prices.
@ -71,7 +71,7 @@ And now, let’s walk through the complete flow of quickly create a microservice
* use goctl to generate `api/bookstore.api`
```
```Plain Text
goctl api -o bookstore.api
```
@ -128,7 +128,7 @@ And now, let’s walk through the complete flow of quickly create a microservice
the generated file structure looks like:
```
```Plain Text
api
├── bookstore.api // api definition
├── bookstore.go // main entrance
@ -188,13 +188,13 @@ And now, let’s walk through the complete flow of quickly create a microservice
* under directory `rpc/add` create `add.proto` file
```shell
goctl rpc template -o add.proto
goctl rpc template -o add.proto
```
edit the file and make the code looks like:
```protobuf
syntax = "proto3";
syntax = "proto3";
package add;
@ -220,7 +220,7 @@ syntax = "proto3";
the generated file structure looks like:
```
```Plain Text
rpc/add
├── add.go // rpc main entrance
├── add.proto // rpc definition
@ -242,7 +242,6 @@ syntax = "proto3";
└── pb
└── add.pb.go
```
just run it, looks like:
@ -258,13 +257,13 @@ you can change the listening port in file `etc/add.yaml`.
* under directory `rpc/check` create `check.proto` file
```shell
goctl rpc template -o check.proto
goctl rpc template -o check.proto
```
edit the file and make the code looks like:
```protobuf
syntax = "proto3";
syntax = "proto3";
package check;
@ -290,7 +289,7 @@ syntax = "proto3";
the generated file structure looks like:
```
```Plain Text
rpc/check
├── check.go // rpc main entrance
├── check.proto // rpc definition
@ -315,7 +314,7 @@ syntax = "proto3";
you can change the listening port in `etc/check.yaml`.
we need to change the port in `etc/check.yaml` to `8081`, because `8080` is used by `add` service.
we need to change the port in `etc/check.yaml` to `8081`, because `8080` is used by `add` service.
just run it, looks like:
@ -444,7 +443,7 @@ Till now, we’ve done the modification of API Gateway. All the manually added c
source book.sql;
```
* under the directory `rpc/model execute the following command to genrate CRUD+cache code, `-c` means using `redis cache`
* under the directory `rpc/model` execute the following command to genrate CRUD+cache code, `-c` means using `redis cache`
```shell
goctl model mysql ddl -c -src book.sql -dir .
@ -454,7 +453,7 @@ Till now, we’ve done the modification of API Gateway. All the manually added c
the generated file structure looks like:
```
```Plain Text
rpc/model
├── bookstore.sql
├── bookstoremodel.go // CRUD+cache code
@ -614,4 +613,4 @@ We not only keep the framework simple, but also encapsulate the complexity into
For the generated code by goctl, lots of microservice components are included, like concurrency control, adaptive circuit breaker, adaptive load shedding, auto cache control etc. And it’s easy to deal with the busy sites.
If you have any ideas that can help us to improve the productivity, tell me any time! 👏
If you have any ideas that can help us to improve the productivity, tell me any time! 👏
## 0. Why building microservices are so difficult?
English | [简体中文](shorturl.md)
## 0. Why building microservices are so difficult
To build a well working microservice, we need lots of knowledges from different aspects.
@ -25,7 +25,7 @@ As well, we always adhere to the idea that **prefer tools over conventions and d
Let’s take the shorturl microservice as a quick example to demonstrate how to quickly create microservices by using [go-zero](https://github.com/tal-tech/go-zero). After finishing this tutorial, you’ll find that it’s so easy to write microservices!
## 1. What is a shorturl service?
## 1. What is a shorturl service
A shorturl service is that it converts a long url into a short one, by well designed algorithms.
@ -129,7 +129,7 @@ And now, let’s walk through the complete flow of quickly create a microservice
the generated file structure looks like:
```
```Plain Text
.
├── api
│ ├── etc
@ -232,7 +232,7 @@ And now, let’s walk through the complete flow of quickly create a microservice
the generated file structure looks like:
```
```Plain Text
rpc/transform
├── etc
│ └── transform.yaml // configuration file
@ -375,7 +375,7 @@ Till now, we’ve done the modification of API Gateway. All the manually added c
source shorturl.sql;
```
* under the directory `rpc/transform/model execute the following command to genrate CRUD+cache code, `-c` means using `redis cache`
* under the directory `rpc/transform/model` execute the following command to genrate CRUD+cache code, `-c` means using `redis cache`
```shell
goctl model mysql ddl -c -src shorturl.sql -dir .
@ -385,7 +385,7 @@ Till now, we’ve done the modification of API Gateway. All the manually added c
the generated file structure looks like:
```
```Plain Text
rpc/transform/model
├── shorturl.sql
├── shorturlmodel.go // CRUD+cache code
@ -533,4 +533,3 @@ We not only keep the framework simple, but also encapsulate the complexity into
For the generated code by goctl, lots of microservice components are included, like concurrency control, adaptive circuit breaker, adaptive load shedding, auto cache control etc. And it’s easy to deal with the busy sites.
If you have any ideas that can help us to improve the productivity, tell me any time! 👏
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.
@ -99,7 +99,7 @@ go get -u github.com/tal-tech/go-zero
1. install goctl
`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.
`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
GO111MODULE=on go get -u github.com/tal-tech/go-zero/tools/goctl
@ -113,11 +113,11 @@ go get -u github.com/tal-tech/go-zero
type Request struct {
Name string `path:"name,options=you|me"` // parameters are auto validated
}
type Response struct {
Message string `json:"message"`
}
service greet-api {
@server(
handler: GreetHandler
@ -140,7 +140,7 @@ go get -u github.com/tal-tech/go-zero
the generated files look like:
```
```Plain Text
├── greet
│ ├── etc
│ │ └── greet-api.yaml // configuration file
@ -159,6 +159,7 @@ go get -u github.com/tal-tech/go-zero
│ └── types.go // request/response defined here
└── greet.api // api description file
```
the generated code can be run directly:
```shell
@ -184,8 +185,9 @@ go get -u github.com/tal-tech/go-zero
4. Write the business logic code
* 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
* 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
5. Generate code like Java, TypeScript, Dart, JavaScript etc. just from the api file