一个生产级别的 Go 微服务项目,包含完整的微服务基础设施。
┌─────────────────────────────────────────────────┐
│ Observability Layer │
│ Jaeger (追踪) │ Prometheus (指标) │ Grafana (可视化)│
└───────────────────────────────────────────────────┘
│
┌────────────────────────────────────────────────────┼────────────────────────────────────────┐
│ │ │
│ ┌─────────────────────────────────────────────────┴─────────────────────────────────────┐ │
│ │ API Gateway │ │
│ │ 认证 │ 限流 │ 熔断 │ 路由 │ 追踪 │ 指标 │ │
│ └────────────────────────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────┼─────────────────────────────┐ │
│ ▼ ▼ ▼ │
│ ┌───────────────────┐ ┌───────────────────┐ ┌───────────────────┐ │
│ │ User Service │◀─────▶│ Product Service │◀─────▶│ Order Service │ │
│ │ HTTP + gRPC │ │ HTTP + gRPC │ │ HTTP + gRPC │ │
│ └───────────────────┘ └───────────────────┘ └───────────────────┘ │
│ │
│ ┌────────────────────────────────────────────────────────────────────────────────────────┐│
│ │ Infrastructure Layer ││
│ │ Consul (服务发现) │ Redis (缓存+锁) │ MySQL (存储) ││
│ └────────────────────────────────────────────────────────────────────────────────────────┘│
└────────────────────────────────────────────────────────────────────────────────────────────┘
- ✅ 服务发现 - Consul 注册与发现
- ✅ 负载均衡 - 健康感知、多种策略
- ✅ 熔断降级 - Circuit Breaker + Fallback
- ✅ 限流保护 - 自适应限流
- ✅ 分布式追踪 - OpenTelemetry + Jaeger
- ✅ 指标监控 - Prometheus + Grafana
- ✅ 分布式事务 - Saga 模式
- ✅ 分布式锁 - Redis 实现
- ✅ 缓存 - Redis + Singleflight
- ✅ Singleflight (防缓存击穿)
- ✅ Bulkhead (故障隔离)
- ✅ Timeout Cascade (超时级联)
- ✅ Smart Retry (智能重试)
- ✅ Adaptive Rate Limiter (自适应限流)
- ✅ Docker Compose 一键启动
- ✅ Makefile 构建脚本
- ✅ 单元测试 & 基准测试
- ✅ 配置热重载
- ✅ 请求参数校验
- Go 1.21+
- Docker & Docker Compose
- Make
# 启动 Consul, MySQL, Redis, Jaeger, Prometheus, Grafana
make infra-up# 方式 1: 本地运行
make run-all
# 方式 2: Docker 运行
make docker-up| 服务 | 地址 |
|---|---|
| API Gateway | http://localhost:8080 |
| Consul UI | http://localhost:8500 |
| Jaeger UI | http://localhost:16686 |
| Prometheus | http://localhost:9090 |
| Grafana | http://localhost:3000 (admin/admin) |
# 创建用户
curl -X POST http://localhost:8080/api/v1/users \
-H "Content-Type: application/json" \
-d '{"username": "alice", "email": "alice@example.com", "password": "Pass123!"}'
# 获取用户
curl http://localhost:8080/api/v1/users/{id}# 获取商品列表
curl http://localhost:8080/api/v1/products
# 获取商品详情
curl http://localhost:8080/api/v1/products/{id}# 创建订单
curl -X POST http://localhost:8080/api/v1/orders \
-H "Content-Type: application/json" \
-d '{
"user_id": "u001",
"items": [{"product_id": "p001", "quantity": 2}],
"shipping_address": "北京市"
}'make help # 查看所有命令
make build # 构建所有服务
make test # 运行测试
make test-coverage # 测试覆盖率
make lint # 代码检查
make proto # 生成 Proto 代码go-microservices/
├── api-gateway/ # API 网关
├── user-service/ # 用户服务
├── product-service/ # 商品服务
├── order-service/ # 订单服务
├── proto/ # Protocol Buffers 定义
├── gen/ # 生成的 gRPC 代码
├── pkg/ # 公共包
│ ├── cache/ # 缓存
│ ├── config/ # 配置管理
│ ├── consul/ # 服务发现
│ ├── database/ # 数据库
│ ├── grpcclient/ # gRPC 客户端
│ ├── grpcserver/ # gRPC 服务端
│ ├── httpclient/ # HTTP 客户端
│ ├── loadbalancer/ # 负载均衡
│ ├── lock/ # 分布式锁
│ ├── logger/ # 日志
│ ├── metrics/ # 指标
│ ├── middleware/ # 中间件
│ ├── resilience/ # 弹性层
│ ├── saga/ # Saga 模式
│ ├── servicemesh/ # 服务网格
│ └── tracer/ # 链路追踪
├── configs/ # 配置文件
├── scripts/ # 脚本
├── docs/ # 文档
├── docker-compose.yaml # Docker 编排
└── Makefile # 构建脚本
启动后访问 http://localhost:3000,使用 admin/admin 登录。
预配置的 Dashboard 包括:
- 服务概览 (QPS, 延迟, 错误率)
- 熔断器状态
- 限流指标
- 缓存命中率
- 分布式锁统计
访问 http://localhost:16686 查看链路追踪。
| 类别 | 技术 |
|---|---|
| 语言 | Go 1.21+ |
| Web框架 | Gin |
| RPC | gRPC + Protocol Buffers |
| 服务发现 | Consul |
| 数据库 | MySQL + GORM |
| 缓存 | Redis |
| 链路追踪 | OpenTelemetry + Jaeger |
| 指标监控 | Prometheus |
| 可视化 | Grafana |
| 容器化 | Docker |
MIT License