Go to file
2026-07-04 08:15:25 +08:00
cmd Refactor logging to use zap logger in main and product server, enhancing error handling and consistency. 2026-07-01 18:15:16 +08:00
docs Initialize ids-svc-product microservice with core structure, including environment configuration, PostgreSQL setup, and gRPC server implementation. Added .env.example for environment variables, .gitignore to exclude sensitive files, and foundational documents for architecture and development guidelines. Implemented product repository and server logic for handling product and SKU data. 2026-06-26 22:10:15 +08:00
internal Refactor logging to use zap logger in main and product server, enhancing error handling and consistency. 2026-07-01 18:15:16 +08:00
.env.example Initialize ids-svc-product microservice with core structure, including environment configuration, PostgreSQL setup, and gRPC server implementation. Added .env.example for environment variables, .gitignore to exclude sensitive files, and foundational documents for architecture and development guidelines. Implemented product repository and server logic for handling product and SKU data. 2026-06-26 22:10:15 +08:00
.gitignore Add .DS_Store to .gitignore to prevent unnecessary file tracking 2026-07-04 08:15:25 +08:00
AGENTS.md Initialize ids-svc-product microservice with core structure, including environment configuration, PostgreSQL setup, and gRPC server implementation. Added .env.example for environment variables, .gitignore to exclude sensitive files, and foundational documents for architecture and development guidelines. Implemented product repository and server logic for handling product and SKU data. 2026-06-26 22:10:15 +08:00
docker-compose.yaml Update PostgreSQL password in docker-compose configuration for improved security. 2026-06-26 23:41:01 +08:00
go.mod Refactor logging to use zap logger in main and product server, enhancing error handling and consistency. 2026-07-01 18:15:16 +08:00
go.sum Refactor logging to use zap logger in main and product server, enhancing error handling and consistency. 2026-07-01 18:15:16 +08:00
README.md Refactor product repository and enhance server logic to improve handling of product and SKU data. Update environment configuration and documentation for clarity and completeness. 2026-06-26 22:16:37 +08:00

ids-svc-product

IDS 电商矩阵中的商品域微服务,负责 SPU/SKU 主数据与价格读取,为 ids-svc-order 算价链路提供稳定的 gRPC 商品快照查询能力。

设计原则: 轻、快、稳、灵 · Doc-First · Schema-per-Service

能力概览

RPC 说明 主要调用方
GetProduct 按 product_id 返回 SPU 及下属 SKU 列表 BFF / 后台 / 订单服务
BatchGetProducts 批量拉取商品快照,减少算价阶段 N 次 RPC ids-svc-order

契约定义见 ids-proto 的 proto/ids/product/v1/product.proto。

技术栈

项 选型
语言 Go 1.26
RPC gRPC (google.golang.org/grpc)
数据库 PostgreSQL product_schema(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_schema 数据访问
│   └── server/product_server.go     # gRPC 契约实现
├── docs/
│   ├── ARCHITECTURE.md              # 架构白皮书
│   └── DEVELOPMENT.md               # 开发规约与 AI 生成指令
├── .env.example
├── go.mod
└── AGENTS.md

快速开始

前置依赖

配置

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_schema 搜索路径
POSTGRES_SSLMODE disable SSL 模式
NATS_URL nats://localhost:4222 预留事件总线地址(首版查询链路不依赖)

构建与运行

go mod tidy
go build ./...
go run ./cmd

启动时会执行 db.PingContext() 校验数据库连通性,并通过 EnsureSchema 补齐 skus 表及 products 扩展列。

本地联调(grpcurl)

# 列出服务
grpcurl -plaintext localhost:50052 list

# 查询单个商品
grpcurl -plaintext -d '{"product_id":"<uuid>"}' \
  localhost:50052 ids.product.v1.ProductService/GetProduct

# 批量查询
grpcurl -plaintext -d '{"product_ids":["<uuid-1>","<uuid-2>"]}' \
  localhost:50052 ids.product.v1.ProductService/BatchGetProducts

数据模型

商品域采用 SPU/SKU 分层,仅读写 product_schema:

表 职责
product_schema.products SPU 主表(名称、描述、状态、扩展属性)
product_schema.skus SKU 明细(规格、价格、库存)
  • 价格字段统一为 BIGINT / Go int64,单位:分
  • 禁止跨 Schema JOIN(order_schema、user_schema 等)

架构约束

生成或修改代码前请阅读:

关键红线:

ID 约束
ARC-PROD-001 仅读写 product_schema,禁止跨 Schema JOIN
ARC-PROD-003 BatchGetProducts 对重复 ID 去重,缺失 ID 静默跳过
ARC-PROD-004 价格字段统一 int64(分)
ARC-PROD-005 状态枚举强对齐 productv1.ProductStatus

与订单域协作

ids-svc-order (算价)  ──BatchGetProducts──▶  ids-svc-product
                                              │
                                              ▼
                                    PG product_schema

订单服务通过 gRPC 批量拉取 SKU 价格,不得跨库查询商品表。

相关仓库

仓库 关系
ids-proto gRPC / Protobuf 契约
ids-svc-order 算价阶段商品快照消费方
ids-ecommerce-infra 本地 PG / NATS / APISIX 基础设施