feat: docker mysql and redis

This commit is contained in:
zzw
2026-08-06 13:43:52 +08:00
parent 58242dee8d
commit c37cedc664
26 changed files with 1349 additions and 129 deletions
+178 -55
View File
@@ -29,7 +29,7 @@ mkdir proto
mv order.proto proto/
```
### 4. 配置 Zero 兼容 Nacos
### 3. Zero 兼容 Nacos 等配置
编辑 `order/internal/config/config.go`:
@@ -40,7 +40,10 @@ import "github.com/zeromicro/go-zero/zrpc"
type Config struct {
zrpc.RpcServerConf
Nacos NacosConf
Nacos NacosConf
Mysql MysqlConf
BizRedis RedisConf
AppLog LogConf
}
type NacosConf struct {
@@ -49,22 +52,69 @@ type NacosConf struct {
Group string `json:",optional"`
RegisterIP string `json:",optional"`
}
type MysqlConf struct {
Host string
Port int
User string
Password string
Database string
Charset string `json:",default=utf8mb4"`
Prefix string `json:",optional"`
ReadHost string `json:",optional"`
ReadPort int `json:",optional"`
ReadUser string `json:",optional"`
ReadPassword string `json:",optional"`
ReadDatabase string `json:",optional"`
}
type RedisConf struct {
Host string
Port int `json:",default=6379"`
Password string `json:",optional"`
DB int `json:",optional"`
}
type LogConf struct {
Path string `json:",default=logs"`
InfoFile string `json:",default=info.log"`
ErrorFile string `json:",default=error.log"`
FatalFile string `json:",default=fatal.log"`
MaxSize int `json:",default=100"`
MaxBackups int `json:",default=10"`
MaxAge int `json:",default=30"`
}
```
### 5. 配置服务名与监听端口
### 4. 配置服务名与监听端口
`order/etc/order.yaml`:
编辑 `order/etc/order.yaml`:
```yaml
Name: order-service # 注册到 Nacos 的 serviceName;BFF Upstream.Target 必须与此一致
Name: order-service
ListenOn: 0.0.0.0:10200
Mode: dev
Log:
ServiceName: order-service
Mode: file # console | file | volume
Encoding: plain
Level: info
Path: logs
KeepDays: 7
Compress: false
# Rotation: daily
# MaxSize: 100
# MaxBackups: 10
Nacos:
Hosts:
- rnacos:8848
NamespaceId: test
Group: LONE_SERVICES
RegisterIP: order # Docker Compose 里的服务名 / 主机名
RegisterIP: order
Mysql:
Host: mysql
@@ -74,36 +124,35 @@ Mysql:
Database: dms-order
Charset: utf8mb4
Prefix:
# ReadHost: mysql-slave
# ReadPort: 3306
# ReadUser: root
# ReadPassword: "123123"
# ReadDatabase: dms-order
BizRedis:
Host: redis
Port: 6379
Password: ""
DB: 0
AppLog:
Path: logs
InfoFile: info.log
ErrorFile: error.log
FatalFile: fatal.log
MaxSize: 100
MaxBackups: 10
MaxAge: 30
```
### 3. 构建 mod
编辑 `order/go.mod`
```go
go 1.24.3
require (
github.com/zeromicro/go-zero v1.10.2
google.golang.org/grpc v1.80.0
google.golang.org/protobuf v1.36.11
)
```
```bash
go mod tidy
```
### 5. 构建 mod
编辑 `order/go.mod` 增加本地包 pkg.local
```go
require (
github.com/zeromicro/go-zero v1.10.2
google.golang.org/grpc v1.80.0
google.golang.org/protobuf v1.36.11
pkg.local v0.0.0
)
go 1.26
replace pkg.local => ../pkg
```
@@ -112,41 +161,99 @@ replace pkg.local => ../pkg
编辑 `order/order.go`:
增加 import
```go
"pkg.local/discovery"
"pkg.local/log"
"pkg.local/modelbase"
"pkg.local/mysql"
"pkg.local/redis"
"pkg.local/validate"
```
增加初始化
```go
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
if err := log.Init(log.Config{
Path: c.AppLog.Path,
InfoFile: c.AppLog.InfoFile,
ErrorFile: c.AppLog.ErrorFile,
FatalFile: c.AppLog.FatalFile,
MaxSize: c.AppLog.MaxSize,
MaxBackups: c.AppLog.MaxBackups,
MaxAge: c.AppLog.MaxAge,
}); err != nil {
fmt.Fprintf(os.Stderr, "log init: %v\n", err)
os.Exit(1)
}
if err := discovery.Init(discovery.Config{
Hosts: c.Nacos.Hosts,
NamespaceId: c.Nacos.NamespaceId,
Group: c.Nacos.Group,
}); err != nil {
log.Fatalf("nacos init: %v", err)
logx.Errorf("nacos init: %v", err)
}
registerIP := c.Nacos.RegisterIP
port, err := listenPort(c.ListenOn)
if err != nil {
log.Fatalf("parse ListenOn: %v", err)
logx.Errorf("parse ListenOn: %v", err)
}
if err := discovery.Register(discovery.Instance{
ServiceName: c.Name,
IP: registerIP,
Port: port,
Group: c.Nacos.Group,
}); err != nil {
log.Fatalf("nacos register: %v", err)
logx.Errorf("nacos register: %v", err)
}
defer func() {
if err := discovery.Deregister(); err != nil {
log.Printf("nacos deregister: %v", err)
logx.Errorf("nacos deregister: %v", err)
}
}()
ctx := svc.NewServiceContext(c)
db, err := mysql.New(mysql.Config{
Host: c.Mysql.Host,
Port: c.Mysql.Port,
User: c.Mysql.User,
Password: c.Mysql.Password,
Database: c.Mysql.Database,
Charset: c.Mysql.Charset,
Prefix: c.Mysql.Prefix,
ReadHost: c.Mysql.ReadHost,
ReadPort: c.Mysql.ReadPort,
ReadUser: c.Mysql.ReadUser,
ReadPassword: c.Mysql.ReadPassword,
ReadDatabase: c.Mysql.ReadDatabase,
})
if err != nil {
logx.Errorf("mysql init: %v", err)
}
if err := redis.Init(redis.Config{
Host: c.BizRedis.Host,
Port: c.BizRedis.Port,
Password: c.BizRedis.Password,
DB: c.BizRedis.DB,
}); err != nil {
logx.Errorf("redis init: %v", err)
}
modelbase.Init(db, modelbase.Config{Prefix: c.Mysql.Prefix})
ctx := svc.NewServiceContext(c, db)
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
order.RegisterOrderServer(grpcServer, server.NewOrderServer(ctx))
@@ -157,7 +264,11 @@ func main() {
})
defer s.Stop()
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
logx.AddWriter(logx.NewWriter(os.Stdout))
s.AddUnaryInterceptors(validate.UnaryServerInterceptor(validate.MustNew()))
logx.Infof("Starting rpc server at %s...", c.ListenOn)
s.Start()
}
@@ -171,27 +282,39 @@ func listenPort(listenOn string) (uint64, error) {
```
编辑 `order/order.go`:增加本地包
执行 tidy
```go
import (
"pkg.local/discovery"
)
```
再次执行
```bash
go mod tidy
```
### 8. 接入 Docker Compose
编辑 `internal/svc/servicecontext.go`:
```go
type ServiceContext struct {
Config config.Config
DB *gorm.DB
Prefix string
}
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
return &ServiceContext{
Config: c,
DB: db,
Prefix: c.Mysql.Prefix,
}
}
```
### 7. 接入 Docker Compose
在 `deploy/docker-compose.override.yml` 增加服务:
```yaml
order:
image: golang:1.24.3
image: golang:1.26.5
volumes:
- ..:/src
- go-mod-cache:/go/pkg/mod
@@ -205,16 +328,16 @@ go mod tidy
- rnacos
```
### 9. 生成 ProtoSet 给 BFF
### 8. 生成 ProtoSet 给 BFF
```bash
protoc --include_imports --proto_path=proto --descriptor_set_out=../bff/etc/order.pb order.proto
```
### 10. 在 BFF 配 Upstream + Mapping
### 9. 在 BFF 配 Upstream + Mapping
编辑 `bff/etc/bff.yaml`,增加一段(go-zero gateway **每个 RPC 都要写 Mapping**):
编辑 `bff/etc/bff.yaml`,在 **Upstreams** 增加一段
```yaml
- Name: order
@@ -229,12 +352,12 @@ protoc --include_imports --proto_path=proto --descriptor_set_out=../bff/etc/orde
RpcPath: order.Order/Ping
```
### 11. 加接口需要
### 编写业务须知
只改业务逻辑(`internal/logic/`)不必跑 goctl。
改 `proto/order.proto`(新增/改名 RPC、改字段等)后,按文末「三条命令何时用」处理;
若 HTTP 路径有变,再改第 10 步的 Mapping。
若 HTTP 路径有变,再改第 9 步的 Mapping。
---
@@ -251,7 +374,7 @@ protoc -I. -I../pkg/third_party --go_out=. --go-grpc_out=. proto/order.proto
### 命令 ②:生成 BFF ProtoSet
```bash
protoc -I. -I../pkg/third_party --descriptor_set_out=../bff/etc/product.pb --include_imports proto/order.proto
protoc -I. -I../pkg/third_party --descriptor_set_out=../bff/etc/order.pb --include_imports proto/order.proto
```
**什么时候跑:**
@@ -267,8 +390,8 @@ goctl rpc protoc proto/order.proto --go_out=. --go-grpc_out=. --zrpc_out=.
**什么时候跑:**
- **新增 RPC**:需要生成 `internal/logic`、更新 `server` / `productclient` 等
- 注意:可能覆盖已改过的 `product.go` 等,生成后对比合并;也可用手写 logic/server 代替
- **新增 RPC**:需要生成 `internal/logic`、更新 `server` / `orderclient` 等
- 注意:可能覆盖已改过的 `order.go` 等,生成后对比合并;也可用手写 logic/server 代替
## Proto 常用校验规则(protovalidate)