DEV Community

zhuyasen
zhuyasen

Posted on

Example 2, Automatically generate a complete grpc+gorm+redis+CRUD microservice project without writing a line of Go code

The generated CRUD API interface code is commonly used in web services, but not much in grpc services. This article introduces the use of the code generation tool Sponge to seamlessly integrate the generated CRUD method code with the generated microservice (grpc) code without adding or modifying any code or writing proto file description information. It also includes features such as paging conditional queries and caching.

Dependencies

  • Import sql into mysql services.
  • Installation tool sponge.

After installing the tool sponge, execute the command to open the UI interface:

sponge run
Enter fullscreen mode Exit fullscreen mode

Quickly create a microservice project

Enter the Sponge UI interface, click on the left menu bar 【SQL】→【Create microservice project】, fill in some parameters to generate a complete microservice project code.

Image description

The microservice code is mainly composed of commonly used libraries such as grpc, gorm, go-redis, and also includes grpc client CRUD test code, common service governance function code, build deployment scripts, etc.

Switch to the user directory and run the command:

# Generate code
make proto

# Compile and start grpc service
make run
Enter fullscreen mode Exit fullscreen mode

Use goland IDE to open user service code, enter the internal/service directory, open the teacher_client_test.go file, you can test CRUD methods here, similar to testing CRUD interfaces in swagger interface. Fill in parameters before testing and click the green button to test.

Image description

Batch add CRUD code to grpc service

Enter the Sponge UI interface, click on the left menu bar 【Public】→【Generate service CRUD code】, select any number of tables to generate code, then move the generated CRUD code to the grpc service directory to complete batch addition of CURD interfaces in microservices without changing any code.

Image description

Switch to user service directory and run command:

# Generate code
make proto

# Compile and start user service
make run
Enter fullscreen mode Exit fullscreen mode

Use goland IDE, enter internal/service directory, open teach_client_test.go and course_client_test.go files to test CRUD methods.

This is the complete grpc service code generated from the above steps micro-grpc-CRUD

See the detailed development microservice documentation https://go-sponge.com/microservice-development-mysql

Top comments (0)