搞定了第一个微服务后,第二个微服务开发起来自然轻车熟路。
代码初始化
执行以下命令,建立名为word
的服务,同样保存在app
目录下。
$ gf init app/word -a
initializing...
initialization done!
you can now run "cd app/word && gf run main.go" to start your journey, enjoy!
如法炮制,删除下列文件,留下一个空白的环境。
app/word/api/*
app/word/internal/controller/*
app/word/internal/cmd/cmd.go
进入微服务仓库,开始正式开发。
$ cd app/word
生成数据模型
建立数据表
在word
数据库下,执行SQL
语句,建立保存用户数据的表:
CREATE TABLE `words` (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
uid INT UNSIGNED NOT NULL,
word VARCHAR ( 255 ) NOT NULL,
definition TEXT,
example_sentence TEXT,
chinese_translation VARCHAR ( 255 ),
pronunciation VARCHAR ( 255 ),
created_at DATETIME,
updated_at DATETIME
);
生成dao模型
app/user/hack/config.yaml
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(srv.com:3306)/word"
descriptionTag: true
$ gf gen dao
generated: D:\project\proxima\app\word\internal\dao\words.go
generated: D:\project\proxima\app\word\internal\dao\internal\words.go
generated: D:\project\proxima\app\word\internal\model\do\words.go
generated: D:\project\proxima\app\word\internal\model\entity\words.go
done!
生成pbentity模型
app/user/hack/config.yaml
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(srv.com:3306)/word"
descriptionTag: true
pbentity:
- link: "mysql:root:12345678@tcp(srv.com:3306)/word"
$ gf gen pbentity
generated: D:\project\proxima\app\word\manifest\protobuf\pbentity\words.proto
done!