Bookstore
Bookstore
Section titled “Bookstore”The go-zero official example: an API gateway that delegates to a backend RPC service, using MySQL for storage.
Architecture
Section titled “Architecture”Client → bookstore-api (HTTP) → bookstore-rpc (gRPC) → MySQLRepository
Section titled “Repository”git clone https://github.com/zeromicro/go-zero.gitcd go-zero/example/bookstoreServices
Section titled “Services”| Service | Port | Description |
|---|---|---|
api/ | 8888 | HTTP gateway |
rpc/ | 8080 | gRPC book service |
Start RPC
Section titled “Start RPC”cd rpcgo run bookstore.go -f etc/bookstore.yamlStart API
Section titled “Start API”cd apigo run bookstore.go -f etc/bookstore-api.yaml# Add a bookcurl -X POST http://localhost:8888/add \ -H "Content-Type: application/json" \ -d '{"book":"The Go Programming Language","price":42}'
# Check stockcurl "http://localhost:8888/check?book=The+Go+Programming+Language"Key Concepts Demonstrated
Section titled “Key Concepts Demonstrated”- API gateway routing to RPC backend
- goctl model generation from MySQL DDL
- ServiceContext wiring of RPC client