create-dp-koa 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -0
- package/index.mjs +97 -0
- package/package.json +33 -0
- package/template/.env.development +9 -0
- package/template/.env.production +12 -0
- package/template/.github/workflows/ci-cd.yml +182 -0
- package/template/.trae/documents/controller_development_plan.md +386 -0
- package/template/.trae/skills/00-backend-core.skill.md +50 -0
- package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
- package/template/.trae/skills/10-backend-api.skill.md +54 -0
- package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
- package/template/.trae/skills/20-backend-repository.skill.md +25 -0
- package/template/.trae/skills/21-backend-service.skill.md +135 -0
- package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
- package/template/.trae/skills/30-backend-validation.skill.md +320 -0
- package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
- package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
- package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
- package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
- package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
- package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
- package/template/.trae/skills/90-backend-testing.skill.md +29 -0
- package/template/.trae/skills/README.md +49 -0
- package/template/.vscode/launch.json +38 -0
- package/template/.vscode/settings.json +1 -0
- package/template/Dockerfile +36 -0
- package/template/README.md +229 -0
- package/template/docker-compose.yml +135 -0
- package/template/docs/API_DOCUMENTATION.md +837 -0
- package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
- package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
- package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
- package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
- package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
- package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
- package/template/docs/DOCUMENTATION_INDEX.md +294 -0
- package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
- package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
- package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
- package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
- package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
- package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
- package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
- package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
- package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
- package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
- package/template/docs/INSTALLATION_GUIDE.md +611 -0
- package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
- package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
- package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
- package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
- package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
- package/template/docs/QUICK_START.md +268 -0
- package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
- package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
- package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
- package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
- package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
- package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
- package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
- package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
- package/template/docs/TROUBLESHOOTING.md +869 -0
- package/template/env.production.example +62 -0
- package/template/jest.config.js +34 -0
- package/template/package-lock.json +13240 -0
- package/template/package.json +119 -0
- package/template/patches/typeorm+0.3.25.patch +22 -0
- package/template/scripts/sync-template.mjs +84 -0
- package/template/scripts/test-annotation-system.sh +48 -0
- package/template/scripts/test-core-functionality.sh +28 -0
- package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
- package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
- package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
- package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
- package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
- package/template/src/annotations/index.ts +50 -0
- package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
- package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
- package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
- package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
- package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
- package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
- package/template/src/annotations/processors/index.ts +10 -0
- package/template/src/app.ts +123 -0
- package/template/src/controllers/base.controller.ts +41 -0
- package/template/src/controllers/cacheManagement.controller.ts +131 -0
- package/template/src/controllers/captcha.controller.ts +57 -0
- package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
- package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
- package/template/src/controllers/example/ExampleController.ts +110 -0
- package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
- package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
- package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
- package/template/src/controllers/health.controller.ts +235 -0
- package/template/src/controllers/home/register.controller.ts +58 -0
- package/template/src/controllers/home/ytGoods.controller.ts +92 -0
- package/template/src/controllers/home/ytShop.controller.ts +135 -0
- package/template/src/controllers/home/ytUser.controller.ts +89 -0
- package/template/src/controllers/logManagement.controller.ts +396 -0
- package/template/src/controllers/public/emailSend.controller.ts +65 -0
- package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
- package/template/src/controllers/testData.controller.ts +253 -0
- package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
- package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
- package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
- package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
- package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
- package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
- package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
- package/template/src/dto/goods.dto.ts +212 -0
- package/template/src/dto/service/ytService.dto.ts +13 -0
- package/template/src/dto/user.dto.ts +177 -0
- package/template/src/entity/base.entity.ts +13 -0
- package/template/src/entity/columnTypes.ts +13 -0
- package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
- package/template/src/entity/goodsUnlocker.entity.ts +34 -0
- package/template/src/entity/index.ts +15 -0
- package/template/src/entity/shop.entity.ts +52 -0
- package/template/src/entity/shopUser.entity.ts +41 -0
- package/template/src/entity/ytGoods.entity.ts +94 -0
- package/template/src/entity/ytUser.entity.ts +96 -0
- package/template/src/examples/InterceptorExampleRunner.ts +284 -0
- package/template/src/examples/ServiceInterceptorExample.ts +214 -0
- package/template/src/examples/SwaggerProcessorExample.ts +169 -0
- package/template/src/examples/TransactionManagerDemo.ts +377 -0
- package/template/src/examples/cacheExamples.ts +155 -0
- package/template/src/framework/decorator/controller.ts +311 -0
- package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
- package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
- package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
- package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
- package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
- package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
- package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
- package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
- package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
- package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
- package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
- package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
- package/template/src/framework/decorator/refactored/index.ts +98 -0
- package/template/src/framework/decorator/swagger.ts +150 -0
- package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
- package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
- package/template/src/framework/interceptors/index.ts +19 -0
- package/template/src/framework/plugins/registry.ts +63 -0
- package/template/src/framework/plugins/types.ts +15 -0
- package/template/src/framework/types/ServiceResult.ts +151 -0
- package/template/src/framework/types/index.ts +16 -0
- package/template/src/framework/utils/CacheManager.ts +430 -0
- package/template/src/framework/utils/CacheService.ts +248 -0
- package/template/src/framework/utils/DtoValidator.ts +164 -0
- package/template/src/framework/utils/MigrationHelper.ts +179 -0
- package/template/src/framework/utils/MigrationManager.ts +256 -0
- package/template/src/framework/utils/NewRouter.ts +207 -0
- package/template/src/framework/utils/TransactionManager.ts +172 -0
- package/template/src/framework/utils/bootstrap.ts +445 -0
- package/template/src/framework/utils/cache.ts +269 -0
- package/template/src/framework/utils/databaseConfig.ts +148 -0
- package/template/src/framework/utils/db.ts +39 -0
- package/template/src/framework/utils/dbMonitor.ts +106 -0
- package/template/src/framework/utils/dynamicSwagger.ts +410 -0
- package/template/src/framework/utils/function.ts +61 -0
- package/template/src/framework/utils/gracefulShutdown.ts +131 -0
- package/template/src/framework/utils/logger.ts +388 -0
- package/template/src/framework/utils/metrics.ts +182 -0
- package/template/src/framework/utils/router.ts +417 -0
- package/template/src/framework/utils/swagger.ts +184 -0
- package/template/src/framework/utils/testDb.ts +19 -0
- package/template/src/framework/utils/token.ts +23 -0
- package/template/src/framework/utils/transform.ts +17 -0
- package/template/src/libs/aokEmailSender.ts +42 -0
- package/template/src/libs/captcha.ts +37 -0
- package/template/src/libs/cos.ts +45 -0
- package/template/src/libs/mCache.ts +7 -0
- package/template/src/libs/serviceValidate.ts +3 -0
- package/template/src/libs/tecentSms.ts +51 -0
- package/template/src/middlewares/a.middleware.ts +6 -0
- package/template/src/middlewares/error.middleware.ts +14 -0
- package/template/src/middlewares/logging.middleware.ts +187 -0
- package/template/src/middlewares/static.middleware.ts +79 -0
- package/template/src/middlewares/swagger.middleware.ts +70 -0
- package/template/src/middlewares/token.middleware.ts +32 -0
- package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
- package/template/src/migrations/index.ts +6 -0
- package/template/src/plugins/weboffice/core/context.ts +47 -0
- package/template/src/plugins/weboffice/core/errors.ts +51 -0
- package/template/src/plugins/weboffice/core/types.ts +63 -0
- package/template/src/plugins/weboffice/core/utils.ts +7 -0
- package/template/src/plugins/weboffice/entities/index.ts +3 -0
- package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
- package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
- package/template/src/plugins/weboffice/http/routes.ts +179 -0
- package/template/src/plugins/weboffice/index.ts +23 -0
- package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
- package/template/src/repository/UserRepository.ts +122 -0
- package/template/src/repository/base/BaseRepository.ts +124 -0
- package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
- package/template/src/routers/index.ts +49 -0
- package/template/src/service/base.service.ts +116 -0
- package/template/src/service/paramValidateTest.service.ts +139 -0
- package/template/src/service/ytGoods.service.ts +42 -0
- package/template/src/service/ytShop.service.ts +90 -0
- package/template/src/service/ytUser.service.ts +451 -0
- package/template/src/test/swaggerParameterTest.ts +90 -0
- package/template/src/utils/testDataInitializer.ts +296 -0
- package/template/static/output.json +15203 -0
- package/template/test/controllers/controllers.test.ts +173 -0
- package/template/test/controllers/example/ExampleController.test.ts +222 -0
- package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
- package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
- package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
- package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
- package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
- package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
- package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
- package/template/test/framework/annotation/NewRouter.test.ts +272 -0
- package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
- package/template/test/framework/annotation/setup.ts +26 -0
- package/template/test/framework/cache.test.ts +101 -0
- package/template/test/framework/databaseConfig.test.ts +142 -0
- package/template/test/integration/integration.test.ts +153 -0
- package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
- package/template/test/service/business.test.ts +87 -0
- package/template/test/service/paramValidateTest.service.test.ts +184 -0
- package/template/test/service/ytUser.service.test.ts +566 -0
- package/template/test/setup.ts +20 -0
- package/template/test/setupAfterEnv.ts +14 -0
- package/template/test/utils/testHelpers.ts +220 -0
- package/template/test_output.txt +0 -0
- package/template/tsconfig.build.json +17 -0
- package/template/tsconfig.json +31 -0
- package/template/webpack.config.js +71 -0
- package/template/yarn.lock +7354 -0
|
@@ -0,0 +1,1062 @@
|
|
|
1
|
+
# 部署指南
|
|
2
|
+
|
|
3
|
+
本指南提供 DP-Koa Framework 在不同环境下的详细部署方案,包括开发环境、测试环境和生产环境。
|
|
4
|
+
|
|
5
|
+
## 📋 目录
|
|
6
|
+
|
|
7
|
+
- [部署概览](#部署概览)
|
|
8
|
+
- [环境准备](#环境准备)
|
|
9
|
+
- [Docker 部署](#docker-部署)
|
|
10
|
+
- [Docker Compose 部署](#docker-compose-部署)
|
|
11
|
+
- [Kubernetes 部署](#kubernetes-部署)
|
|
12
|
+
- [传统服务器部署](#传统服务器部署)
|
|
13
|
+
- [CI/CD 流水线](#cicd-流水线)
|
|
14
|
+
- [监控配置](#监控配置)
|
|
15
|
+
- [安全配置](#安全配置)
|
|
16
|
+
- [性能优化](#性能优化)
|
|
17
|
+
- [故障排除](#故障排除)
|
|
18
|
+
|
|
19
|
+
## 🚀 部署概览
|
|
20
|
+
|
|
21
|
+
### 部署架构
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
|
|
25
|
+
│ Load Balancer │────│ Application │────│ Database │
|
|
26
|
+
│ (Nginx) │ │ (Node.js) │ │ (MySQL) │
|
|
27
|
+
└─────────────────┘ └─────────────────┘ └─────────────────┘
|
|
28
|
+
│ │ │
|
|
29
|
+
│ │ │
|
|
30
|
+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
|
|
31
|
+
│ SSL/TLS │ │ Cache Layer │ │ Backup │
|
|
32
|
+
│ Certificate │ │ (Redis) │ │ System │
|
|
33
|
+
└─────────────────┘ └─────────────────┘ └─────────────────┘
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 部署选项
|
|
37
|
+
|
|
38
|
+
| 部署方式 | 适用场景 | 复杂度 | 可扩展性 | 维护成本 |
|
|
39
|
+
|----------|----------|--------|----------|----------|
|
|
40
|
+
| Docker | 单机部署 | 低 | 中 | 低 |
|
|
41
|
+
| Docker Compose | 多服务部署 | 中 | 中 | 中 |
|
|
42
|
+
| Kubernetes | 大规模部署 | 高 | 高 | 高 |
|
|
43
|
+
| 传统服务器 | 简单部署 | 低 | 低 | 中 |
|
|
44
|
+
|
|
45
|
+
## 🔧 环境准备
|
|
46
|
+
|
|
47
|
+
### 1. 服务器要求
|
|
48
|
+
|
|
49
|
+
#### 最低配置
|
|
50
|
+
|
|
51
|
+
- **CPU**: 2 核心
|
|
52
|
+
- **内存**: 4GB RAM
|
|
53
|
+
- **存储**: 20GB SSD
|
|
54
|
+
- **网络**: 100Mbps
|
|
55
|
+
|
|
56
|
+
#### 推荐配置
|
|
57
|
+
|
|
58
|
+
- **CPU**: 4 核心
|
|
59
|
+
- **内存**: 8GB RAM
|
|
60
|
+
- **存储**: 50GB SSD
|
|
61
|
+
- **网络**: 1Gbps
|
|
62
|
+
|
|
63
|
+
### 2. 系统要求
|
|
64
|
+
|
|
65
|
+
#### 操作系统
|
|
66
|
+
|
|
67
|
+
- **Ubuntu**: 20.04 LTS 或更高版本
|
|
68
|
+
- **CentOS**: 8 或更高版本
|
|
69
|
+
- **RHEL**: 8 或更高版本
|
|
70
|
+
- **Debian**: 11 或更高版本
|
|
71
|
+
|
|
72
|
+
#### 必需软件
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
# Ubuntu/Debian
|
|
76
|
+
sudo apt update
|
|
77
|
+
sudo apt install -y curl wget git nginx
|
|
78
|
+
|
|
79
|
+
# CentOS/RHEL
|
|
80
|
+
sudo yum update -y
|
|
81
|
+
sudo yum install -y curl wget git nginx
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## 🐳 Docker 部署
|
|
85
|
+
|
|
86
|
+
### 1. 构建镜像
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
# 构建生产镜像
|
|
90
|
+
docker build -t dp-koa-framework:latest .
|
|
91
|
+
|
|
92
|
+
# 查看镜像
|
|
93
|
+
docker images dp-koa-framework
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 2. 运行容器
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
# 基本运行
|
|
100
|
+
docker run -d \
|
|
101
|
+
--name dp-koa-app \
|
|
102
|
+
-p 3000:3000 \
|
|
103
|
+
-e NODE_ENV=production \
|
|
104
|
+
dp-koa-framework:latest
|
|
105
|
+
|
|
106
|
+
# 带环境变量运行
|
|
107
|
+
docker run -d \
|
|
108
|
+
--name dp-koa-app \
|
|
109
|
+
-p 3000:3000 \
|
|
110
|
+
-e NODE_ENV=production \
|
|
111
|
+
-e DB_HOST=mysql-server \
|
|
112
|
+
-e DB_PASSWORD=your_password \
|
|
113
|
+
-v /app/logs:/app/logs \
|
|
114
|
+
dp-koa-framework:latest
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### 3. 容器管理
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
# 查看容器状态
|
|
121
|
+
docker ps
|
|
122
|
+
|
|
123
|
+
# 查看容器日志
|
|
124
|
+
docker logs dp-koa-app
|
|
125
|
+
|
|
126
|
+
# 进入容器
|
|
127
|
+
docker exec -it dp-koa-app sh
|
|
128
|
+
|
|
129
|
+
# 停止容器
|
|
130
|
+
docker stop dp-koa-app
|
|
131
|
+
|
|
132
|
+
# 删除容器
|
|
133
|
+
docker rm dp-koa-app
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 🐙 Docker Compose 部署
|
|
137
|
+
|
|
138
|
+
### 1. 基础部署
|
|
139
|
+
|
|
140
|
+
使用项目提供的 `docker-compose.yml`:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
# 启动所有服务
|
|
144
|
+
docker-compose up -d
|
|
145
|
+
|
|
146
|
+
# 查看服务状态
|
|
147
|
+
docker-compose ps
|
|
148
|
+
|
|
149
|
+
# 查看日志
|
|
150
|
+
docker-compose logs -f
|
|
151
|
+
|
|
152
|
+
# 停止服务
|
|
153
|
+
docker-compose down
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 2. 自定义配置
|
|
157
|
+
|
|
158
|
+
创建 `docker-compose.override.yml`:
|
|
159
|
+
|
|
160
|
+
```yaml
|
|
161
|
+
version: '3.8'
|
|
162
|
+
|
|
163
|
+
services:
|
|
164
|
+
app:
|
|
165
|
+
environment:
|
|
166
|
+
- NODE_ENV=production
|
|
167
|
+
- DB_HOST=mysql
|
|
168
|
+
- DB_PASSWORD=your_secure_password
|
|
169
|
+
- REDIS_PASSWORD=your_redis_password
|
|
170
|
+
volumes:
|
|
171
|
+
- ./logs:/app/logs
|
|
172
|
+
- ./uploads:/app/uploads
|
|
173
|
+
restart: unless-stopped
|
|
174
|
+
|
|
175
|
+
mysql:
|
|
176
|
+
environment:
|
|
177
|
+
- MYSQL_ROOT_PASSWORD=root_secure_password
|
|
178
|
+
- MYSQL_PASSWORD=your_secure_password
|
|
179
|
+
volumes:
|
|
180
|
+
- mysql_data:/var/lib/mysql
|
|
181
|
+
- ./backup:/backup
|
|
182
|
+
|
|
183
|
+
redis:
|
|
184
|
+
command: redis-server --requirepass your_redis_password
|
|
185
|
+
volumes:
|
|
186
|
+
- redis_data:/data
|
|
187
|
+
|
|
188
|
+
volumes:
|
|
189
|
+
mysql_data:
|
|
190
|
+
redis_data:
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### 3. 生产环境配置
|
|
194
|
+
|
|
195
|
+
创建 `docker-compose.prod.yml`:
|
|
196
|
+
|
|
197
|
+
```yaml
|
|
198
|
+
version: '3.8'
|
|
199
|
+
|
|
200
|
+
services:
|
|
201
|
+
app:
|
|
202
|
+
build:
|
|
203
|
+
context: .
|
|
204
|
+
dockerfile: Dockerfile.prod
|
|
205
|
+
environment:
|
|
206
|
+
- NODE_ENV=production
|
|
207
|
+
- PORT=3000
|
|
208
|
+
- DB_HOST=mysql
|
|
209
|
+
- DB_USERNAME=app_user
|
|
210
|
+
- DB_PASSWORD=${DB_PASSWORD}
|
|
211
|
+
- REDIS_HOST=redis
|
|
212
|
+
- REDIS_PASSWORD=${REDIS_PASSWORD}
|
|
213
|
+
depends_on:
|
|
214
|
+
mysql:
|
|
215
|
+
condition: service_healthy
|
|
216
|
+
redis:
|
|
217
|
+
condition: service_started
|
|
218
|
+
restart: unless-stopped
|
|
219
|
+
networks:
|
|
220
|
+
- app-network
|
|
221
|
+
deploy:
|
|
222
|
+
resources:
|
|
223
|
+
limits:
|
|
224
|
+
memory: 1G
|
|
225
|
+
cpus: '0.5'
|
|
226
|
+
reservations:
|
|
227
|
+
memory: 512M
|
|
228
|
+
cpus: '0.25'
|
|
229
|
+
|
|
230
|
+
mysql:
|
|
231
|
+
image: mysql:8.0
|
|
232
|
+
environment:
|
|
233
|
+
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
|
|
234
|
+
- MYSQL_DATABASE=app_db
|
|
235
|
+
- MYSQL_USER=app_user
|
|
236
|
+
- MYSQL_PASSWORD=${DB_PASSWORD}
|
|
237
|
+
volumes:
|
|
238
|
+
- mysql_data:/var/lib/mysql
|
|
239
|
+
- ./docker/mysql/init:/docker-entrypoint-initdb.d
|
|
240
|
+
restart: unless-stopped
|
|
241
|
+
networks:
|
|
242
|
+
- app-network
|
|
243
|
+
healthcheck:
|
|
244
|
+
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
|
|
245
|
+
interval: 10s
|
|
246
|
+
timeout: 5s
|
|
247
|
+
retries: 5
|
|
248
|
+
deploy:
|
|
249
|
+
resources:
|
|
250
|
+
limits:
|
|
251
|
+
memory: 2G
|
|
252
|
+
cpus: '1.0'
|
|
253
|
+
|
|
254
|
+
redis:
|
|
255
|
+
image: redis:7-alpine
|
|
256
|
+
command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD}
|
|
257
|
+
volumes:
|
|
258
|
+
- redis_data:/data
|
|
259
|
+
restart: unless-stopped
|
|
260
|
+
networks:
|
|
261
|
+
- app-network
|
|
262
|
+
deploy:
|
|
263
|
+
resources:
|
|
264
|
+
limits:
|
|
265
|
+
memory: 512M
|
|
266
|
+
cpus: '0.25'
|
|
267
|
+
|
|
268
|
+
nginx:
|
|
269
|
+
image: nginx:alpine
|
|
270
|
+
ports:
|
|
271
|
+
- "80:80"
|
|
272
|
+
- "443:443"
|
|
273
|
+
volumes:
|
|
274
|
+
- ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
|
|
275
|
+
- ./docker/nginx/ssl:/etc/nginx/ssl
|
|
276
|
+
- ./logs/nginx:/var/log/nginx
|
|
277
|
+
depends_on:
|
|
278
|
+
- app
|
|
279
|
+
restart: unless-stopped
|
|
280
|
+
networks:
|
|
281
|
+
- app-network
|
|
282
|
+
|
|
283
|
+
volumes:
|
|
284
|
+
mysql_data:
|
|
285
|
+
redis_data:
|
|
286
|
+
|
|
287
|
+
networks:
|
|
288
|
+
app-network:
|
|
289
|
+
driver: bridge
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### 4. 环境变量配置
|
|
293
|
+
|
|
294
|
+
创建 `.env.prod`:
|
|
295
|
+
|
|
296
|
+
```bash
|
|
297
|
+
# 数据库配置
|
|
298
|
+
DB_PASSWORD=your_secure_database_password
|
|
299
|
+
MYSQL_ROOT_PASSWORD=your_secure_root_password
|
|
300
|
+
|
|
301
|
+
# Redis配置
|
|
302
|
+
REDIS_PASSWORD=your_secure_redis_password
|
|
303
|
+
|
|
304
|
+
# JWT配置
|
|
305
|
+
JWT_SECRET=your_super_secure_jwt_secret_key
|
|
306
|
+
|
|
307
|
+
# 其他配置
|
|
308
|
+
NODE_ENV=production
|
|
309
|
+
LOG_LEVEL=info
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### 5. 启动生产环境
|
|
313
|
+
|
|
314
|
+
```bash
|
|
315
|
+
# 使用生产配置启动
|
|
316
|
+
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
|
|
317
|
+
|
|
318
|
+
# 查看服务状态
|
|
319
|
+
docker-compose ps
|
|
320
|
+
|
|
321
|
+
# 查看应用日志
|
|
322
|
+
docker-compose logs -f app
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## ☸️ Kubernetes 部署
|
|
326
|
+
|
|
327
|
+
### 1. 创建命名空间
|
|
328
|
+
|
|
329
|
+
```yaml
|
|
330
|
+
# namespace.yaml
|
|
331
|
+
apiVersion: v1
|
|
332
|
+
kind: Namespace
|
|
333
|
+
metadata:
|
|
334
|
+
name: dp-koa-framework
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
### 2. 配置 ConfigMap
|
|
338
|
+
|
|
339
|
+
```yaml
|
|
340
|
+
# configmap.yaml
|
|
341
|
+
apiVersion: v1
|
|
342
|
+
kind: ConfigMap
|
|
343
|
+
metadata:
|
|
344
|
+
name: dp-koa-config
|
|
345
|
+
namespace: dp-koa-framework
|
|
346
|
+
data:
|
|
347
|
+
NODE_ENV: "production"
|
|
348
|
+
PORT: "3000"
|
|
349
|
+
LOG_LEVEL: "info"
|
|
350
|
+
DB_HOST: "mysql-service"
|
|
351
|
+
DB_PORT: "3306"
|
|
352
|
+
DB_DATABASE: "app_db"
|
|
353
|
+
REDIS_HOST: "redis-service"
|
|
354
|
+
REDIS_PORT: "6379"
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
### 3. 配置 Secret
|
|
358
|
+
|
|
359
|
+
```yaml
|
|
360
|
+
# secret.yaml
|
|
361
|
+
apiVersion: v1
|
|
362
|
+
kind: Secret
|
|
363
|
+
metadata:
|
|
364
|
+
name: dp-koa-secrets
|
|
365
|
+
namespace: dp-koa-framework
|
|
366
|
+
type: Opaque
|
|
367
|
+
data:
|
|
368
|
+
DB_PASSWORD: <base64-encoded-password>
|
|
369
|
+
REDIS_PASSWORD: <base64-encoded-redis-password>
|
|
370
|
+
JWT_SECRET: <base64-encoded-jwt-secret>
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### 4. 部署应用
|
|
374
|
+
|
|
375
|
+
```yaml
|
|
376
|
+
# deployment.yaml
|
|
377
|
+
apiVersion: apps/v1
|
|
378
|
+
kind: Deployment
|
|
379
|
+
metadata:
|
|
380
|
+
name: dp-koa-app
|
|
381
|
+
namespace: dp-koa-framework
|
|
382
|
+
spec:
|
|
383
|
+
replicas: 3
|
|
384
|
+
selector:
|
|
385
|
+
matchLabels:
|
|
386
|
+
app: dp-koa-app
|
|
387
|
+
template:
|
|
388
|
+
metadata:
|
|
389
|
+
labels:
|
|
390
|
+
app: dp-koa-app
|
|
391
|
+
spec:
|
|
392
|
+
containers:
|
|
393
|
+
- name: app
|
|
394
|
+
image: dp-koa-framework:latest
|
|
395
|
+
ports:
|
|
396
|
+
- containerPort: 3000
|
|
397
|
+
envFrom:
|
|
398
|
+
- configMapRef:
|
|
399
|
+
name: dp-koa-config
|
|
400
|
+
- secretRef:
|
|
401
|
+
name: dp-koa-secrets
|
|
402
|
+
resources:
|
|
403
|
+
requests:
|
|
404
|
+
memory: "512Mi"
|
|
405
|
+
cpu: "250m"
|
|
406
|
+
limits:
|
|
407
|
+
memory: "1Gi"
|
|
408
|
+
cpu: "500m"
|
|
409
|
+
livenessProbe:
|
|
410
|
+
httpGet:
|
|
411
|
+
path: /live
|
|
412
|
+
port: 3000
|
|
413
|
+
initialDelaySeconds: 30
|
|
414
|
+
periodSeconds: 10
|
|
415
|
+
readinessProbe:
|
|
416
|
+
httpGet:
|
|
417
|
+
path: /ready
|
|
418
|
+
port: 3000
|
|
419
|
+
initialDelaySeconds: 5
|
|
420
|
+
periodSeconds: 5
|
|
421
|
+
volumeMounts:
|
|
422
|
+
- name: logs
|
|
423
|
+
mountPath: /app/logs
|
|
424
|
+
volumes:
|
|
425
|
+
- name: logs
|
|
426
|
+
emptyDir: {}
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
### 5. 创建服务
|
|
430
|
+
|
|
431
|
+
```yaml
|
|
432
|
+
# service.yaml
|
|
433
|
+
apiVersion: v1
|
|
434
|
+
kind: Service
|
|
435
|
+
metadata:
|
|
436
|
+
name: dp-koa-service
|
|
437
|
+
namespace: dp-koa-framework
|
|
438
|
+
spec:
|
|
439
|
+
selector:
|
|
440
|
+
app: dp-koa-app
|
|
441
|
+
ports:
|
|
442
|
+
- port: 80
|
|
443
|
+
targetPort: 3000
|
|
444
|
+
type: ClusterIP
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
### 6. 配置 Ingress
|
|
448
|
+
|
|
449
|
+
```yaml
|
|
450
|
+
# ingress.yaml
|
|
451
|
+
apiVersion: networking.k8s.io/v1
|
|
452
|
+
kind: Ingress
|
|
453
|
+
metadata:
|
|
454
|
+
name: dp-koa-ingress
|
|
455
|
+
namespace: dp-koa-framework
|
|
456
|
+
annotations:
|
|
457
|
+
nginx.ingress.kubernetes.io/rewrite-target: /
|
|
458
|
+
nginx.ingress.kubernetes.io/ssl-redirect: "true"
|
|
459
|
+
spec:
|
|
460
|
+
tls:
|
|
461
|
+
- hosts:
|
|
462
|
+
- api.yourdomain.com
|
|
463
|
+
secretName: tls-secret
|
|
464
|
+
rules:
|
|
465
|
+
- host: api.yourdomain.com
|
|
466
|
+
http:
|
|
467
|
+
paths:
|
|
468
|
+
- path: /
|
|
469
|
+
pathType: Prefix
|
|
470
|
+
backend:
|
|
471
|
+
service:
|
|
472
|
+
name: dp-koa-service
|
|
473
|
+
port:
|
|
474
|
+
number: 80
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
### 7. 部署到 Kubernetes
|
|
478
|
+
|
|
479
|
+
```bash
|
|
480
|
+
# 应用所有配置
|
|
481
|
+
kubectl apply -f namespace.yaml
|
|
482
|
+
kubectl apply -f configmap.yaml
|
|
483
|
+
kubectl apply -f secret.yaml
|
|
484
|
+
kubectl apply -f deployment.yaml
|
|
485
|
+
kubectl apply -f service.yaml
|
|
486
|
+
kubectl apply -f ingress.yaml
|
|
487
|
+
|
|
488
|
+
# 查看部署状态
|
|
489
|
+
kubectl get pods -n dp-koa-framework
|
|
490
|
+
kubectl get services -n dp-koa-framework
|
|
491
|
+
kubectl get ingress -n dp-koa-framework
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
## 🖥️ 传统服务器部署
|
|
495
|
+
|
|
496
|
+
### 1. 安装 Node.js
|
|
497
|
+
|
|
498
|
+
```bash
|
|
499
|
+
# 使用 NodeSource 仓库安装 Node.js 20
|
|
500
|
+
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
|
|
501
|
+
sudo apt-get install -y nodejs
|
|
502
|
+
|
|
503
|
+
# 验证安装
|
|
504
|
+
node --version
|
|
505
|
+
npm --version
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
### 2. 安装 PM2
|
|
509
|
+
|
|
510
|
+
```bash
|
|
511
|
+
# 全局安装 PM2
|
|
512
|
+
npm install -g pm2
|
|
513
|
+
|
|
514
|
+
# 验证安装
|
|
515
|
+
pm2 --version
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
### 3. 部署应用
|
|
519
|
+
|
|
520
|
+
```bash
|
|
521
|
+
# 克隆项目
|
|
522
|
+
git clone <repository-url>
|
|
523
|
+
cd dp-koa-framework
|
|
524
|
+
|
|
525
|
+
# 安装依赖
|
|
526
|
+
npm install --production
|
|
527
|
+
|
|
528
|
+
# 构建应用
|
|
529
|
+
npm run build
|
|
530
|
+
|
|
531
|
+
# 使用 PM2 启动
|
|
532
|
+
pm2 start dist/main.js --name "dp-koa-app"
|
|
533
|
+
|
|
534
|
+
# 保存 PM2 配置
|
|
535
|
+
pm2 save
|
|
536
|
+
pm2 startup
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
### 4. 配置 Nginx
|
|
540
|
+
|
|
541
|
+
创建 `/etc/nginx/sites-available/dp-koa-framework`:
|
|
542
|
+
|
|
543
|
+
```nginx
|
|
544
|
+
server {
|
|
545
|
+
listen 80;
|
|
546
|
+
server_name your-domain.com;
|
|
547
|
+
|
|
548
|
+
# 重定向到 HTTPS
|
|
549
|
+
return 301 https://$server_name$request_uri;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
server {
|
|
553
|
+
listen 443 ssl http2;
|
|
554
|
+
server_name your-domain.com;
|
|
555
|
+
|
|
556
|
+
# SSL 配置
|
|
557
|
+
ssl_certificate /path/to/your/certificate.crt;
|
|
558
|
+
ssl_certificate_key /path/to/your/private.key;
|
|
559
|
+
ssl_protocols TLSv1.2 TLSv1.3;
|
|
560
|
+
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512;
|
|
561
|
+
ssl_prefer_server_ciphers off;
|
|
562
|
+
|
|
563
|
+
# 日志配置
|
|
564
|
+
access_log /var/log/nginx/dp-koa-access.log;
|
|
565
|
+
error_log /var/log/nginx/dp-koa-error.log;
|
|
566
|
+
|
|
567
|
+
# 反向代理配置
|
|
568
|
+
location / {
|
|
569
|
+
proxy_pass http://localhost:3000;
|
|
570
|
+
proxy_http_version 1.1;
|
|
571
|
+
proxy_set_header Upgrade $http_upgrade;
|
|
572
|
+
proxy_set_header Connection 'upgrade';
|
|
573
|
+
proxy_set_header Host $host;
|
|
574
|
+
proxy_set_header X-Real-IP $remote_addr;
|
|
575
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
576
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
|
577
|
+
proxy_cache_bypass $http_upgrade;
|
|
578
|
+
|
|
579
|
+
# 超时配置
|
|
580
|
+
proxy_connect_timeout 60s;
|
|
581
|
+
proxy_send_timeout 60s;
|
|
582
|
+
proxy_read_timeout 60s;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
# 静态文件缓存
|
|
586
|
+
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
|
|
587
|
+
expires 1y;
|
|
588
|
+
add_header Cache-Control "public, immutable";
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
# 健康检查
|
|
592
|
+
location /health {
|
|
593
|
+
proxy_pass http://localhost:3000/health;
|
|
594
|
+
access_log off;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
启用站点:
|
|
600
|
+
|
|
601
|
+
```bash
|
|
602
|
+
# 启用站点
|
|
603
|
+
sudo ln -s /etc/nginx/sites-available/dp-koa-framework /etc/nginx/sites-enabled/
|
|
604
|
+
|
|
605
|
+
# 测试配置
|
|
606
|
+
sudo nginx -t
|
|
607
|
+
|
|
608
|
+
# 重启 Nginx
|
|
609
|
+
sudo systemctl restart nginx
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
## 🔄 CI/CD 流水线
|
|
613
|
+
|
|
614
|
+
### 1. GitHub Actions
|
|
615
|
+
|
|
616
|
+
创建 `.github/workflows/deploy.yml`:
|
|
617
|
+
|
|
618
|
+
```yaml
|
|
619
|
+
name: Deploy to Production
|
|
620
|
+
|
|
621
|
+
on:
|
|
622
|
+
push:
|
|
623
|
+
branches: [ main ]
|
|
624
|
+
pull_request:
|
|
625
|
+
branches: [ main ]
|
|
626
|
+
|
|
627
|
+
jobs:
|
|
628
|
+
test:
|
|
629
|
+
runs-on: ubuntu-latest
|
|
630
|
+
|
|
631
|
+
steps:
|
|
632
|
+
- uses: actions/checkout@v3
|
|
633
|
+
|
|
634
|
+
- name: Setup Node.js
|
|
635
|
+
uses: actions/setup-node@v3
|
|
636
|
+
with:
|
|
637
|
+
node-version: '20'
|
|
638
|
+
cache: 'npm'
|
|
639
|
+
|
|
640
|
+
- name: Install dependencies
|
|
641
|
+
run: npm ci
|
|
642
|
+
|
|
643
|
+
- name: Run tests
|
|
644
|
+
run: npm test
|
|
645
|
+
|
|
646
|
+
- name: Run linting
|
|
647
|
+
run: npm run lint
|
|
648
|
+
|
|
649
|
+
- name: Type check
|
|
650
|
+
run: npm run type-check
|
|
651
|
+
|
|
652
|
+
build:
|
|
653
|
+
needs: test
|
|
654
|
+
runs-on: ubuntu-latest
|
|
655
|
+
|
|
656
|
+
steps:
|
|
657
|
+
- uses: actions/checkout@v3
|
|
658
|
+
|
|
659
|
+
- name: Setup Node.js
|
|
660
|
+
uses: actions/setup-node@v3
|
|
661
|
+
with:
|
|
662
|
+
node-version: '20'
|
|
663
|
+
cache: 'npm'
|
|
664
|
+
|
|
665
|
+
- name: Install dependencies
|
|
666
|
+
run: npm ci
|
|
667
|
+
|
|
668
|
+
- name: Build application
|
|
669
|
+
run: npm run build
|
|
670
|
+
|
|
671
|
+
- name: Build Docker image
|
|
672
|
+
run: docker build -t dp-koa-framework:${{ github.sha }} .
|
|
673
|
+
|
|
674
|
+
- name: Push to registry
|
|
675
|
+
run: |
|
|
676
|
+
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
|
|
677
|
+
docker push dp-koa-framework:${{ github.sha }}
|
|
678
|
+
|
|
679
|
+
deploy:
|
|
680
|
+
needs: build
|
|
681
|
+
runs-on: ubuntu-latest
|
|
682
|
+
if: github.ref == 'refs/heads/main'
|
|
683
|
+
|
|
684
|
+
steps:
|
|
685
|
+
- name: Deploy to production
|
|
686
|
+
run: |
|
|
687
|
+
# 部署脚本
|
|
688
|
+
echo "Deploying to production..."
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
### 2. GitLab CI
|
|
692
|
+
|
|
693
|
+
创建 `.gitlab-ci.yml`:
|
|
694
|
+
|
|
695
|
+
```yaml
|
|
696
|
+
stages:
|
|
697
|
+
- test
|
|
698
|
+
- build
|
|
699
|
+
- deploy
|
|
700
|
+
|
|
701
|
+
variables:
|
|
702
|
+
DOCKER_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
|
|
703
|
+
|
|
704
|
+
test:
|
|
705
|
+
stage: test
|
|
706
|
+
image: node:20-alpine
|
|
707
|
+
script:
|
|
708
|
+
- npm ci
|
|
709
|
+
- npm test
|
|
710
|
+
- npm run lint
|
|
711
|
+
- npm run type-check
|
|
712
|
+
only:
|
|
713
|
+
- merge_requests
|
|
714
|
+
- main
|
|
715
|
+
|
|
716
|
+
build:
|
|
717
|
+
stage: build
|
|
718
|
+
image: docker:latest
|
|
719
|
+
services:
|
|
720
|
+
- docker:dind
|
|
721
|
+
script:
|
|
722
|
+
- docker build -t $DOCKER_IMAGE .
|
|
723
|
+
- docker push $DOCKER_IMAGE
|
|
724
|
+
only:
|
|
725
|
+
- main
|
|
726
|
+
|
|
727
|
+
deploy:
|
|
728
|
+
stage: deploy
|
|
729
|
+
image: alpine:latest
|
|
730
|
+
script:
|
|
731
|
+
- apk add --no-cache curl
|
|
732
|
+
- curl -X POST "$DEPLOY_WEBHOOK_URL" -H "Content-Type: application/json" -d '{"image": "'$DOCKER_IMAGE'"}'
|
|
733
|
+
only:
|
|
734
|
+
- main
|
|
735
|
+
when: manual
|
|
736
|
+
```
|
|
737
|
+
|
|
738
|
+
## 📊 监控配置
|
|
739
|
+
|
|
740
|
+
### 1. Prometheus 配置
|
|
741
|
+
|
|
742
|
+
创建 `docker/prometheus/prometheus.yml`:
|
|
743
|
+
|
|
744
|
+
```yaml
|
|
745
|
+
global:
|
|
746
|
+
scrape_interval: 15s
|
|
747
|
+
evaluation_interval: 15s
|
|
748
|
+
|
|
749
|
+
rule_files:
|
|
750
|
+
- "rules/*.yml"
|
|
751
|
+
|
|
752
|
+
scrape_configs:
|
|
753
|
+
- job_name: 'dp-koa-framework'
|
|
754
|
+
static_configs:
|
|
755
|
+
- targets: ['app:3000']
|
|
756
|
+
metrics_path: '/metrics'
|
|
757
|
+
scrape_interval: 5s
|
|
758
|
+
scrape_timeout: 5s
|
|
759
|
+
|
|
760
|
+
- job_name: 'mysql'
|
|
761
|
+
static_configs:
|
|
762
|
+
- targets: ['mysql-exporter:9104']
|
|
763
|
+
|
|
764
|
+
- job_name: 'redis'
|
|
765
|
+
static_configs:
|
|
766
|
+
- targets: ['redis-exporter:9121']
|
|
767
|
+
|
|
768
|
+
- job_name: 'nginx'
|
|
769
|
+
static_configs:
|
|
770
|
+
- targets: ['nginx-exporter:9113']
|
|
771
|
+
```
|
|
772
|
+
|
|
773
|
+
### 2. Grafana 仪表板
|
|
774
|
+
|
|
775
|
+
创建 `docker/grafana/provisioning/dashboards/dashboard.yml`:
|
|
776
|
+
|
|
777
|
+
```yaml
|
|
778
|
+
apiVersion: 1
|
|
779
|
+
|
|
780
|
+
providers:
|
|
781
|
+
- name: 'dp-koa-framework'
|
|
782
|
+
orgId: 1
|
|
783
|
+
folder: ''
|
|
784
|
+
type: file
|
|
785
|
+
disableDeletion: false
|
|
786
|
+
updateIntervalSeconds: 10
|
|
787
|
+
allowUiUpdates: true
|
|
788
|
+
options:
|
|
789
|
+
path: /etc/grafana/provisioning/dashboards
|
|
790
|
+
```
|
|
791
|
+
|
|
792
|
+
### 3. 告警规则
|
|
793
|
+
|
|
794
|
+
创建 `docker/prometheus/rules/app-alerts.yml`:
|
|
795
|
+
|
|
796
|
+
```yaml
|
|
797
|
+
groups:
|
|
798
|
+
- name: dp-koa-framework
|
|
799
|
+
rules:
|
|
800
|
+
- alert: HighErrorRate
|
|
801
|
+
expr: rate(http_requests_total{status=~"5.."}[5m]) > 0.1
|
|
802
|
+
for: 5m
|
|
803
|
+
labels:
|
|
804
|
+
severity: warning
|
|
805
|
+
annotations:
|
|
806
|
+
summary: "High error rate detected"
|
|
807
|
+
description: "Error rate is {{ $value }} errors per second"
|
|
808
|
+
|
|
809
|
+
- alert: HighResponseTime
|
|
810
|
+
expr: histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m])) > 1
|
|
811
|
+
for: 5m
|
|
812
|
+
labels:
|
|
813
|
+
severity: warning
|
|
814
|
+
annotations:
|
|
815
|
+
summary: "High response time detected"
|
|
816
|
+
description: "95th percentile response time is {{ $value }} seconds"
|
|
817
|
+
|
|
818
|
+
- alert: DatabaseConnectionDown
|
|
819
|
+
expr: up{job="mysql"} == 0
|
|
820
|
+
for: 1m
|
|
821
|
+
labels:
|
|
822
|
+
severity: critical
|
|
823
|
+
annotations:
|
|
824
|
+
summary: "Database connection down"
|
|
825
|
+
description: "MySQL database is not responding"
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
## 🔒 安全配置
|
|
829
|
+
|
|
830
|
+
### 1. SSL/TLS 配置
|
|
831
|
+
|
|
832
|
+
#### 使用 Let's Encrypt
|
|
833
|
+
|
|
834
|
+
```bash
|
|
835
|
+
# 安装 Certbot
|
|
836
|
+
sudo apt install certbot python3-certbot-nginx
|
|
837
|
+
|
|
838
|
+
# 获取证书
|
|
839
|
+
sudo certbot --nginx -d your-domain.com
|
|
840
|
+
|
|
841
|
+
# 自动续期
|
|
842
|
+
sudo crontab -e
|
|
843
|
+
# 添加以下行
|
|
844
|
+
0 12 * * * /usr/bin/certbot renew --quiet
|
|
845
|
+
```
|
|
846
|
+
|
|
847
|
+
#### 手动配置 SSL
|
|
848
|
+
|
|
849
|
+
```nginx
|
|
850
|
+
server {
|
|
851
|
+
listen 443 ssl http2;
|
|
852
|
+
server_name your-domain.com;
|
|
853
|
+
|
|
854
|
+
ssl_certificate /etc/ssl/certs/your-domain.crt;
|
|
855
|
+
ssl_certificate_key /etc/ssl/private/your-domain.key;
|
|
856
|
+
|
|
857
|
+
# 安全配置
|
|
858
|
+
ssl_protocols TLSv1.2 TLSv1.3;
|
|
859
|
+
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
|
|
860
|
+
ssl_prefer_server_ciphers off;
|
|
861
|
+
ssl_session_cache shared:SSL:10m;
|
|
862
|
+
ssl_session_timeout 10m;
|
|
863
|
+
|
|
864
|
+
# HSTS
|
|
865
|
+
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
|
|
866
|
+
|
|
867
|
+
# 其他安全头
|
|
868
|
+
add_header X-Frame-Options DENY;
|
|
869
|
+
add_header X-Content-Type-Options nosniff;
|
|
870
|
+
add_header X-XSS-Protection "1; mode=block";
|
|
871
|
+
add_header Referrer-Policy "strict-origin-when-cross-origin";
|
|
872
|
+
}
|
|
873
|
+
```
|
|
874
|
+
|
|
875
|
+
### 2. 防火墙配置
|
|
876
|
+
|
|
877
|
+
```bash
|
|
878
|
+
# UFW 配置
|
|
879
|
+
sudo ufw default deny incoming
|
|
880
|
+
sudo ufw default allow outgoing
|
|
881
|
+
sudo ufw allow ssh
|
|
882
|
+
sudo ufw allow 80/tcp
|
|
883
|
+
sudo ufw allow 443/tcp
|
|
884
|
+
sudo ufw enable
|
|
885
|
+
|
|
886
|
+
# iptables 配置
|
|
887
|
+
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
|
|
888
|
+
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
|
|
889
|
+
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
|
|
890
|
+
sudo iptables -A INPUT -j DROP
|
|
891
|
+
```
|
|
892
|
+
|
|
893
|
+
### 3. 应用安全配置
|
|
894
|
+
|
|
895
|
+
```bash
|
|
896
|
+
# 环境变量安全配置
|
|
897
|
+
export NODE_ENV=production
|
|
898
|
+
export JWT_SECRET=$(openssl rand -base64 64)
|
|
899
|
+
export DB_PASSWORD=$(openssl rand -base64 32)
|
|
900
|
+
export REDIS_PASSWORD=$(openssl rand -base64 32)
|
|
901
|
+
|
|
902
|
+
# 文件权限
|
|
903
|
+
chmod 600 .env
|
|
904
|
+
chmod 700 logs/
|
|
905
|
+
chown -R app:app /app
|
|
906
|
+
```
|
|
907
|
+
|
|
908
|
+
## ⚡ 性能优化
|
|
909
|
+
|
|
910
|
+
### 1. 应用优化
|
|
911
|
+
|
|
912
|
+
```bash
|
|
913
|
+
# PM2 集群模式
|
|
914
|
+
pm2 start dist/main.js -i max --name "dp-koa-app"
|
|
915
|
+
|
|
916
|
+
# 内存优化
|
|
917
|
+
export NODE_OPTIONS="--max-old-space-size=2048"
|
|
918
|
+
|
|
919
|
+
# 启用压缩
|
|
920
|
+
export COMPRESSION_ENABLED=true
|
|
921
|
+
```
|
|
922
|
+
|
|
923
|
+
### 2. 数据库优化
|
|
924
|
+
|
|
925
|
+
```sql
|
|
926
|
+
-- MySQL 配置优化
|
|
927
|
+
SET GLOBAL innodb_buffer_pool_size = 1G;
|
|
928
|
+
SET GLOBAL innodb_log_file_size = 256M;
|
|
929
|
+
SET GLOBAL max_connections = 200;
|
|
930
|
+
SET GLOBAL query_cache_size = 64M;
|
|
931
|
+
```
|
|
932
|
+
|
|
933
|
+
### 3. 缓存优化
|
|
934
|
+
|
|
935
|
+
```bash
|
|
936
|
+
# Redis 配置优化
|
|
937
|
+
redis-cli CONFIG SET maxmemory 1gb
|
|
938
|
+
redis-cli CONFIG SET maxmemory-policy allkeys-lru
|
|
939
|
+
redis-cli CONFIG SET save "900 1 300 10 60 10000"
|
|
940
|
+
```
|
|
941
|
+
|
|
942
|
+
### 4. Nginx 优化
|
|
943
|
+
|
|
944
|
+
```nginx
|
|
945
|
+
# 启用 gzip 压缩
|
|
946
|
+
gzip on;
|
|
947
|
+
gzip_vary on;
|
|
948
|
+
gzip_min_length 1024;
|
|
949
|
+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
|
|
950
|
+
|
|
951
|
+
# 连接优化
|
|
952
|
+
worker_processes auto;
|
|
953
|
+
worker_connections 1024;
|
|
954
|
+
|
|
955
|
+
# 缓存配置
|
|
956
|
+
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=app_cache:10m max_size=1g inactive=60m;
|
|
957
|
+
```
|
|
958
|
+
|
|
959
|
+
## 🚨 故障排除
|
|
960
|
+
|
|
961
|
+
### 1. 常见问题
|
|
962
|
+
|
|
963
|
+
#### 应用无法启动
|
|
964
|
+
|
|
965
|
+
```bash
|
|
966
|
+
# 检查日志
|
|
967
|
+
pm2 logs dp-koa-app
|
|
968
|
+
docker logs dp-koa-app
|
|
969
|
+
|
|
970
|
+
# 检查端口占用
|
|
971
|
+
netstat -tulpn | grep :3000
|
|
972
|
+
|
|
973
|
+
# 检查环境变量
|
|
974
|
+
env | grep NODE_ENV
|
|
975
|
+
```
|
|
976
|
+
|
|
977
|
+
#### 数据库连接失败
|
|
978
|
+
|
|
979
|
+
```bash
|
|
980
|
+
# 检查数据库状态
|
|
981
|
+
systemctl status mysql
|
|
982
|
+
docker ps | grep mysql
|
|
983
|
+
|
|
984
|
+
# 测试连接
|
|
985
|
+
mysql -h localhost -u app_user -p app_db
|
|
986
|
+
|
|
987
|
+
# 检查防火墙
|
|
988
|
+
sudo ufw status
|
|
989
|
+
```
|
|
990
|
+
|
|
991
|
+
#### 内存不足
|
|
992
|
+
|
|
993
|
+
```bash
|
|
994
|
+
# 检查内存使用
|
|
995
|
+
free -h
|
|
996
|
+
top -p $(pgrep node)
|
|
997
|
+
|
|
998
|
+
# 增加 swap
|
|
999
|
+
sudo fallocate -l 2G /swapfile
|
|
1000
|
+
sudo chmod 600 /swapfile
|
|
1001
|
+
sudo mkswap /swapfile
|
|
1002
|
+
sudo swapon /swapfile
|
|
1003
|
+
```
|
|
1004
|
+
|
|
1005
|
+
### 2. 监控和告警
|
|
1006
|
+
|
|
1007
|
+
```bash
|
|
1008
|
+
# 设置监控脚本
|
|
1009
|
+
#!/bin/bash
|
|
1010
|
+
# monitor.sh
|
|
1011
|
+
|
|
1012
|
+
# 检查应用状态
|
|
1013
|
+
if ! curl -f http://localhost:3000/health > /dev/null 2>&1; then
|
|
1014
|
+
echo "Application is down!" | mail -s "Alert" admin@example.com
|
|
1015
|
+
fi
|
|
1016
|
+
|
|
1017
|
+
# 检查磁盘空间
|
|
1018
|
+
if [ $(df / | awk 'NR==2 {print $5}' | sed 's/%//') -gt 80 ]; then
|
|
1019
|
+
echo "Disk space is low!" | mail -s "Alert" admin@example.com
|
|
1020
|
+
fi
|
|
1021
|
+
```
|
|
1022
|
+
|
|
1023
|
+
### 3. 备份策略
|
|
1024
|
+
|
|
1025
|
+
```bash
|
|
1026
|
+
# 数据库备份脚本
|
|
1027
|
+
#!/bin/bash
|
|
1028
|
+
# backup.sh
|
|
1029
|
+
|
|
1030
|
+
DATE=$(date +%Y%m%d_%H%M%S)
|
|
1031
|
+
BACKUP_DIR="/backup"
|
|
1032
|
+
DB_NAME="app_db"
|
|
1033
|
+
|
|
1034
|
+
# 创建备份目录
|
|
1035
|
+
mkdir -p $BACKUP_DIR
|
|
1036
|
+
|
|
1037
|
+
# 备份数据库
|
|
1038
|
+
mysqldump -u root -p$MYSQL_ROOT_PASSWORD $DB_NAME > $BACKUP_DIR/db_backup_$DATE.sql
|
|
1039
|
+
|
|
1040
|
+
# 压缩备份
|
|
1041
|
+
gzip $BACKUP_DIR/db_backup_$DATE.sql
|
|
1042
|
+
|
|
1043
|
+
# 删除7天前的备份
|
|
1044
|
+
find $BACKUP_DIR -name "db_backup_*.sql.gz" -mtime +7 -delete
|
|
1045
|
+
```
|
|
1046
|
+
|
|
1047
|
+
## 📚 相关文档
|
|
1048
|
+
|
|
1049
|
+
- [安装配置指南](INSTALLATION_GUIDE.md) - 详细安装步骤
|
|
1050
|
+
- [开发指南](DEVELOPMENT_GUIDE.md) - 开发规范和最佳实践
|
|
1051
|
+
- [API 文档](API_DOCUMENTATION.md) - API 参考
|
|
1052
|
+
- [故障排除指南](TROUBLESHOOTING.md) - 常见问题解决
|
|
1053
|
+
|
|
1054
|
+
---
|
|
1055
|
+
|
|
1056
|
+
**相关链接**:
|
|
1057
|
+
- [Docker 官方文档](https://docs.docker.com/) - Docker 使用指南
|
|
1058
|
+
- [Kubernetes 官方文档](https://kubernetes.io/docs/) - K8s 部署指南
|
|
1059
|
+
- [Nginx 配置指南](https://nginx.org/en/docs/) - Nginx 配置参考
|
|
1060
|
+
- [Prometheus 文档](https://prometheus.io/docs/) - 监控系统配置
|
|
1061
|
+
|
|
1062
|
+
|