feat: dev

This commit is contained in:
2026-06-01 18:22:30 +08:00
parent f021b43f05
commit 0bbbe20ec7
20 changed files with 9493 additions and 11427 deletions
+111
View File
@@ -0,0 +1,111 @@
stages:
- install
- build
- deploy
variables:
NODE_ENV: production
# 仅 prod 分支触发
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: never
- if: $CI_COMMIT_BRANCH == "prod"
# pnpm store 缓存
cache:
key:
files:
- pnpm-lock.yaml
paths:
- .pnpm-store
# ==================== INSTALL ====================
install_dependencies:
stage: install
tags:
- rent-deploy
script:
- npm install -g pnpm
- pnpm config set store-dir .pnpm-store
- pnpm install --frozen-lockfile
artifacts:
paths:
- node_modules/
- apps/*/node_modules/
- packages/*/node_modules/
expire_in: 1 hour
# ==================== BUILD ====================
build_server:
stage: build
tags:
- rent-deploy
needs: [install_dependencies]
script:
- npm install -g pnpm
- pnpm config set store-dir .pnpm-store
- pnpm --filter @rent/shared-types build
- pnpm --filter @rent/shared-utils build
- pnpm --filter @rent/server build
artifacts:
paths:
- apps/server/dist/
expire_in: 1 hour
build_merchant:
stage: build
tags:
- rent-deploy
needs: [install_dependencies]
script:
- npm install -g pnpm
- pnpm config set store-dir .pnpm-store
- pnpm --filter @rent/shared-types build
- pnpm --filter @rent/shared-utils build
- pnpm --filter @rent/merchant-admin build
artifacts:
paths:
- apps/merchant-admin/dist/
expire_in: 1 hour
build_platform:
stage: build
tags:
- rent-deploy
needs: [install_dependencies]
script:
- npm install -g pnpm
- pnpm config set store-dir .pnpm-store
- pnpm --filter @rent/shared-types build
- pnpm --filter @rent/shared-utils build
- pnpm --filter @rent/platform-admin build
artifacts:
paths:
- apps/platform-admin/dist/
expire_in: 1 hour
# ==================== DEPLOY ====================
deploy_production:
stage: deploy
tags:
- rent-deploy
needs:
- build_server
- build_merchant
- build_platform
script:
- echo "部署到生产环境..."
- cd deploy/docker
- docker-compose down --remove-orphans
- docker-compose build --parallel
- docker-compose up -d
- docker image prune -f
- echo "等待服务启动..."
- sleep 10
- docker-compose ps
environment:
name: production
only:
- prod
+1 -2
View File
@@ -10,7 +10,7 @@
- **小程序**: uni-app + Vue3 + Pinia + SCSS - **小程序**: uni-app + Vue3 + Pinia + SCSS
- **商家后台**: React 18 + TypeScript + Ant Design + Zustand - **商家后台**: React 18 + TypeScript + Ant Design + Zustand
- **平台后台**: React 18 + TypeScript + Ant Design + Zustand - **平台后台**: React 18 + TypeScript + Ant Design + Zustand
- **部署**: Docker + Kubernetes + Nginx - **部署**: Docker + Docker Compose + Nginx
- **包管理**: pnpm (monorepo) - **包管理**: pnpm (monorepo)
## 项目结构 ## 项目结构
@@ -32,7 +32,6 @@ rent-platform/
│ └── seeds/ # 种子数据 │ └── seeds/ # 种子数据
├── deploy/ ├── deploy/
│ ├── docker/ # Docker & Docker Compose │ ├── docker/ # Docker & Docker Compose
│ ├── k8s/ # Kubernetes manifests
│ └── nginx/ # Nginx 配置 │ └── nginx/ # Nginx 配置
└── 项目需求文档.md └── 项目需求文档.md
``` ```
+43
View File
@@ -0,0 +1,43 @@
.PHONY: help install build up down restart logs ps deploy clean
DOCKER_COMPOSE = docker-compose -f deploy/docker/docker-compose.yml
help: ## 显示帮助信息
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
install: ## 安装依赖
npm install -g pnpm
pnpm install
build: ## 构建所有 Docker 镜像
$(DOCKER_COMPOSE) build --parallel
up: ## 启动所有服务
$(DOCKER_COMPOSE) up -d
down: ## 停止所有服务
$(DOCKER_COMPOSE) down
restart: ## 重启所有服务
$(DOCKER_COMPOSE) restart
logs: ## 查看所有服务日志
$(DOCKER_COMPOSE) logs -f --tail=100
logs-server: ## 查看后端服务日志
$(DOCKER_COMPOSE) logs -f --tail=100 server
ps: ## 查看运行中的容器
$(DOCKER_COMPOSE) ps
deploy: down build up ## 完整部署(停止 → 构建 → 启动)
@echo ""
@echo "部署完成!"
@echo " 商家后台: http://localhost:8081"
@echo " 平台后台: http://localhost:8082"
@echo " API 服务: http://localhost:3000"
clean: ## 清理所有容器、数据卷和悬空镜像
$(DOCKER_COMPOSE) down -v
docker image prune -f
+2 -2
View File
@@ -15,7 +15,7 @@ rent-platform/
│ ├── shared-types/ # 共享 TypeScript 类型定义 │ ├── shared-types/ # 共享 TypeScript 类型定义
│ ├── shared-utils/ # 共享工具函数 │ ├── shared-utils/ # 共享工具函数
│ └── shared-config/ # 共享配置 (ESLint, TS等) │ └── shared-config/ # 共享配置 (ESLint, TS等)
├── deploy/ # 部署配置 (Docker, K8s) ├── deploy/ # 部署配置 (Docker, Nginx)
├── docs/ # 项目文档 ├── docs/ # 项目文档
├── database/ # 数据库脚本 ├── database/ # 数据库脚本
└── pnpm-workspace.yaml └── pnpm-workspace.yaml
@@ -29,7 +29,7 @@ rent-platform/
| 商家管理后台 | React 18 + TypeScript + Ant Design | | 商家管理后台 | React 18 + TypeScript + Ant Design |
| 平台管理后台 | React 18 + TypeScript + Ant Design | | 平台管理后台 | React 18 + TypeScript + Ant Design |
| 后端服务 | NestJS + MySQL + Redis + RabbitMQ | | 后端服务 | NestJS + MySQL + Redis + RabbitMQ |
| 基础设施 | Docker + Kubernetes + Nginx | | 基础设施 | Docker + Docker Compose + Nginx |
## 快速开始 ## 快速开始
+194
View File
@@ -0,0 +1,194 @@
# 部署指南
## 目录结构
```
deploy/
├── docker/
│ ├── docker-compose.yml # Docker Compose 编排文件
│ ├── Dockerfile.server # 后端服务镜像
│ ├── Dockerfile.merchant # 商家后台镜像
│ ├── Dockerfile.platform # 平台后台镜像
│ └── .env.example # 环境变量模板
└── nginx/
├── api.conf # API 反向代理配置
├── merchant.conf # 商家后台 Nginx 配置
└── platform.conf # 平台后台 Nginx 配置
```
## 服务端口
| 服务 | 端口 | 说明 |
|------|------|------|
| MySQL | 3306 | 数据库 |
| Redis | 6379 | 缓存 |
| Server | 3000 | 后端 API |
| Merchant Admin | 8081 | 商家管理后台 |
| Platform Admin | 8082 | 平台管理后台 |
---
## 一、快速部署(手动)
### 1. 配置环境变量
```bash
cd deploy/docker
cp .env.example .env
# 编辑 .env,填入真实的密码和密钥
vi .env
```
### 2. 构建并启动
```bash
# 方式一:使用 Makefile(推荐)
make deploy
# 方式二:直接使用 docker-compose
docker-compose -f deploy/docker/docker-compose.yml up -d --build
```
### 3. 常用命令
```bash
make help # 查看所有命令
make logs # 查看日志
make logs-server # 查看后端日志
make ps # 查看容器状态
make restart # 重启服务
make down # 停止服务
make clean # 清理所有数据(包括数据库)
```
---
## 二、GitLab CI/CD 自动化部署
### 前提条件
- 服务器已安装 Docker 和 Docker Compose
- 服务器已部署 GitLab 实例
- 项目代码已推送到 GitLab 仓库
### 第 1 步:安装 GitLab Runner
```bash
# 添加 GitLab Runner 仓库
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
# 安装
sudo apt install gitlab-runner
```
### 第 2 步:注册 Runner
1. 在 GitLab 中打开项目 → **Settings → CI/CD → Runners**
2. 复制 registration token
3. 在服务器上执行注册:
```bash
sudo gitlab-runner register
```
按提示填写:
| 项目 | 值 |
|------|------|
| GitLab URL | `http://你的GitLab域名/` |
| Token | 从 Settings 页面复制的 token |
| Description | `rent-deploy-runner` |
| Tags | `rent-deploy` |
| Executor | `shell` |
### 第 3 步:配置 Runner 权限
```bash
# 让 gitlab-runner 用户可以执行 docker 命令
sudo usermod -aG docker gitlab-runner
# 全局安装 pnpm
sudo -u gitlab-runner npm install -g pnpm
# 验证
sudo -u gitlab-runner docker ps
sudo -u gitlab-runner pnpm --version
```
### 第 4 步:配置 CI/CD 变量
在 GitLab 中打开 **Settings → CI/CD → Variables**,添加:
| 变量名 | 说明 | 勾选 Masked |
|--------|------|-------------|
| `PROD_DB_PASSWORD` | MySQL root 密码 | ✅ |
| `PROD_JWT_SECRET` | JWT 签名密钥 | ✅ |
### 第 5 步:创建环境变量文件
```bash
# 在服务器项目目录下创建 .env(只需一次)
cd /path/to/repo/deploy/docker
cp .env.example .env
vi .env
# 填入 PROD_DB_PASSWORD 和 PROD_JWT_SECRET 的真实值
```
### 第 6 步:推送代码触发部署
```bash
git push origin prod
```
推送后 GitLab 会自动运行流水线:
1. **install** 阶段 — 安装依赖
2. **build** 阶段 — 并行构建 server / merchant / platform
3. **deploy** 阶段 — 停止旧服务 → 构建镜像 → 启动新服务
可以在 GitLab → **CI/CD → Pipelines** 页面查看实时日志。
---
## 三、故障排查
### 查看服务状态
```bash
make ps
```
### 查看日志
```bash
# 所有服务
make logs
# 仅后端
make logs-server
# 特定容器
docker logs rent-mysql
docker logs rent-redis
docker logs rent-server
```
### 数据库连接失败
```bash
# 检查 MySQL 是否就绪
docker exec rent-mysql mysqladmin ping -h localhost -u root -p
# 进入 MySQL 手动检查
docker exec -it rent-mysql mysql -u root -p rent_platform
```
### 重建某个服务
```bash
# 只重建后端
docker-compose -f deploy/docker/docker-compose.yml up -d --build server
# 只重建商家后台
docker-compose -f deploy/docker/docker-compose.yml up -d --build merchant-admin
```
+9
View File
@@ -0,0 +1,9 @@
# ===== 数据库 =====
DB_PASSWORD=change_me_in_production
# ===== JWT =====
JWT_SECRET=change_me_in_production
# ===== 服务配置 =====
NODE_ENV=production
PORT=3000
+3
View File
@@ -2,8 +2,11 @@
FROM node:18-alpine AS merchant-builder FROM node:18-alpine AS merchant-builder
WORKDIR /app WORKDIR /app
COPY pnpm-workspace.yaml pnpm-lock.yaml package.json ./ COPY pnpm-workspace.yaml pnpm-lock.yaml package.json ./
COPY packages/ ./packages/
COPY apps/merchant-admin/ ./apps/merchant-admin/ COPY apps/merchant-admin/ ./apps/merchant-admin/
RUN npm install -g pnpm && pnpm install --frozen-lockfile RUN npm install -g pnpm && pnpm install --frozen-lockfile
RUN pnpm --filter @rent/shared-types build
RUN pnpm --filter @rent/shared-utils build
RUN cd apps/merchant-admin && pnpm run build RUN cd apps/merchant-admin && pnpm run build
FROM nginx:alpine AS merchant FROM nginx:alpine AS merchant
+3
View File
@@ -2,8 +2,11 @@
FROM node:18-alpine AS platform-builder FROM node:18-alpine AS platform-builder
WORKDIR /app WORKDIR /app
COPY pnpm-workspace.yaml pnpm-lock.yaml package.json ./ COPY pnpm-workspace.yaml pnpm-lock.yaml package.json ./
COPY packages/ ./packages/
COPY apps/platform-admin/ ./apps/platform-admin/ COPY apps/platform-admin/ ./apps/platform-admin/
RUN npm install -g pnpm && pnpm install --frozen-lockfile RUN npm install -g pnpm && pnpm install --frozen-lockfile
RUN pnpm --filter @rent/shared-types build
RUN pnpm --filter @rent/shared-utils build
RUN cd apps/platform-admin && pnpm run build RUN cd apps/platform-admin && pnpm run build
FROM nginx:alpine AS platform FROM nginx:alpine AS platform
-30
View File
@@ -1,30 +0,0 @@
apiVersion: v1
kind: Namespace
metadata:
name: rent-platform
---
# 以下文件按顺序执行
# 1. config.yaml - ConfigMap & Secret
# 2. mysql.yaml - MySQL StatefulSet
# 3. redis.yaml - Redis StatefulSet
# 4. server.yaml - 后端服务 Deployment + Service + Ingress
#
# 使用方式
# kubectl apply -f config.yaml
# kubectl apply -f mysql.yaml
# kubectl apply -f redis.yaml
# 等待 MySQL 和 Redis 就绪后
# kubectl apply -f server.yaml
-24
View File
@@ -1,24 +0,0 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: rent-server-config
namespace: rent-platform
data:
NODE_ENV: "production"
PORT: "3000"
DB_HOST: "mysql-service"
DB_PORT: "3306"
DB_USERNAME: "root"
DB_DATABASE: "rent_platform"
REDIS_HOST: "redis-service"
REDIS_PORT: "6379"
---
apiVersion: v1
kind: Secret
metadata:
name: rent-server-secret
namespace: rent-platform
type: Opaque
stringData:
DB_PASSWORD: "CHANGE_ME"
JWT_SECRET: "CHANGE_ME"
-67
View File
@@ -1,67 +0,0 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: rent-platform
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: rent-server-secret
key: DB_PASSWORD
- name: MYSQL_DATABASE
value: rent_platform
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
resources:
requests:
memory: "512Mi"
cpu: "250m"
limits:
memory: "1Gi"
cpu: "500m"
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-pvc
---
apiVersion: v1
kind: Service
metadata:
name: mysql-service
namespace: rent-platform
spec:
selector:
app: mysql
ports:
- port: 3306
targetPort: 3306
clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
namespace: rent-platform
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
-60
View File
@@ -1,60 +0,0 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: rent-platform
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:7-alpine
ports:
- containerPort: 6379
command: ["redis-server", "--appendonly", "yes"]
volumeMounts:
- name: redis-data
mountPath: /data
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
volumes:
- name: redis-data
persistentVolumeClaim:
claimName: redis-pvc
---
apiVersion: v1
kind: Service
metadata:
name: redis-service
namespace: rent-platform
spec:
selector:
app: redis
ports:
- port: 6379
targetPort: 6379
clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-pvc
namespace: rent-platform
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
-76
View File
@@ -1,76 +0,0 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: server
namespace: rent-platform
spec:
replicas: 2
selector:
matchLabels:
app: server
template:
metadata:
labels:
app: server
spec:
containers:
- name: server
image: rent-platform/server:latest
ports:
- containerPort: 3000
envFrom:
- configMapRef:
name: rent-server-config
- secretRef:
name: rent-server-secret
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
readinessProbe:
httpGet:
path: /api
port: 3000
initialDelaySeconds: 10
periodSeconds: 10
livenessProbe:
httpGet:
path: /api
port: 3000
initialDelaySeconds: 30
periodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
name: server-service
namespace: rent-platform
spec:
selector:
app: server
ports:
- port: 3000
targetPort: 3000
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: rent-ingress
namespace: rent-platform
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: api.rent-platform.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: server-service
port:
number: 3000
+1 -1
View File
@@ -3,7 +3,7 @@ server {
server_name api.rent-platform.com; server_name api.rent-platform.com;
location / { location / {
proxy_pass http://server-service:3000; proxy_pass http://server:3000;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+1 -1
View File
@@ -10,7 +10,7 @@ server {
} }
location /api/ { location /api/ {
proxy_pass http://server-service:3000/api/; proxy_pass http://server:3000/api/;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+1 -1
View File
@@ -10,7 +10,7 @@ server {
} }
location /api/ { location /api/ {
proxy_pass http://server-service:3000/api/; proxy_pass http://server:3000/api/;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-1
View File
@@ -234,7 +234,6 @@ rent-platform/
### 部署 ### 部署
- **容器化**Docker + Docker Compose - **容器化**Docker + Docker Compose
- **编排**Kubernetes
- **反向代理**Nginx - **反向代理**Nginx
- **包管理**pnpm (monorepo) - **包管理**pnpm (monorepo)
+1 -1
View File
@@ -176,7 +176,7 @@ VALUES ('PLATFORM_MAIN', 0.00, 0.00, 0.00, 0.00, 'active');
| 小程序 | uni-app + Vue3 + Pinia + SCSS | | 小程序 | uni-app + Vue3 + Pinia + SCSS |
| 商家后台 | React 18 + TypeScript + Ant Design + Zustand | | 商家后台 | React 18 + TypeScript + Ant Design + Zustand |
| 平台后台 | React 18 + TypeScript + Ant Design + Zustand | | 平台后台 | React 18 + TypeScript + Ant Design + Zustand |
| 部署 | Docker + Kubernetes + Nginx | | 部署 | Docker + Docker Compose + Nginx |
| 包管理 | pnpm (monorepo) | | 包管理 | pnpm (monorepo) |
--- ---
+1 -1
View File
@@ -18,7 +18,7 @@
| 商家管理后台 | React 18 + TypeScript + Ant Design + 自适应 | 强类型语言保障代码质量,UI组件库快速构建企业级管理界面 | | 商家管理后台 | React 18 + TypeScript + Ant Design + 自适应 | 强类型语言保障代码质量,UI组件库快速构建企业级管理界面 |
| 平台管理后台 | React 18 + TypeScript + Ant Design + 自适应 | 与商家后台技术栈统一,降低团队学习成本,便于跨端功能复用 | | 平台管理后台 | React 18 + TypeScript + Ant Design + 自适应 | 与商家后台技术栈统一,降低团队学习成本,便于跨端功能复用 |
| 后端服务 | NestJS + MySQL + Redis + RabbitMQ | Node.js企业级框架,搭配关系型数据库与缓存中间件,保障高并发场景下的系统性能 | | 后端服务 | NestJS + MySQL + Redis + RabbitMQ | Node.js企业级框架,搭配关系型数据库与缓存中间件,保障高并发场景下的系统性能 |
| 基础设施 | Docker + Kubernetes + Nginx | 容器化部署与编排,实现服务的弹性伸缩与负载均衡,提升系统可用性 | | 基础设施 | Docker + Docker Compose + Nginx | 容器化部署,实现服务的快速部署与负载均衡,提升系统可用性 |
### (三)skills ### (三)skills
+9122 -11160
View File
File diff suppressed because it is too large Load Diff