mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
- Introduced Prometheus metrics for gRPC requests and product creation. - Integrated OpenTelemetry for distributed tracing. - Updated Docker Compose configuration to expose metrics endpoint. - Enhanced architecture and development documentation with new environment variables. - Refactored configuration loading to include metrics and tracing settings. - Implemented observability package to handle tracing and metrics. - Updated product server to utilize observability features for logging and tracing.
209 lines
8.1 KiB
Markdown
209 lines
8.1 KiB
Markdown
# ids-svc-product
|
||
|
||
IDS 电商矩阵中的**商品域微服务**,负责 SPU/SKU 主数据、分类商品查询与价格读取。
|
||
|
||
**设计原则:** 轻、快、稳、灵 · Doc-First · Schema-per-Service
|
||
|
||
## 能力概览
|
||
|
||
| RPC | 说明 | 主要调用方 |
|
||
|-----|------|-----------|
|
||
| `CreateProduct` | 创建商品 SPU 及可选 SKU 列表 | 后台 / 运营系统 |
|
||
| `GetProduct` | 按 `product_id` 返回 SPU 及下属 SKU 列表 | BFF / 后台 |
|
||
| `ListCategoryProducts` | 按 `category_id` 分页返回分类下商品列表 | BFF / 商城前台 |
|
||
|
||
契约定义见 [`ids-proto`](https://llms.idsbuybuybuy.com/ecommerce/ids-proto) 的 `proto/ids/product/v1/product.proto`。
|
||
|
||
## 接口清单
|
||
|
||
当前商品服务对外暴露 3 个接口,REST 路径由 `google.api.http` 注解提供给 APISIX gRPC-Transcode 使用。
|
||
|
||
| RPC | REST | 请求 | 响应 | 说明 |
|
||
|---|---|---|---|---|
|
||
| `CreateProduct` | `POST /v1/products` | `CreateProductRequest` | `CreateProductResponse` | 创建商品 SPU,可同时提交 SKU 列表 |
|
||
| `GetProduct` | `GET /v1/products/{product_id}` | `GetProductRequest` | `GetProductResponse` | 按商品 ID 查询商品详情,返回 SPU 与 SKU 列表 |
|
||
| `ListCategoryProducts` | `GET /v1/categories/{category_id}/products?page=1&page_size=20` | `ListCategoryProductsRequest` | `ListCategoryProductsResponse` | 按分类/分组 ID 分页查询商品列表 |
|
||
|
||
### `CreateProduct`
|
||
|
||
| 字段 | 类型 | 必填 | 说明 |
|
||
|---|---|---|---|
|
||
| `product_id` | `string` | 是 | 商品 SPU ID,由调用方生成 |
|
||
| `spu_no` | `string` | 是 | SPU 编码,未删除商品内唯一 |
|
||
| `title` | `string` | 是 | 商品标题 |
|
||
| `selling_point` | `string` | 否 | 商品卖点 |
|
||
| `status` | `ProductStatus` | 是 | 商品状态:`DRAFT` / `ON_SALE` / `OFF_SALE` |
|
||
| `skus` | `CreateProductSKU[]` | 否 | 创建时附带的 SKU 列表 |
|
||
|
||
`CreateProductSKU` 字段:
|
||
|
||
| 字段 | 类型 | 必填 | 说明 |
|
||
|---|---|---|---|
|
||
| `sku_id` | `string` | 是 | SKU ID,由调用方生成 |
|
||
| `name` | `string` | 是 | SKU 名称 |
|
||
| `price` | `int64` | 是 | 售价,单位:分 |
|
||
| `available_quantity` | `int32` | 是 | 初始可售库存 |
|
||
| `attributes_json` | `string` | 否 | 规格属性 JSON 字符串 |
|
||
|
||
### `GetProduct`
|
||
|
||
| 字段 | 类型 | 必填 | 说明 |
|
||
|---|---|---|---|
|
||
| `product_id` | `string` | 是 | 商品 SPU ID |
|
||
|
||
响应 `product` 包含 SPU 基础信息、SKU 列表、`created_at`、`updated_at`。
|
||
|
||
### `ListCategoryProducts`
|
||
|
||
| 字段 | 类型 | 必填 | 说明 |
|
||
|---|---|---|---|
|
||
| `category_id` | `string` | 是 | 分类/分组节点 ID,对应 `product.product_category.id` |
|
||
| `page` | `int32` | 否 | 页码,从 `1` 开始;未传或小于 `1` 时按 `1` 处理 |
|
||
| `page_size` | `int32` | 否 | 每页数量;未传或小于 `1` 时按 `20` 处理,大于 `100` 时截断为 `100` |
|
||
|
||
分页响应字段:
|
||
|
||
| 字段 | 类型 | 说明 |
|
||
|---|---|---|
|
||
| `products` | `Product[]` | 当前页商品列表 |
|
||
| `page` | `int32` | 当前页码 |
|
||
| `page_size` | `int32` | 当前每页数量 |
|
||
| `total` | `int64` | 分类/分组下商品总数 |
|
||
| `total_pages` | `int32` | 总页数 |
|
||
|
||
## 技术栈
|
||
|
||
| 项 | 选型 |
|
||
|---|---|
|
||
| 语言 | Go 1.26 |
|
||
| RPC | gRPC (`google.golang.org/grpc`) |
|
||
| 数据库 | PostgreSQL `product`(`github.com/jackc/pgx/v5`) |
|
||
| 配置 | 环境变量 + 可选本地 `.env`(`github.com/joho/godotenv`) |
|
||
| 契约 | `llms.idsbuybuybuy.com/ecommerce/ids-proto`(本地 `replace ../ids-proto`) |
|
||
|
||
## 目录结构
|
||
|
||
```
|
||
ids-svc-product/
|
||
├── cmd/main.go # 服务入口
|
||
├── internal/
|
||
│ ├── config/config.go # 配置加载
|
||
│ ├── repository/product_repository.go # product 数据访问
|
||
│ └── server/product_server.go # gRPC 契约实现
|
||
├── docs/
|
||
│ ├── ARCHITECTURE.md # 架构白皮书
|
||
│ └── DEVELOPMENT.md # 开发规约与 AI 生成指令
|
||
├── .env.example
|
||
├── go.mod
|
||
└── AGENTS.md
|
||
```
|
||
|
||
## 快速开始
|
||
|
||
### 前置依赖
|
||
|
||
- Go 1.26+
|
||
- 本地 [`ids-proto`](../ids-proto) 仓库(与 `go.mod` 中 `replace` 路径对齐)
|
||
- PostgreSQL(推荐通过 [`ids-ecommerce-infra`](../ids-ecommerce-infra) 启动)
|
||
|
||
### 配置
|
||
|
||
```bash
|
||
cp .env.example .env
|
||
# 按本地 infra 实际凭证修改 POSTGRES_* 或 POSTGRES_DSN
|
||
```
|
||
|
||
| 环境变量 | 默认值 | 说明 |
|
||
|---|---|---|
|
||
| `SERVER_PORT` | `:50052` | gRPC 监听地址 |
|
||
| `POSTGRES_DSN` | — | 完整 DSN(设置后优先于离散变量) |
|
||
| `POSTGRES_HOST` | `localhost` | 数据库主机 |
|
||
| `POSTGRES_PORT` | `5432` | 数据库端口 |
|
||
| `POSTGRES_USER` | `postgres` | 数据库用户 |
|
||
| `POSTGRES_PASSWORD` | `postgres` | 数据库密码 |
|
||
| `POSTGRES_DB` | `ids_ecommerce` | 数据库名 |
|
||
| `POSTGRES_SCHEMA` | `product` | 搜索路径 |
|
||
| `POSTGRES_SSLMODE` | `disable` | SSL 模式 |
|
||
| `NATS_URL` | `nats://localhost:4222` | 预留事件总线地址(首版查询链路不依赖) |
|
||
| `METRICS_PORT` | `:9102` | Prometheus 指标与 HTTP 健康检查监听地址 |
|
||
| `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4318` | OTLP HTTP trace exporter 地址 |
|
||
| `OTEL_EXPORTER_OTLP_INSECURE` | `true` | OTLP exporter 是否使用明文连接 |
|
||
| `DEPLOYMENT_ENVIRONMENT` | `development` | Trace resource 中的部署环境标签 |
|
||
|
||
### 可观测性
|
||
|
||
| 能力 | 入口 / 指标 | 说明 |
|
||
|---|---|---|
|
||
| HTTP 健康检查 | `GET /healthz`, `GET /readyz` | 返回 200 前会检查 PostgreSQL 连通性 |
|
||
| gRPC 健康检查 | `grpc.health.v1.Health/Check` | 注册标准 gRPC Health Checking |
|
||
| Prometheus 指标 | `GET /metrics` | 暴露 Go runtime、process、RPC 与商品业务指标 |
|
||
| RPC 指标 | `ids_product_grpc_requests_total`, `ids_product_grpc_request_duration_seconds` | 按 `method`、`code` 维度统计请求数和耗时 |
|
||
| 商品业务指标 | `ids_product_products_created_total`, `ids_product_category_product_lists_total` | 统计商品创建成功数和分类商品查询成功数 |
|
||
| 链路追踪 | OTLP HTTP | gRPC 自动 span,并为商品创建、商品查询、分类商品查询补充业务 span |
|
||
| 结构化日志 | stdout | access log 与错误日志包含 `trace_id`、`span_id`、`correlation_id` |
|
||
|
||
### 构建与运行
|
||
|
||
```bash
|
||
go mod tidy
|
||
go build ./...
|
||
go run ./cmd
|
||
```
|
||
|
||
启动时会执行 `db.PingContext()` 校验数据库连通性,并通过 `EnsureSchema` 补齐 `skus` 表及 `products` 扩展列。
|
||
|
||
### 本地联调(grpcurl)
|
||
|
||
```bash
|
||
# 列出服务
|
||
grpcurl -plaintext localhost:50052 list
|
||
|
||
# 查询单个商品
|
||
grpcurl -plaintext -d '{"product_id":"<snowflake-id>"}' \
|
||
localhost:50052 ids.product.v1.ProductService/GetProduct
|
||
|
||
# 创建商品
|
||
grpcurl -plaintext -d '{"product_id":"<snowflake-id>","spu_no":"<spu-no>","title":"商品标题","selling_point":"商品卖点","status":"PRODUCT_STATUS_DRAFT","skus":[]}' \
|
||
localhost:50052 ids.product.v1.ProductService/CreateProduct
|
||
|
||
# 分页查询分类商品
|
||
grpcurl -plaintext -d '{"category_id":"<snowflake-category-id>","page":1,"page_size":20}' \
|
||
localhost:50052 ids.product.v1.ProductService/ListCategoryProducts
|
||
```
|
||
|
||
## 数据模型
|
||
|
||
商品域采用 SPU/SKU 分层,**仅读写** `product`:
|
||
|
||
| 表 | 职责 |
|
||
|---|---|
|
||
| `product.products` | SPU 主表(标题、卖点、状态、扩展属性) |
|
||
| `product.product_items` | SKU 明细(规格、价格、属性) |
|
||
| `product.product_item_inventory` | SKU 库存销量(可售、锁定、已售) |
|
||
|
||
- 价格字段统一为 `BIGINT` / Go `int64`,单位:**分**
|
||
- 禁止跨 Schema JOIN(`trade`、`customer` 等)
|
||
|
||
## 架构约束
|
||
|
||
生成或修改代码前请阅读:
|
||
|
||
- [docs/ARCHITECTURE.md](./docs/ARCHITECTURE.md) — 域边界、分层职责、错误语义
|
||
- [docs/DEVELOPMENT.md](./docs/DEVELOPMENT.md) — 依赖对齐、配置规范、实现细则
|
||
|
||
关键红线:
|
||
|
||
| ID | 约束 |
|
||
|---|---|
|
||
| ARC-PROD-001 | 仅读写 `product`,禁止跨 Schema JOIN |
|
||
| ARC-PROD-004 | 价格字段统一 `int64`(分) |
|
||
| ARC-PROD-005 | 状态枚举强对齐 `productv1.ProductStatus` |
|
||
|
||
## 相关仓库
|
||
|
||
| 仓库 | 关系 |
|
||
|---|---|
|
||
| [`ids-proto`](../ids-proto) | gRPC / Protobuf 契约 |
|
||
| [`ids-svc-order`](../ids-svc-order) | 算价阶段商品快照消费方 |
|
||
| [`ids-ecommerce-infra`](../ids-ecommerce-infra) | 本地 PG / NATS / APISIX 基础设施 |
|