RPC Service
Warning
Must know go-zero's RPC command. RPC Command RPC Service
Refer to the Example project to generate it again, confirm that the generated file is consistent with the Example project, and the Example project has complete commands.
RPC Responsibilities
In Simple Admin, RPC is mainly used to obtain data and provide extended functions. It mainly has the following responsibilities:
- Interact with the database to obtain the required data, such as PostgreSql
- Interact with data sources to obtain data, such as ES
- Provide additional functions for API layer calls, such as sending text messages and emails
Multiple different APIs can access the same RPC to call its functions.
Create RPC project
goctls rpc new example -e -m github.com/suyuan32/simple-admin-example-rpc -p 8080 -dWarning
The project name only supports lowercase and camelcase and must be not contains rpc words.
Important
Goctls implements the function of splitting proto files. Adding the -d parameter can split the proto files for better management and grouping. It also supports using comments to group and generate RPC interfaces into different folders—simply add the comment // group: xxx to the rpc interface.
// group: menu
rpc createMenu (MenuInfo) returns (BaseIDResp);rpc new parameters
| Parameter | Must | Default | Introduction | Usage |
|---|---|---|---|---|
| ent | No | false | Whether to use Ent | true means use |
| i18n | No | false | Whether to use i18n | true means use |
| module_name | No | Module name in go.mod | If your project will be used by other project, you should set as above which is a github repository. If it is empty, the module will be the same as project name. | |
| port | No | 9100 | Port number | The service port |
| desc | No | false | Whether to split the proto file into the desc folder | true will generate the desc folder |
| style | No | go_zero | The format of the file name. | snake case format is go_zero. |
** More parameters please check goctls rpc new --help **
$ goctls rpc new --help
Generate rpc demo service
Usage:
goctl rpc new [flags]
Flags:
--branch string The branch of the remote repo, it does work with --remote
-d, --desc Whether to create desc folder for splitting proto files
-e, --ent Whether use Ent in project
-h, --help help for new
--home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
-i, --i18n Whether to use i18n
--idea For idea plugin [optional]
-m, --module_name string The module name in go.mod. e.g. github.com/suyuan32/simple-admin-core
-p, --port int The service port exposed (default 9110)
--remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
-s, --style string The file naming format, see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md] (default "go_zero")
-v, --verbose Enable log outputFile structure
├── desc proto file directory
├── ent ent file directory
│ ├── enttest
│ ├── hook
│ ├── migrate
│ ├── predicate
│ ├── runtime
│ ├── schema ent schema declaration directory
│ └── template
├── etc configuration file directory
├── example grpc and types directory
├── exampleclient client directory
└── internal
├──config
├── logic logic code directory
│ ├── base
├── server
└── svc global service_context directory
└── utils tools such as ent error handler and ent transaction functionGenerate CRUD logic codes by Ent schema
# Student example
goctls rpc ent --schema=./ent/schema --style=go_zero --multiple=false --service_name=Example --output=./ --model=Student --group=student --proto_out=./desc/student.proto
make gen-rpcrpc ent parameters
| Parameters | Must | Default | Introduction | Usage |
|---|---|---|---|---|
| schema | Yes | Schema folder | Input the relative path of Ent schema | |
| style | No | go_zero | File name format | The go_zero means snack format |
| service_name | Yes | service name | The same as the service name in the proto file | |
| project_name | Yes | project name | Same as the name you create project, same as main file name, needs to be set in multiple mode, single service is same as service name by default | |
| search_key_num | No | 300 | The number of search fields in the list (default is 300). | Only string type fields can be automatically generated. |
| output | Yes | Output path | The output path,it can be relative path. It should target to the root path of project. | |
| model | Yes | Model name | The model name for generating e.g. User, if it is "all", generate codes for all models in schema directory | |
| group | Yes | Group Name | The group name is used to separate logic code | |
| multiple | No | false | Multiple Service | If your proto file contains multiple service, you should set true |
| proto_out | No | Proto file output directory | If it is empty, the data will be generated to the proto file in the root directory of the project, otherwise it will be generated in the specified path desc, such as ./desc/student.proto, note that the folder storing proto must be desc, and there can be sub-files inside folder | |
| proto_field_style | no | go_zero | proto field naming format | default is underscore |
| i18n | No | false | Whether to use i18n | true means use |
| import_prefix | No | The path prefix of import | Import paths' prefix is only used when the service in sub folder, such as core service's api and rpc | |
| overwrite | No | false | Whether it covers the generated file | true will cover all generated files |
| split_time_field | No | Split time fields in list request | Change time fields in list request to begin and end |
More parameters please check goctls rpc ent --help
$ goctls rpc ent --help
Generate CRUD template codes by Ent
Usage:
goctls rpc ent [flags]
Flags:
-g, --group string The group name for logic. e.g. user
-h, --help help for ent
-i, --i18n Whether to use i18n
-x, --import_prefix string Import paths' prefix is only used when the service in sub folder, such as core service's api and rpc
-m, --model string The model name for generating e.g. user, if it is "all", generate codes for all models in schema directory
--multiple Generated in multiple rpc service mode
-o, --output string The output path
-w, --overwrite Whether to overwrite the files, it will overwrite all generated files
-p, --project_name string The project name
-f, --proto_field_style string The proto field style (default "go_zero")
-t, --proto_out string The output proto file path
-c, --schema string The schema path of the Ent
-k, --search_key_num int The max number of search keys (default 300)
-r, --service_name string The service name
-s, --style string The file name format style (default "go_zero")
--split_time_field Whether to change the time request in the list request to "Begin" and "End" range requestImportant
The shortcut command make gen-rpc-ent-logic model={modelName} group={groupName} means to generate the code whose schema is {modelName}, and {groupName} is the group name. Note that the first letter of modelName needs to be capitalized. Be consistent with the struct name in the schema, use make gen-rpc-ent-logic model=all group=all to generate all CRUD codes.
# Generate the Student structure in the schema
make gen-rpc-ent-logic model=Student group=student
make gen-rpc
# You may need to run
go mod tidy
You can see CRUD code !