-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
## 初始化仓库 | ||
--- | ||
执行命令,初始化一个名为`proxima`的微服务仓库: | ||
|
||
```bash | ||
$ gf init proxima -m | ||
``` | ||
|
||
`GoFrame`升级到最新版本: | ||
```bash | ||
$ cd proxima | ||
gf -up | ||
``` | ||
|
||
删除不必要的示例文件: | ||
```bash | ||
$ rm -rf app/* | ||
``` | ||
|
||
完成后的项目结构: | ||
```text | ||
app | ||
hack | ||
hack.mk | ||
hack-cli.mk | ||
utility | ||
go.mod | ||
go.sum | ||
``` | ||
在`Monorepo`仓库模式下,根目录只提供对项目依赖管理,不存在`main.go`文件。`app`目录下保存各微服务的代码文件,例如`app/user/main.go`,`app/word/main.go`。 | ||
|
||
## 建立数据库 | ||
--- | ||
微服务架构下,每个单独的服务都应当有自己的数据库。所以,我们需要建立两个数据库,名称分别是`user`和`word`。 | ||
|
||
## 安装数据库驱动 | ||
--- | ||
和单体服务一样,也需要安装对应的数据库驱动,这里演示的是`MySQL`: | ||
|
||
```bash | ||
go get -u github.com/gogf/gf/contrib/drivers/mysql/v2 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
本章都是一些基础准备,和单体服务一模一样。 | ||
|
||
## 代码初始化 | ||
--- | ||
`GoFrame`为我们准备好了初始化微服务仓库的命令,执行以下命令,建立名为`user`的服务,并保存在`app`目录下。 | ||
|
||
```bash | ||
$ gf init app/user -a | ||
initializing... | ||
initialization done! | ||
you can now run "cd app/user && gf run main.go" to start your journey, enjoy! | ||
``` | ||
|
||
成功后,会在`app`下建立一个微服务,它其实和单体服务没什么两样,就是缺少了`go.mod`和`go.sum`文件。 | ||
|
||
把初始的示例文件全部删除,留下一个空白的环境,用作后续开发。删除如下目录内的所有文件: | ||
```text | ||
api/* | ||
internal/controller/* | ||
``` | ||
|
||
编辑`cmd`文件,去掉不必要的代码。 | ||
|
||
*app/user/internal/cmd/cmd.go* | ||
```go | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gogf/gf/v2/frame/g" | ||
"github.com/gogf/gf/v2/net/ghttp" | ||
"github.com/gogf/gf/v2/os/gcmd" | ||
) | ||
|
||
var ( | ||
Main = gcmd.Command{ | ||
Name: "main", | ||
Usage: "main", | ||
Brief: "start http server", | ||
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) { | ||
s := g.Server() | ||
s.Group("/", func(group *ghttp.RouterGroup) { | ||
group.Middleware(ghttp.MiddlewareHandlerResponse) | ||
}) | ||
s.Run() | ||
return nil | ||
}, | ||
} | ||
) | ||
``` | ||
|
||
代码完成后,我们就可以进入微服务仓库,和单体服务走一样的流程进行开发。 | ||
|
||
```bash | ||
$ cd app/user | ||
``` | ||
## 生成数据模型 | ||
--- | ||
在`user`下,执行`SQL`语句,建立保存用户数据的表: | ||
```sql | ||
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 | ||
); | ||
``` | ||
|
||
修改配置文件,生成数据模型,这里和单体服务一样。 | ||
|
||
*app/user/hack/config.yaml* | ||
```yaml | ||
gfcli: | ||
gen: | ||
dao: | ||
- link: "mysql:root:12345678@tcp(srv.com:3306)/user" | ||
descriptionTag: true | ||
``` | ||
```bash | ||
$ 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! | ||
``` | ||
|
||
> 注意,应该在微服务仓库下执行`gf gen dao`命令,也就是`app/user`目录下,后续的其他相关操作开发也类似。 | ||
## 引入数据库驱动 | ||
--- | ||
这里和单体服务一样,在`main.go`下引入。 | ||
|
||
*app/user/main.go* | ||
```go | ||
package main | ||
|
||
import ( | ||
_ "github.com/gogf/gf/contrib/drivers/mysql/v2" | ||
) | ||
|
||
func main() { | ||
cmd.Main.Run(gctx.GetInitCtx()) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
本章是一些微服务准备,和单体服务不太一致。 | ||
|
||
## 生成数据模型 | ||
--- | ||
*app/user/hack/config.yaml* | ||
```bash | ||
gfcli: | ||
gen: | ||
dao: | ||
- link: "mysql:root:12345678@tcp(srv.com:3306)/user" | ||
descriptionTag: true | ||
|
||
pbentity: | ||
- link: "mysql:root:12345678@tcp(srv.com:3306)/user" | ||
``` | ||
|
||
```bash | ||
$ gf gen pbentity | ||
generated: D:\project\proxima\app\user\manifest\protobuf\pbentity\users.proto | ||
done! | ||
``` | ||
|
||
### 与 `gen dao` 的差别 | ||
|
||
- `gen dao` 生成的数据文件是`go`文件,主要在微服务内部使用,例如`ORM`操作; | ||
- `gen pbentity`生成的数据文件是`proto`文件,主要用作`gRPC`微服务之间的通讯。 | ||
|
||
## 配置文件 | ||
--- | ||
和`HTTP`服务一样,`gRPC`微服务也需要有自己的配置文件。 | ||
|
||
*app/user/manifest/config/config.yaml* | ||
```go | ||
grpc: | ||
name: "user" # 服务名称 | ||
address: ":32001" # 自定义服务监听地址 | ||
|
||
database: | ||
default: | ||
link: "mysql:root:12345678@tcp(srv.com:3306)/user" | ||
debug: true | ||
``` | ||
|
||
`gprc`字段定义了两个字段,微服务名称和监听端口,微服务名称将用作服务发现。这两个是必要的,完整配置见[配置模板](https://goframe.org/docs/micro-service/config#%E9%85%8D%E7%BD%AE%E6%A8%A1%E6%9D%BF)。 |