fix: ci dir error
This commit is contained in:
@@ -1,34 +1,9 @@
|
||||
## 常用命令
|
||||
|
||||
### 命令 ①:goctl 生成/更新脚手架
|
||||
|
||||
```bash
|
||||
goctl -I. -I../pkg/third_party rpc protoc proto/order.proto --go_out=. --go-grpc_out=. --zrpc_out=.
|
||||
rpc/scripts/gen-rpc.sh order
|
||||
```
|
||||
|
||||
### 命令 ②:生成服务端 pb / grpc
|
||||
|
||||
```bash
|
||||
goctl rpc protoc proto/order.proto --proto_path=. --proto_path=../pkg/third_party --go_out=. --go-grpc_out=. --zrpc_out=.
|
||||
```
|
||||
|
||||
**什么时候跑:**
|
||||
|
||||
- 只改 **校验规则**(必填/长度/范围等)→ **只跑这条**
|
||||
- 增删改字段、增删 RPC → 也要跑(服务端描述符要更新)
|
||||
|
||||
### 命令 ③:生成 BFF ProtoSet
|
||||
|
||||
```bash
|
||||
protoc -I. -I../pkg/third_party --descriptor_set_out=../bff/etc/order.pb --include_imports proto/order.proto
|
||||
```
|
||||
|
||||
**什么时候跑:**
|
||||
|
||||
- 增删改 **字段**(BFF gateway 编解码需要)
|
||||
- 增删 **RPC**
|
||||
- 改 **google.api.http** 路由
|
||||
|
||||
## 如何加一个新服务
|
||||
|
||||
以加 `order` 为例(对标现有 `product`)。
|
||||
@@ -47,20 +22,36 @@ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
||||
在仓库根目录执行:
|
||||
|
||||
```bash
|
||||
cd services
|
||||
goctl rpc new order
|
||||
cd order
|
||||
cd ../rpc
|
||||
mkdir order
|
||||
|
||||
```
|
||||
|
||||
### 2. 将 proto 移到 `proto/` 目录
|
||||
### 2. 将 proto 移到 `rpc/` 目录
|
||||
|
||||
`goctl rpc new` 默认把 proto 放在服务根目录(如 `order/order.proto`)。统一挪到 `proto/`:
|
||||
`goctl rpc new` 默认把 proto 放在服务根目录(如 `order/order.proto`)。统一挪到 `rpc/`:
|
||||
|
||||
```bash
|
||||
mkdir proto
|
||||
mv order.proto proto/
|
||||
cd ../services/order
|
||||
mv order.proto ../../rpc/order/
|
||||
```
|
||||
|
||||
### 3. Zero 兼容 Nacos 等配置
|
||||
编辑 `rpc/order/order.proto`,把 `go_package` 改成本仓库模块路径:
|
||||
|
||||
```protobuf
|
||||
option go_package = "lone-services/rpc/order";
|
||||
```
|
||||
|
||||
### 3. 生成pb 文件
|
||||
|
||||
windows 用 git 的 bash 跑
|
||||
```bash
|
||||
D:\Git\bin\bash.exe rpc/scripts/gen-rpc.sh order
|
||||
```
|
||||
|
||||
### 4. Zero 兼容 Nacos 等配置
|
||||
|
||||
编辑 `order/internal/config/config.go`:
|
||||
|
||||
@@ -80,47 +71,22 @@ type NacosConf struct {
|
||||
}
|
||||
```
|
||||
|
||||
### 4. 配置服务名与监听端口
|
||||
### 5. 配置服务名与监听端口
|
||||
|
||||
编辑 `order/etc/order.yaml`:
|
||||
|
||||
```yaml
|
||||
Nacos:
|
||||
Hosts:
|
||||
- host.docker.internal:8848
|
||||
- rnacos:8848
|
||||
NamespaceId: test
|
||||
Group: LONE_SERVICES
|
||||
RegisterIP: admin
|
||||
ConfigID: develop-admin
|
||||
RegisterIP: order
|
||||
ConfigID: order
|
||||
```
|
||||
|
||||
### 5. 构建 mod
|
||||
|
||||
编辑 `order/go.mod` 增加本地包 pkg.local
|
||||
|
||||
```go
|
||||
go 1.26
|
||||
|
||||
replace pkg.local => ../pkg
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 6. 服务注册
|
||||
|
||||
编辑 `order/order.go`:
|
||||
|
||||
增加 import
|
||||
|
||||
```go
|
||||
"pkg.local/discovery"
|
||||
"pkg.local/modelbase"
|
||||
"pkg.local/mysql"
|
||||
"pkg.local/redis"
|
||||
"pkg.local/validate"
|
||||
"pkg.local/utils"
|
||||
```
|
||||
|
||||
增加初始化
|
||||
|
||||
```go
|
||||
@@ -251,12 +217,6 @@ func listenPort(listenOn string) (uint64, error) {
|
||||
|
||||
```
|
||||
|
||||
执行 tidy
|
||||
|
||||
```bash
|
||||
go mod tidy
|
||||
```
|
||||
|
||||
编辑 `internal/svc/servicecontext.go`:
|
||||
|
||||
```go
|
||||
@@ -279,7 +239,7 @@ func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||
|
||||
### 7. 接入 Docker Compose
|
||||
|
||||
在 `deploy/docker-compose.dev.override.yml` 增加服务:
|
||||
在 `deploy/docker-compose.override.yml` 增加服务:
|
||||
|
||||
```yaml
|
||||
order:
|
||||
@@ -288,7 +248,7 @@ func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||
- ..:/src
|
||||
- go-mod-cache:/go/pkg/mod
|
||||
- go-build-cache:/root/.cache/go-build
|
||||
working_dir: /src/order
|
||||
working_dir: /src/services/order
|
||||
command: go run . -f etc/order.yaml
|
||||
ports:
|
||||
- "10200:10200"
|
||||
@@ -298,13 +258,7 @@ func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||
- rnacos
|
||||
```
|
||||
|
||||
### 8. 生成 ProtoSet 给 BFF
|
||||
|
||||
```bash
|
||||
protoc --include_imports --proto_path=proto --descriptor_set_out=../bff/etc/order.pb order.proto
|
||||
```
|
||||
|
||||
### 9. 在 BFF 配 Upstream(HTTP 路由写在 proto)
|
||||
### 8. 在 BFF 配 Upstream(HTTP 路由写在 proto)
|
||||
|
||||
编辑 `bff/etc/bff.yaml`,在 **Upstreams** 增加一段(不必写 Mappings,gateway 会从 ProtoSet 里的 http option 注册路由):
|
||||
|
||||
@@ -314,63 +268,9 @@ protoc --include_imports --proto_path=proto --descriptor_set_out=../bff/etc/orde
|
||||
Target: order-service # 必须等于 order.yaml 的 Name
|
||||
Timeout: 5000
|
||||
ProtoSets:
|
||||
- etc/order.pb
|
||||
- ../rpc/order/order.pb
|
||||
```
|
||||
|
||||
在对应 RPC 上声明 `google.api.http`(需 `import "google/api/annotations.proto";`):
|
||||
|
||||
```protobuf
|
||||
rpc Ping(Request) returns (Response) {
|
||||
option (google.api.http) = {
|
||||
post: "/admin/v3/order/ping"
|
||||
body: "*"
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
### 编写业务须知
|
||||
|
||||
只改业务逻辑(`internal/logic/`)不必跑 goctl。
|
||||
改 `proto/order.proto`(新增/改名 RPC、改字段、改 http option 等)后,按文末「三条命令何时用」处理;
|
||||
|
||||
若 HTTP 路径有变,改 proto 里的 `google.api.http` 后重新跑命令 ② 生成 ProtoSet。
|
||||
|
||||
---
|
||||
|
||||
### 命令 ①:生成服务端 pb / grpc
|
||||
|
||||
```bash
|
||||
goctl rpc protoc proto/order.proto --proto_path=. --proto_path=../pkg/third_party --go_out=. --go-grpc_out=. --zrpc_out=.
|
||||
```
|
||||
|
||||
**什么时候跑:**
|
||||
|
||||
- 只改 **校验规则**(必填/长度/范围等)→ **只跑这条**
|
||||
- 增删改字段、增删 RPC → 也要跑(服务端描述符要更新)
|
||||
|
||||
### 命令 ②:生成 BFF ProtoSet
|
||||
|
||||
```bash
|
||||
protoc -I. -I../pkg/third_party --descriptor_set_out=../bff/etc/order.pb --include_imports proto/order.proto
|
||||
```
|
||||
|
||||
**什么时候跑:**
|
||||
|
||||
- 增删改 **字段**(BFF gateway 编解码需要)
|
||||
- 增删 **RPC**
|
||||
- 改 **google.api.http** 路由
|
||||
|
||||
### 命令 ③:goctl 生成/更新脚手架
|
||||
|
||||
```bash
|
||||
goctl -I. -I../pkg/third_party rpc protoc proto/order.proto --go_out=. --go-grpc_out=. --zrpc_out=.
|
||||
```
|
||||
|
||||
**什么时候跑:**
|
||||
|
||||
- **新增 RPC**:需要生成 `internal/logic`、更新 `server` / `orderclient` 等
|
||||
- 注意:可能覆盖已改过的 `order.go` 等,生成后对比合并;也可用手写 logic/server 代替
|
||||
|
||||
## Proto 常用校验规则(protovalidate)
|
||||
|
||||
文档:[https://protovalidate.com/schemas/standard-rules/](https://protovalidate.com/schemas/standard-rules/)
|
||||
|
||||
Reference in New Issue
Block a user