247 lines
4.8 KiB
Markdown
247 lines
4.8 KiB
Markdown
# Proto 编译完成总结
|
||
|
||
> **完成时间**:2026-01-06
|
||
> **状态**:✅ 编译成功
|
||
|
||
---
|
||
|
||
## ✅ 编译结果
|
||
|
||
### 生成的文件
|
||
|
||
#### 1. Social Service
|
||
|
||
```
|
||
pkg/proto/social/
|
||
├── social.pb.go (49KB) - 消息定义
|
||
└── social.triple.go (13KB) - Dubbo Triple 服务接口
|
||
```
|
||
|
||
#### 2. User Service
|
||
|
||
```
|
||
pkg/proto/user/
|
||
├── user-social.pb.go (72KB) - 消息定义
|
||
└── user-social.triple.go (20KB) - Dubbo Triple 服务接口
|
||
```
|
||
|
||
---
|
||
|
||
## 🔧 解决的问题
|
||
|
||
### 问题 1:protoc-gen-go-triple 包路径错误
|
||
|
||
**原始错误**:
|
||
```bash
|
||
module github.com/dubbogo/triple@latest found (v1.2.1),
|
||
but does not contain package github.com/dubbogo/triple/cmd/protoc-gen-triple
|
||
```
|
||
|
||
**解决方案**:
|
||
使用正确的包路径:
|
||
```bash
|
||
go install github.com/dubbogo/protoc-gen-go-triple/v3@latest
|
||
```
|
||
|
||
---
|
||
|
||
### 问题 2:插件未找到
|
||
|
||
**原因**:`GOPATH/bin` 不在 PATH 中
|
||
|
||
**解决方案**:
|
||
在编译脚本开头添加:
|
||
```bash
|
||
export PATH="$PATH:$(go env GOPATH)/bin"
|
||
```
|
||
|
||
---
|
||
|
||
### 问题 3:google/api/annotations.proto 未找到
|
||
|
||
**解决方案**:
|
||
下载 Google API proto 文件:
|
||
```bash
|
||
cd backend
|
||
mkdir -p google/api
|
||
cd google/api
|
||
curl -o annotations.proto https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto
|
||
curl -o http.proto https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto
|
||
```
|
||
|
||
---
|
||
|
||
### 问题 4:social.pb.go 生成位置错误
|
||
|
||
**原因**:使用 `paths=source_relative` 选项
|
||
|
||
**解决方案**:
|
||
手动移动文件到正确位置:
|
||
```bash
|
||
mv proto/social.pb.go pkg/proto/social/
|
||
```
|
||
|
||
---
|
||
|
||
## 📦 已安装的插件
|
||
|
||
```bash
|
||
$ ls $(go env GOPATH)/bin/ | grep protoc
|
||
protoc-gen-go
|
||
protoc-gen-go-grpc
|
||
protoc-gen-go-triple
|
||
protoc-gen-grpc-gateway
|
||
protoc-gen-openapiv2
|
||
```
|
||
|
||
---
|
||
|
||
## 🚀 使用生成的代码
|
||
|
||
### 1. Service 层导入
|
||
|
||
```go
|
||
import (
|
||
pb "github.com/topfans/backend/pkg/proto/social"
|
||
)
|
||
|
||
type SocialService struct {
|
||
pb.UnimplementedSocialServiceServer
|
||
// ...
|
||
}
|
||
|
||
func (s *SocialService) SendFriendRequest(
|
||
ctx context.Context,
|
||
req *pb.SendFriendRequestRequest,
|
||
) (*pb.SendFriendRequestResponse, error) {
|
||
// 实现逻辑
|
||
}
|
||
```
|
||
|
||
### 2. Gateway 层调用
|
||
|
||
```go
|
||
import (
|
||
pb "github.com/topfans/backend/pkg/proto/social"
|
||
)
|
||
|
||
type SocialController struct {
|
||
socialClient pb.SocialServiceClient
|
||
}
|
||
|
||
func (c *SocialController) SendFriendRequest(ctx *gin.Context) {
|
||
var req pb.SendFriendRequestRequest
|
||
// ...
|
||
resp, err := c.socialClient.SendFriendRequest(dubboCtx, &req)
|
||
// ...
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 📊 编译命令
|
||
|
||
### 完整的编译命令
|
||
|
||
```bash
|
||
# 编译 user-social.proto
|
||
protoc --proto_path=. \
|
||
--go_out=. \
|
||
--go_opt=paths=source_relative \
|
||
--go-triple_out=. \
|
||
--go-triple_opt=paths=source_relative \
|
||
proto/user-social.proto
|
||
|
||
# 编译 social.proto
|
||
protoc --proto_path=. \
|
||
--go_out=. \
|
||
--go_opt=paths=source_relative \
|
||
--go-triple_out=. \
|
||
--go-triple_opt=paths=source_relative \
|
||
proto/social.proto
|
||
```
|
||
|
||
### 使用脚本
|
||
|
||
```bash
|
||
cd backend
|
||
./scripts/compile-proto.sh
|
||
```
|
||
|
||
---
|
||
|
||
## 📁 相关文件
|
||
|
||
| 文件 | 说明 |
|
||
|------|------|
|
||
| `proto/social.proto` | Social Service Proto 定义 |
|
||
| `proto/user-social.proto` | User Service Proto 定义 |
|
||
| `proto/common.proto` | 通用消息定义 |
|
||
| `google/api/annotations.proto` | Google API 注解 |
|
||
| `google/api/http.proto` | HTTP 注解 |
|
||
| `scripts/compile-proto.sh` | 编译脚本 |
|
||
| `pkg/proto/social/` | 生成的 Social Service 代码 |
|
||
| `pkg/proto/user/` | 生成的 User Service 代码 |
|
||
|
||
---
|
||
|
||
## 🎯 下一步
|
||
|
||
现在 Proto 编译已完成,可以继续:
|
||
|
||
### 1. 实现 Social Service
|
||
|
||
```
|
||
services/socialService/
|
||
├── service/
|
||
│ └── social_service.go # 实现 8 个 RPC 方法
|
||
├── provider/
|
||
│ └── social_provider.go # Dubbo 服务提供者
|
||
├── configs/
|
||
│ └── dubbo.yaml # Dubbo 配置
|
||
└── main.go # 服务启动入口
|
||
```
|
||
|
||
### 2. 创建 Gateway Controller
|
||
|
||
```
|
||
gateway/
|
||
├── controller/
|
||
│ └── social_controller.go # HTTP API 控制器
|
||
├── dto/
|
||
│ └── social_dto.go # 数据传输对象
|
||
└── routes/
|
||
└── routes.go # 路由注册(更新)
|
||
```
|
||
|
||
### 3. 测试
|
||
|
||
- 单元测试
|
||
- 集成测试
|
||
- Dubbo RPC 调用测试
|
||
- API 接口测试
|
||
|
||
---
|
||
|
||
## ✅ 检查清单
|
||
|
||
- [x] 安装 protoc 编译器
|
||
- [x] 安装 protoc-gen-go
|
||
- [x] 安装 protoc-gen-go-triple
|
||
- [x] 下载 Google API proto 文件
|
||
- [x] 配置 PATH 环境变量
|
||
- [x] 编译 user-social.proto
|
||
- [x] 编译 social.proto
|
||
- [x] 验证生成的文件
|
||
- [ ] 实现 Service 层
|
||
- [ ] 创建 Provider 层
|
||
- [ ] 创建 Gateway Controller
|
||
- [ ] 测试验证
|
||
|
||
---
|
||
|
||
**完成人**:AI Assistant
|
||
**完成时间**:2026-01-06
|
||
**状态**:✅ Proto 编译成功,可以继续开发
|
||
|