跳转到内容

书店示例

go-zero 官方示例:HTTP API 网关委托给后端 gRPC 服务,使用 MySQL 持久化数据。

客户端 → bookstore-api (HTTP) → bookstore-rpc (gRPC) → MySQL
Terminal window
git clone https://github.com/zeromicro/go-zero.git
cd go-zero/example/bookstore
服务端口说明
api/8888HTTP 网关
rpc/8080gRPC 书籍服务
Terminal window
cd rpc
go run bookstore.go -f etc/bookstore.yaml
Terminal window
cd api
go run bookstore.go -f etc/bookstore-api.yaml
Terminal window
# 添加书籍
curl -X POST http://localhost:8888/add \
-H "Content-Type: application/json" \
-d '{"book":"The Go Programming Language","price":42}'
# 查询库存
curl "http://localhost:8888/check?book=The+Go+Programming+Language"
  • API 网关路由到 RPC 后端
  • goctl 从 MySQL DDL 生成 model
  • ServiceContext 注入 RPC 客户端