# Rapid development of microservices - multiple RPCs
# 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.
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!
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.
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`
* use goctl to generate `api/bookstore.api`
```
```Plain Text
goctl api -o bookstore.api
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:
the generated file structure looks like:
```
```Plain Text
api
api
├── bookstore.api // api definition
├── bookstore.api // api definition
├── bookstore.go // main entrance
├── 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
* under directory `rpc/add` create `add.proto` file
```shell
```shell
goctl rpc template -o add.proto
goctl rpc template -o add.proto
```
```
edit the file and make the code looks like:
edit the file and make the code looks like:
```protobuf
```protobuf
syntax = "proto3";
syntax = "proto3";
package add;
package add;
@ -220,7 +220,7 @@ syntax = "proto3";
the generated file structure looks like:
the generated file structure looks like:
```
```Plain Text
rpc/add
rpc/add
├── add.go // rpc main entrance
├── add.go // rpc main entrance
├── add.proto // rpc definition
├── add.proto // rpc definition
@ -242,7 +242,6 @@ syntax = "proto3";
└── pb
└── pb
└── add.pb.go
└── add.pb.go
```
```
just run it, looks like:
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
* under directory `rpc/check` create `check.proto` file
```shell
```shell
goctl rpc template -o check.proto
goctl rpc template -o check.proto
```
```
edit the file and make the code looks like:
edit the file and make the code looks like:
```protobuf
```protobuf
syntax = "proto3";
syntax = "proto3";
package check;
package check;
@ -290,7 +289,7 @@ syntax = "proto3";
the generated file structure looks like:
the generated file structure looks like:
```
```Plain Text
rpc/check
rpc/check
├── check.go // rpc main entrance
├── check.go // rpc main entrance
├── check.proto // rpc definition
├── check.proto // rpc definition
@ -315,7 +314,7 @@ syntax = "proto3";
you can change the listening port in `etc/check.yaml`.
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:
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;
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
```shell
goctl model mysql ddl -c -src book.sql -dir .
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:
the generated file structure looks like:
```
```Plain Text
rpc/model
rpc/model
├── bookstore.sql
├── bookstore.sql
├── bookstoremodel.go // CRUD+cache code
├── 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.
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.
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!
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.
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:
the generated file structure looks like:
```
```Plain Text
.
.
├── api
├── api
│ ├── etc
│ ├── 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:
the generated file structure looks like:
```
```Plain Text
rpc/transform
rpc/transform
├── etc
├── etc
│ └── transform.yaml // configuration file
│ └── 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;
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
```shell
goctl model mysql ddl -c -src shorturl.sql -dir .
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:
the generated file structure looks like:
```
```Plain Text
rpc/transform/model
rpc/transform/model
├── shorturl.sql
├── shorturl.sql
├── shorturlmodel.go // CRUD+cache code
├── 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.
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! 👏
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 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
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
```shell
GO111MODULE=on go get -u github.com/tal-tech/go-zero/tools/goctl
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 {
type Request struct {
Name string `path:"name,options=you|me"` // parameters are auto validated
Name string `path:"name,options=you|me"` // parameters are auto validated
}
}
type Response struct {
type Response struct {
Message string `json:"message"`
Message string `json:"message"`
}
}
service greet-api {
service greet-api {
@server(
@server(
handler: GreetHandler
handler: GreetHandler
@ -140,7 +140,7 @@ go get -u github.com/tal-tech/go-zero
the generated files look like:
the generated files look like:
```
```Plain Text
├── greet
├── greet
│ ├── etc
│ ├── etc
│ │ └── greet-api.yaml // configuration file
│ │ └── greet-api.yaml // configuration file
@ -159,6 +159,7 @@ go get -u github.com/tal-tech/go-zero
│ └── types.go // request/response defined here
│ └── types.go // request/response defined here
└── greet.api // api description file
└── greet.api // api description file
```
```
the generated code can be run directly:
the generated code can be run directly:
```shell
```shell
@ -184,8 +185,9 @@ go get -u github.com/tal-tech/go-zero
4. Write the business logic code
4. Write the business logic code
* the dependencies can be passed into the logic within servicecontext.go, like mysql, reds etc.
* 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
* 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
5. Generate code like Java, TypeScript, Dart, JavaScript etc. just from the api file