用户管理演示应用程序
一个简单的演示应用程序,展示了微服务架构,包括:
- 后端API:Node.js/Express REST API
- 前端:React web应用程序
- 客户服务:Node.js服务,定期访问API
所有服务都是容器化的,可以部署Kubernetes。
建筑
┌─────────────────┐
│ Frontend │ (React App on Port 80)
│ (Port 80) │
└────────┬────────┘
│
│ HTTP
│
┌────────▼────────┐
│ Backend API │ (Express API on Port 3000)
│ (Port 3000) │
└────────┬────────┘
│
│ HTTP
│
┌────────▼────────┐
│ Client Service │ (Periodic API calls)
│ (Daemon) │
└─────────────────┘地方发展
后端
cd backend
npm install
npm start
# Runs on http://localhost:3000前端
cd frontend
npm install
npm start
# Runs on http://localhost:3000 (React default)
# Set REACT_APP_API_URL=http://localhost:3001 if backend is on different port客户服务
cd client-service
npm install
API_URL=http://localhost:3000 npm startDocker构建
构建所有Docker镜像:
# Backend
cd backend
docker build -t user-api-backend:latest .
# Frontend
cd frontend
docker build -t user-management-frontend:latest .
# Client Service
cd client-service
docker build -t user-api-client:latest .Kubernetes部署
部署到本地Kubernetes集群:
# Create namespace
kubectl apply -f k8s/namespace.yaml
# Deploy backend
kubectl apply -f k8s/backend-deployment.yaml
# Deploy frontend
kubectl apply -f k8s/frontend-deployment.yaml
# Deploy client service
kubectl apply -f k8s/client-service-deployment.yaml
# Deploy ingress (optional)
kubectl apply -f k8s/ingress.yaml端口转发(如果不使用Ingress)
# Backend
kubectl port-forward -n demo-app svc/user-api-backend-service 3000:3000
# Frontend
kubectl port-forward -n demo-app svc/user-management-frontend-service 8080:80然后访问:
- 前端:http://localhost:8080
- 后端API:http://localhost:3000
后台集成
每个服务目录中的catalog-info.yaml文件都向Backstage注册了它们。
要在Backstage注册(在端口7007上运行):
- 注册组件组:
kubectl apply -f catalog-info.yaml- 或添加到Backstage目录:
- 首选http://localhost:7007/catalog - 点击“注册现有组件” - 如果使用版本控制,请选择“GitHub”或“GitLab” - 或者使用“URL”并指向catalog-info.yaml文件
API终点
后端API(http://localhost:3000)
GET /health-健康检查GET /api/users-获取所有用户GET /api/users/:id-按ID获取用户POST /api/users-创建新用户PUT /api/users/:id-更新用户DELETE /api/users/:id-删除用户
客户服务行为
客户服务:
- 每5个周期(50秒)检查一次后端运行状况
- 定期(每10秒)获取用户列表
- 每3个周期创建一个示例用户
- 将所有操作记录到stdout
环境变量
后端
PORT-服务器端口(默认值:3000)
前端
REACT_APP_API_URL-后端API URL(默认值:http://localhost:3000)
客户服务
API_URL-后端API URL(默认值:http://user-api-backend-service:3000)INTERVAL-轮询间隔(毫秒)(默认值:10000)
许可证
麻省理工学院
