Introduction

The project directory for microservices development adopts a unified framework directory structure. For details, please refer to the section: Project Structure🔥

Here we illustrate using the project https://github.com/gogf/gf-demo-grpc as an example.

Protocol Files

Project Structure - 图1 Protocol files are defined in the manifest/protobuf directory. The directory path rule for protocol files is: module name/version number/xxx.proto. The version number is managed in a form similar to v1/v2 to facilitate maintaining interface compatibility.

Data table structures involved are stored in the manifest/pbentity directory as protobuf files generated by the framework development tools.

Interface Files

Interface documents generated by compiling proto protocol files are stored in a unified api directory.

Development Tools

Generate Data Table Structure

We can automatically generate corresponding protobuf protocol files based on the database table structure using the gf gen pbentity / make pbentity command. For details on the command, please refer to the section: DB Table To Protobuf

Compile proto Protocol Files

The framework provides the gf gen pb / make pb command to automatically compile proto protocol files and generate corresponding interface and controller files.

Service Startup

Service startup is still maintained through the cmd directory, for example: https://github.com/gogf/gf-demo-grpc/blob/main/internal/cmd/cmd.go

Interface Implementation and Registration

Controllers are used for the specific implementation of interfaces defined by proto. Controllers can be automatically generated and can automatically generate Register methods to register specific implementations with the service object.

Registration method:

Project Structure - 图2

Startup registration:

Project Structure - 图3

Tag Injection and Data Validation

Automatic Tag Injection

When compiling proto files using the gf gen pb/make pb command, automatic tag injection is supported. You only need to write comments in the proto file, and these comments will automatically be embedded as dc tags into the struct properties. If a comment rule is in the form xxx:yyy, it will automatically generate the xxx tag. For example:

Project Structure - 图4

Project Structure - 图5

Note that in the GRPC protocol, since both input and output use struct forms, the default value feature of HTTP services cannot be implemented.

Data Validation Plugin

The data validation plugin will automatically validate requests based on rules set in the tags, and it needs to be manually introduced on the server side in the form of interceptors:

Project Structure - 图6