Most aspects of microservice development are similar to monolithic services, and in many cases, even simpler. This chapter covers some basic preparations that should be familiar to everyone.
Code Initialization
GoFrame provides a command for initializing microservice repositories. Execute the following command to create a service named user in the app directory.
$ gf init app/user -a
initializing...
initialization done!
you can now run "cd app/user && gf run main.go" to start your journey, enjoy!
After successful initialization, a microservice will be created in the app directory. It's similar to a monolithic service, except that it lacks the go.mod and go.sum files.
Delete the following files to start with a clean environment:
app/user/api/*
app/user/internal/controller/*
app/user/internal/cmd/cmd.go
After completing these steps, enter the microservice repository to begin development.
$ cd app/user
Database Setup
Creating Tables
In the user database, execute the following SQL statement to create a table for storing user data:
CREATE TABLE users (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password CHAR(32) NOT NULL,
email VARCHAR(100),
created_at DATETIME,
updated_at DATETIME
);
Generating DAO Models
app/user/hack/config.yaml
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(srv.com:3306)/user"
descriptionTag: true
$ gf gen dao
generated: D:\project\proxima\app\user\internal\dao\users.go
generated: D:\project\proxima\app\user\internal\dao\internal\users.go
generated: D:\project\proxima\app\user\internal\model\do\users.go
generated: D:\project\proxima\app\user\internal\model\entity\users.go
done!
Note: Execute the
gf gen daocommand in the microservice repository (i.e., theapp/userdirectory). Be careful not to get this wrong. Similar rules apply to other related operations.
Generating Protocol Buffer Entity Models
app/user/hack/config.yaml
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(srv.com:3306)/user"
descriptionTag: true
pbentity:
- link: "mysql:root:12345678@tcp(srv.com:3306)/user"
$ gf gen pbentity
generated: D:\project\proxima\app\user\manifest\protobuf\pbentity\users.proto
done!
Differences Between gen dao and gen pbentity
gen daogenerates Go files primarily used within the microservice, such as for ORM operationsgen pbentitygenerates Protocol Buffer files mainly used for gRPC communication between microservices