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.
Files changed (235) hide show
  1. package/README.md +50 -0
  2. package/index.mjs +97 -0
  3. package/package.json +33 -0
  4. package/template/.env.development +9 -0
  5. package/template/.env.production +12 -0
  6. package/template/.github/workflows/ci-cd.yml +182 -0
  7. package/template/.trae/documents/controller_development_plan.md +386 -0
  8. package/template/.trae/skills/00-backend-core.skill.md +50 -0
  9. package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
  10. package/template/.trae/skills/10-backend-api.skill.md +54 -0
  11. package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
  12. package/template/.trae/skills/20-backend-repository.skill.md +25 -0
  13. package/template/.trae/skills/21-backend-service.skill.md +135 -0
  14. package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
  15. package/template/.trae/skills/30-backend-validation.skill.md +320 -0
  16. package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
  17. package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
  18. package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
  19. package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
  20. package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
  21. package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
  22. package/template/.trae/skills/90-backend-testing.skill.md +29 -0
  23. package/template/.trae/skills/README.md +49 -0
  24. package/template/.vscode/launch.json +38 -0
  25. package/template/.vscode/settings.json +1 -0
  26. package/template/Dockerfile +36 -0
  27. package/template/README.md +229 -0
  28. package/template/docker-compose.yml +135 -0
  29. package/template/docs/API_DOCUMENTATION.md +837 -0
  30. package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
  31. package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
  32. package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
  33. package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
  34. package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
  35. package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
  36. package/template/docs/DOCUMENTATION_INDEX.md +294 -0
  37. package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
  38. package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
  39. package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
  40. package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
  41. package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
  42. package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
  43. package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
  44. package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
  45. package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
  46. package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
  47. package/template/docs/INSTALLATION_GUIDE.md +611 -0
  48. package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
  49. package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
  50. package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
  51. package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
  52. package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
  53. package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
  54. package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
  55. package/template/docs/QUICK_START.md +268 -0
  56. package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
  57. package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
  58. package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
  59. package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
  60. package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
  61. package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
  62. package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
  63. package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
  64. package/template/docs/TROUBLESHOOTING.md +869 -0
  65. package/template/env.production.example +62 -0
  66. package/template/jest.config.js +34 -0
  67. package/template/package-lock.json +13240 -0
  68. package/template/package.json +119 -0
  69. package/template/patches/typeorm+0.3.25.patch +22 -0
  70. package/template/scripts/sync-template.mjs +84 -0
  71. package/template/scripts/test-annotation-system.sh +48 -0
  72. package/template/scripts/test-core-functionality.sh +28 -0
  73. package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
  74. package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
  75. package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
  76. package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
  77. package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
  78. package/template/src/annotations/index.ts +50 -0
  79. package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
  80. package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
  81. package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
  82. package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
  83. package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
  84. package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
  85. package/template/src/annotations/processors/index.ts +10 -0
  86. package/template/src/app.ts +123 -0
  87. package/template/src/controllers/base.controller.ts +41 -0
  88. package/template/src/controllers/cacheManagement.controller.ts +131 -0
  89. package/template/src/controllers/captcha.controller.ts +57 -0
  90. package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
  91. package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
  92. package/template/src/controllers/example/ExampleController.ts +110 -0
  93. package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
  94. package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
  95. package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
  96. package/template/src/controllers/health.controller.ts +235 -0
  97. package/template/src/controllers/home/register.controller.ts +58 -0
  98. package/template/src/controllers/home/ytGoods.controller.ts +92 -0
  99. package/template/src/controllers/home/ytShop.controller.ts +135 -0
  100. package/template/src/controllers/home/ytUser.controller.ts +89 -0
  101. package/template/src/controllers/logManagement.controller.ts +396 -0
  102. package/template/src/controllers/public/emailSend.controller.ts +65 -0
  103. package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
  104. package/template/src/controllers/testData.controller.ts +253 -0
  105. package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
  106. package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
  107. package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
  108. package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
  109. package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
  110. package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
  111. package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
  112. package/template/src/dto/goods.dto.ts +212 -0
  113. package/template/src/dto/service/ytService.dto.ts +13 -0
  114. package/template/src/dto/user.dto.ts +177 -0
  115. package/template/src/entity/base.entity.ts +13 -0
  116. package/template/src/entity/columnTypes.ts +13 -0
  117. package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
  118. package/template/src/entity/goodsUnlocker.entity.ts +34 -0
  119. package/template/src/entity/index.ts +15 -0
  120. package/template/src/entity/shop.entity.ts +52 -0
  121. package/template/src/entity/shopUser.entity.ts +41 -0
  122. package/template/src/entity/ytGoods.entity.ts +94 -0
  123. package/template/src/entity/ytUser.entity.ts +96 -0
  124. package/template/src/examples/InterceptorExampleRunner.ts +284 -0
  125. package/template/src/examples/ServiceInterceptorExample.ts +214 -0
  126. package/template/src/examples/SwaggerProcessorExample.ts +169 -0
  127. package/template/src/examples/TransactionManagerDemo.ts +377 -0
  128. package/template/src/examples/cacheExamples.ts +155 -0
  129. package/template/src/framework/decorator/controller.ts +311 -0
  130. package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
  131. package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
  132. package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
  133. package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
  134. package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
  135. package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
  136. package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
  137. package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
  138. package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
  139. package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
  140. package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
  141. package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
  142. package/template/src/framework/decorator/refactored/index.ts +98 -0
  143. package/template/src/framework/decorator/swagger.ts +150 -0
  144. package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
  145. package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
  146. package/template/src/framework/interceptors/index.ts +19 -0
  147. package/template/src/framework/plugins/registry.ts +63 -0
  148. package/template/src/framework/plugins/types.ts +15 -0
  149. package/template/src/framework/types/ServiceResult.ts +151 -0
  150. package/template/src/framework/types/index.ts +16 -0
  151. package/template/src/framework/utils/CacheManager.ts +430 -0
  152. package/template/src/framework/utils/CacheService.ts +248 -0
  153. package/template/src/framework/utils/DtoValidator.ts +164 -0
  154. package/template/src/framework/utils/MigrationHelper.ts +179 -0
  155. package/template/src/framework/utils/MigrationManager.ts +256 -0
  156. package/template/src/framework/utils/NewRouter.ts +207 -0
  157. package/template/src/framework/utils/TransactionManager.ts +172 -0
  158. package/template/src/framework/utils/bootstrap.ts +445 -0
  159. package/template/src/framework/utils/cache.ts +269 -0
  160. package/template/src/framework/utils/databaseConfig.ts +148 -0
  161. package/template/src/framework/utils/db.ts +39 -0
  162. package/template/src/framework/utils/dbMonitor.ts +106 -0
  163. package/template/src/framework/utils/dynamicSwagger.ts +410 -0
  164. package/template/src/framework/utils/function.ts +61 -0
  165. package/template/src/framework/utils/gracefulShutdown.ts +131 -0
  166. package/template/src/framework/utils/logger.ts +388 -0
  167. package/template/src/framework/utils/metrics.ts +182 -0
  168. package/template/src/framework/utils/router.ts +417 -0
  169. package/template/src/framework/utils/swagger.ts +184 -0
  170. package/template/src/framework/utils/testDb.ts +19 -0
  171. package/template/src/framework/utils/token.ts +23 -0
  172. package/template/src/framework/utils/transform.ts +17 -0
  173. package/template/src/libs/aokEmailSender.ts +42 -0
  174. package/template/src/libs/captcha.ts +37 -0
  175. package/template/src/libs/cos.ts +45 -0
  176. package/template/src/libs/mCache.ts +7 -0
  177. package/template/src/libs/serviceValidate.ts +3 -0
  178. package/template/src/libs/tecentSms.ts +51 -0
  179. package/template/src/middlewares/a.middleware.ts +6 -0
  180. package/template/src/middlewares/error.middleware.ts +14 -0
  181. package/template/src/middlewares/logging.middleware.ts +187 -0
  182. package/template/src/middlewares/static.middleware.ts +79 -0
  183. package/template/src/middlewares/swagger.middleware.ts +70 -0
  184. package/template/src/middlewares/token.middleware.ts +32 -0
  185. package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
  186. package/template/src/migrations/index.ts +6 -0
  187. package/template/src/plugins/weboffice/core/context.ts +47 -0
  188. package/template/src/plugins/weboffice/core/errors.ts +51 -0
  189. package/template/src/plugins/weboffice/core/types.ts +63 -0
  190. package/template/src/plugins/weboffice/core/utils.ts +7 -0
  191. package/template/src/plugins/weboffice/entities/index.ts +3 -0
  192. package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
  193. package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
  194. package/template/src/plugins/weboffice/http/routes.ts +179 -0
  195. package/template/src/plugins/weboffice/index.ts +23 -0
  196. package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
  197. package/template/src/repository/UserRepository.ts +122 -0
  198. package/template/src/repository/base/BaseRepository.ts +124 -0
  199. package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
  200. package/template/src/routers/index.ts +49 -0
  201. package/template/src/service/base.service.ts +116 -0
  202. package/template/src/service/paramValidateTest.service.ts +139 -0
  203. package/template/src/service/ytGoods.service.ts +42 -0
  204. package/template/src/service/ytShop.service.ts +90 -0
  205. package/template/src/service/ytUser.service.ts +451 -0
  206. package/template/src/test/swaggerParameterTest.ts +90 -0
  207. package/template/src/utils/testDataInitializer.ts +296 -0
  208. package/template/static/output.json +15203 -0
  209. package/template/test/controllers/controllers.test.ts +173 -0
  210. package/template/test/controllers/example/ExampleController.test.ts +222 -0
  211. package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
  212. package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
  213. package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
  214. package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
  215. package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
  216. package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
  217. package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
  218. package/template/test/framework/annotation/NewRouter.test.ts +272 -0
  219. package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
  220. package/template/test/framework/annotation/setup.ts +26 -0
  221. package/template/test/framework/cache.test.ts +101 -0
  222. package/template/test/framework/databaseConfig.test.ts +142 -0
  223. package/template/test/integration/integration.test.ts +153 -0
  224. package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
  225. package/template/test/service/business.test.ts +87 -0
  226. package/template/test/service/paramValidateTest.service.test.ts +184 -0
  227. package/template/test/service/ytUser.service.test.ts +566 -0
  228. package/template/test/setup.ts +20 -0
  229. package/template/test/setupAfterEnv.ts +14 -0
  230. package/template/test/utils/testHelpers.ts +220 -0
  231. package/template/test_output.txt +0 -0
  232. package/template/tsconfig.build.json +17 -0
  233. package/template/tsconfig.json +31 -0
  234. package/template/webpack.config.js +71 -0
  235. package/template/yarn.lock +7354 -0
@@ -0,0 +1,869 @@
1
+ # 故障排除指南
2
+
3
+ 本指南提供 DP-Koa Framework 常见问题的诊断和解决方案,帮助您快速定位和解决问题。
4
+
5
+ ## 📋 目录
6
+
7
+ - [故障诊断流程](#故障诊断流程)
8
+ - [启动问题](#启动问题)
9
+ - [数据库问题](#数据库问题)
10
+ - [网络连接问题](#网络连接问题)
11
+ - [性能问题](#性能问题)
12
+ - [内存问题](#内存问题)
13
+ - [权限问题](#权限问题)
14
+ - [配置问题](#配置问题)
15
+ - [日志分析](#日志分析)
16
+ - [监控和告警](#监控和告警)
17
+ - [紧急恢复](#紧急恢复)
18
+
19
+ ## 🔍 故障诊断流程
20
+
21
+ ### 1. 问题分类
22
+
23
+ | 问题类型 | 症状 | 优先级 | 影响范围 |
24
+ |----------|------|--------|----------|
25
+ | 启动失败 | 应用无法启动 | 高 | 全部功能 |
26
+ | 数据库连接 | 数据操作失败 | 高 | 核心功能 |
27
+ | 网络问题 | API 请求失败 | 中 | 部分功能 |
28
+ | 性能问题 | 响应缓慢 | 中 | 用户体验 |
29
+ | 内存泄漏 | 内存持续增长 | 低 | 长期稳定性 |
30
+
31
+ ### 2. 诊断步骤
32
+
33
+ ```bash
34
+ # 1. 检查应用状态
35
+ curl http://localhost:3000/health
36
+
37
+ # 2. 检查系统资源
38
+ top
39
+ free -h
40
+ df -h
41
+
42
+ # 3. 检查网络连接
43
+ netstat -tulpn | grep :3000
44
+ ss -tulpn | grep :3000
45
+
46
+ # 4. 检查日志
47
+ tail -f logs/error.$(date +%Y-%m-%d).log
48
+ tail -f logs/log.$(date +%Y-%m-%d).log
49
+
50
+ # 5. 检查进程状态
51
+ ps aux | grep node
52
+ pm2 status
53
+ ```
54
+
55
+ ## 🚀 启动问题
56
+
57
+ ### 1. 应用无法启动
58
+
59
+ #### 症状
60
+ - 应用启动后立即退出
61
+ - 端口无法绑定
62
+ - 依赖加载失败
63
+
64
+ #### 诊断步骤
65
+
66
+ ```bash
67
+ # 检查 Node.js 版本
68
+ node --version
69
+ npm --version
70
+
71
+ # 检查依赖安装
72
+ npm list --depth=0
73
+
74
+ # 检查端口占用
75
+ lsof -i :3000
76
+ netstat -tulpn | grep :3000
77
+
78
+ # 检查环境变量
79
+ env | grep NODE_ENV
80
+ env | grep DB_
81
+
82
+ # 手动启动查看错误
83
+ node dist/main.js
84
+ ```
85
+
86
+ #### 常见解决方案
87
+
88
+ **端口被占用**:
89
+ ```bash
90
+ # 查找占用进程
91
+ lsof -i :3000
92
+
93
+ # 杀死进程
94
+ kill -9 <PID>
95
+
96
+ # 或使用其他端口
97
+ PORT=3001 npm start
98
+ ```
99
+
100
+ **依赖缺失**:
101
+ ```bash
102
+ # 重新安装依赖
103
+ rm -rf node_modules package-lock.json
104
+ npm install
105
+
106
+ # 检查依赖版本冲突
107
+ npm ls
108
+ ```
109
+
110
+ **环境变量问题**:
111
+ ```bash
112
+ # 检查 .env 文件
113
+ cat .env
114
+
115
+ # 验证环境变量
116
+ node -e "console.log(process.env.NODE_ENV)"
117
+ ```
118
+
119
+ ### 2. Docker 容器启动失败
120
+
121
+ #### 症状
122
+ - 容器启动后立即退出
123
+ - 健康检查失败
124
+ - 日志显示错误信息
125
+
126
+ #### 诊断步骤
127
+
128
+ ```bash
129
+ # 查看容器状态
130
+ docker ps -a
131
+
132
+ # 查看容器日志
133
+ docker logs <container-name>
134
+
135
+ # 进入容器调试
136
+ docker exec -it <container-name> sh
137
+
138
+ # 检查镜像
139
+ docker images dp-koa-framework
140
+ ```
141
+
142
+ #### 常见解决方案
143
+
144
+ **内存不足**:
145
+ ```bash
146
+ # 检查系统内存
147
+ free -h
148
+
149
+ # 增加 swap
150
+ sudo fallocate -l 2G /swapfile
151
+ sudo chmod 600 /swapfile
152
+ sudo mkswap /swapfile
153
+ sudo swapon /swapfile
154
+ ```
155
+
156
+ **权限问题**:
157
+ ```bash
158
+ # 检查文件权限
159
+ ls -la dist/main.js
160
+
161
+ # 修复权限
162
+ chmod +x dist/main.js
163
+ chown -R nodejs:nodejs /app
164
+ ```
165
+
166
+ ## 🗄️ 数据库问题
167
+
168
+ ### 1. 数据库连接失败
169
+
170
+ #### 症状
171
+ - 应用启动时报数据库连接错误
172
+ - API 请求返回数据库相关错误
173
+ - 健康检查显示数据库不可用
174
+
175
+ #### 诊断步骤
176
+
177
+ ```bash
178
+ # 检查数据库服务状态
179
+ systemctl status mysql
180
+ docker ps | grep mysql
181
+
182
+ # 测试数据库连接
183
+ mysql -h localhost -u app_user -p app_db
184
+
185
+ # 检查数据库配置
186
+ cat .env | grep DB_
187
+
188
+ # 检查网络连接
189
+ telnet localhost 3306
190
+ ```
191
+
192
+ #### 常见解决方案
193
+
194
+ **MySQL 服务未启动**:
195
+ ```bash
196
+ # 启动 MySQL 服务
197
+ sudo systemctl start mysql
198
+ sudo systemctl enable mysql
199
+
200
+ # Docker 环境
201
+ docker start mysql-container
202
+ ```
203
+
204
+ **连接配置错误**:
205
+ ```bash
206
+ # 检查配置文件
207
+ cat .env
208
+
209
+ # 验证连接参数
210
+ mysql -h $DB_HOST -P $DB_PORT -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE
211
+ ```
212
+
213
+ **防火墙阻止**:
214
+ ```bash
215
+ # 检查防火墙状态
216
+ sudo ufw status
217
+ sudo iptables -L
218
+
219
+ # 开放数据库端口
220
+ sudo ufw allow 3306
221
+ ```
222
+
223
+ ### 2. 数据库性能问题
224
+
225
+ #### 症状
226
+ - 查询响应缓慢
227
+ - 连接池耗尽
228
+ - 数据库 CPU 使用率高
229
+
230
+ #### 诊断步骤
231
+
232
+ ```bash
233
+ # 检查数据库状态
234
+ mysql -u root -p -e "SHOW PROCESSLIST;"
235
+ mysql -u root -p -e "SHOW STATUS LIKE 'Connections';"
236
+
237
+ # 检查慢查询
238
+ mysql -u root -p -e "SHOW VARIABLES LIKE 'slow_query_log';"
239
+ mysql -u root -p -e "SHOW VARIABLES LIKE 'long_query_time';"
240
+ ```
241
+
242
+ #### 常见解决方案
243
+
244
+ **连接池配置**:
245
+ ```bash
246
+ # 增加连接池大小
247
+ export DB_POOL_MAX=50
248
+ export DB_POOL_MIN=10
249
+ ```
250
+
251
+ **查询优化**:
252
+ ```sql
253
+ -- 添加索引
254
+ CREATE INDEX idx_user_email ON users(email);
255
+ CREATE INDEX idx_goods_status ON goods(status);
256
+
257
+ -- 分析查询计划
258
+ EXPLAIN SELECT * FROM users WHERE email = 'user@example.com';
259
+ ```
260
+
261
+ ### 3. 数据库迁移问题
262
+
263
+ #### 症状
264
+ - 迁移执行失败
265
+ - 表结构不匹配
266
+ - 数据丢失
267
+
268
+ #### 诊断步骤
269
+
270
+ ```bash
271
+ # 检查迁移状态
272
+ npm run migration:show
273
+
274
+ # 查看迁移日志
275
+ tail -f logs/migration.log
276
+
277
+ # 检查数据库结构
278
+ mysql -u root -p -e "DESCRIBE users;"
279
+ ```
280
+
281
+ #### 常见解决方案
282
+
283
+ **回滚迁移**:
284
+ ```bash
285
+ # 回滚到上一个版本
286
+ npm run migration:revert
287
+
288
+ # 手动修复数据
289
+ mysql -u root -p app_db < backup.sql
290
+ ```
291
+
292
+ ## 🌐 网络连接问题
293
+
294
+ ### 1. API 请求失败
295
+
296
+ #### 症状
297
+ - HTTP 请求超时
298
+ - 连接被拒绝
299
+ - 响应状态码异常
300
+
301
+ #### 诊断步骤
302
+
303
+ ```bash
304
+ # 测试本地连接
305
+ curl -v http://localhost:3000/health
306
+
307
+ # 测试外部连接
308
+ curl -v http://your-domain.com/health
309
+
310
+ # 检查 DNS 解析
311
+ nslookup your-domain.com
312
+ dig your-domain.com
313
+
314
+ # 检查路由
315
+ traceroute your-domain.com
316
+ ```
317
+
318
+ #### 常见解决方案
319
+
320
+ **端口未开放**:
321
+ ```bash
322
+ # 检查防火墙
323
+ sudo ufw status
324
+ sudo iptables -L
325
+
326
+ # 开放端口
327
+ sudo ufw allow 3000
328
+ sudo ufw allow 80
329
+ sudo ufw allow 443
330
+ ```
331
+
332
+ **代理配置问题**:
333
+ ```bash
334
+ # 检查 Nginx 配置
335
+ sudo nginx -t
336
+ sudo nginx -s reload
337
+
338
+ # 检查代理设置
339
+ curl -H "Host: your-domain.com" http://localhost:3000/health
340
+ ```
341
+
342
+ ### 2. SSL/TLS 问题
343
+
344
+ #### 症状
345
+ - HTTPS 连接失败
346
+ - 证书错误
347
+ - 安全警告
348
+
349
+ #### 诊断步骤
350
+
351
+ ```bash
352
+ # 检查证书
353
+ openssl x509 -in /path/to/certificate.crt -text -noout
354
+
355
+ # 测试 SSL 连接
356
+ openssl s_client -connect your-domain.com:443
357
+
358
+ # 检查证书链
359
+ curl -vI https://your-domain.com
360
+ ```
361
+
362
+ #### 常见解决方案
363
+
364
+ **证书过期**:
365
+ ```bash
366
+ # 更新 Let's Encrypt 证书
367
+ sudo certbot renew
368
+
369
+ # 手动更新证书
370
+ sudo cp new-certificate.crt /etc/ssl/certs/
371
+ sudo cp new-private.key /etc/ssl/private/
372
+ sudo systemctl reload nginx
373
+ ```
374
+
375
+ **证书配置错误**:
376
+ ```nginx
377
+ # 检查 Nginx SSL 配置
378
+ server {
379
+ listen 443 ssl;
380
+ ssl_certificate /path/to/certificate.crt;
381
+ ssl_certificate_key /path/to/private.key;
382
+ ssl_protocols TLSv1.2 TLSv1.3;
383
+ }
384
+ ```
385
+
386
+ ## ⚡ 性能问题
387
+
388
+ ### 1. 响应缓慢
389
+
390
+ #### 症状
391
+ - API 响应时间过长
392
+ - 用户界面加载缓慢
393
+ - 系统资源使用率高
394
+
395
+ #### 诊断步骤
396
+
397
+ ```bash
398
+ # 检查系统负载
399
+ top
400
+ htop
401
+ iostat -x 1
402
+
403
+ # 检查网络延迟
404
+ ping google.com
405
+ traceroute api.your-domain.com
406
+
407
+ # 检查应用性能
408
+ curl -w "@curl-format.txt" -o /dev/null -s http://localhost:3000/health
409
+ ```
410
+
411
+ 创建 `curl-format.txt`:
412
+ ```
413
+ time_namelookup: %{time_namelookup}\n
414
+ time_connect: %{time_connect}\n
415
+ time_appconnect: %{time_appconnect}\n
416
+ time_pretransfer: %{time_pretransfer}\n
417
+ time_redirect: %{time_redirect}\n
418
+ time_starttransfer: %{time_starttransfer}\n
419
+ ----------\n
420
+ time_total: %{time_total}\n
421
+ ```
422
+
423
+ #### 常见解决方案
424
+
425
+ **数据库查询优化**:
426
+ ```sql
427
+ -- 添加索引
428
+ CREATE INDEX idx_created_at ON orders(created_at);
429
+ CREATE INDEX idx_user_status ON users(status);
430
+
431
+ -- 优化查询
432
+ EXPLAIN SELECT * FROM users WHERE status = 'active' ORDER BY created_at DESC LIMIT 10;
433
+ ```
434
+
435
+ **缓存配置**:
436
+ ```bash
437
+ # 启用 Redis 缓存
438
+ export CACHE_TYPE=redis
439
+ export REDIS_HOST=localhost
440
+ export REDIS_PORT=6379
441
+
442
+ # 调整缓存 TTL
443
+ export CACHE_TTL=3600
444
+ ```
445
+
446
+ **应用优化**:
447
+ ```bash
448
+ # 启用集群模式
449
+ pm2 start dist/main.js -i max
450
+
451
+ # 调整内存限制
452
+ export NODE_OPTIONS="--max-old-space-size=2048"
453
+ ```
454
+
455
+ ### 2. 内存使用过高
456
+
457
+ #### 症状
458
+ - 系统内存不足
459
+ - 应用崩溃
460
+ - 交换空间使用率高
461
+
462
+ #### 诊断步骤
463
+
464
+ ```bash
465
+ # 检查内存使用
466
+ free -h
467
+ top -p $(pgrep node)
468
+
469
+ # 检查内存泄漏
470
+ node --inspect dist/main.js
471
+ # 然后在 Chrome 中打开 chrome://inspect
472
+
473
+ # 检查交换使用
474
+ swapon -s
475
+ ```
476
+
477
+ #### 常见解决方案
478
+
479
+ **增加系统内存**:
480
+ ```bash
481
+ # 增加 swap
482
+ sudo fallocate -l 4G /swapfile
483
+ sudo chmod 600 /swapfile
484
+ sudo mkswap /swapfile
485
+ sudo swapon /swapfile
486
+
487
+ # 永久挂载
488
+ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
489
+ ```
490
+
491
+ **应用内存优化**:
492
+ ```bash
493
+ # 限制 Node.js 内存
494
+ export NODE_OPTIONS="--max-old-space-size=1024"
495
+
496
+ # 启用垃圾回收
497
+ export NODE_OPTIONS="--expose-gc"
498
+ ```
499
+
500
+ ## 🔐 权限问题
501
+
502
+ ### 1. 文件权限错误
503
+
504
+ #### 症状
505
+ - 无法读取配置文件
506
+ - 日志文件无法写入
507
+ - 上传文件失败
508
+
509
+ #### 诊断步骤
510
+
511
+ ```bash
512
+ # 检查文件权限
513
+ ls -la .env
514
+ ls -la logs/
515
+ ls -la uploads/
516
+
517
+ # 检查用户权限
518
+ whoami
519
+ groups
520
+ id
521
+
522
+ # 检查 SELinux 状态
523
+ sestatus
524
+ getenforce
525
+ ```
526
+
527
+ #### 常见解决方案
528
+
529
+ **修复文件权限**:
530
+ ```bash
531
+ # 修复应用目录权限
532
+ sudo chown -R app:app /app
533
+ sudo chmod -R 755 /app
534
+
535
+ # 修复日志目录权限
536
+ sudo chmod 755 logs/
537
+ sudo chmod 644 logs/*.log
538
+
539
+ # 修复上传目录权限
540
+ sudo chmod 755 uploads/
541
+ sudo chmod 644 uploads/*
542
+ ```
543
+
544
+ **SELinux 配置**:
545
+ ```bash
546
+ # 临时禁用 SELinux
547
+ sudo setenforce 0
548
+
549
+ # 永久禁用 SELinux
550
+ sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
551
+ ```
552
+
553
+ ### 2. 数据库权限问题
554
+
555
+ #### 症状
556
+ - 数据库连接被拒绝
557
+ - 表操作失败
558
+ - 迁移执行失败
559
+
560
+ #### 诊断步骤
561
+
562
+ ```bash
563
+ # 测试数据库连接
564
+ mysql -u app_user -p app_db
565
+
566
+ # 检查用户权限
567
+ mysql -u root -p -e "SHOW GRANTS FOR 'app_user'@'localhost';"
568
+
569
+ # 检查数据库权限
570
+ mysql -u root -p -e "SELECT * FROM mysql.user WHERE User='app_user';"
571
+ ```
572
+
573
+ #### 常见解决方案
574
+
575
+ **修复数据库权限**:
576
+ ```sql
577
+ -- 重新授权
578
+ GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';
579
+ FLUSH PRIVILEGES;
580
+
581
+ -- 创建用户
582
+ CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'password';
583
+ GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';
584
+ FLUSH PRIVILEGES;
585
+ ```
586
+
587
+ ## ⚙️ 配置问题
588
+
589
+ ### 1. 环境变量问题
590
+
591
+ #### 症状
592
+ - 配置未生效
593
+ - 默认值被使用
594
+ - 敏感信息泄露
595
+
596
+ #### 诊断步骤
597
+
598
+ ```bash
599
+ # 检查环境变量
600
+ env | grep NODE_ENV
601
+ env | grep DB_
602
+ env | grep JWT_
603
+
604
+ # 检查 .env 文件
605
+ cat .env
606
+
607
+ # 验证配置加载
608
+ node -e "console.log(require('dotenv').config())"
609
+ ```
610
+
611
+ #### 常见解决方案
612
+
613
+ **修复环境变量**:
614
+ ```bash
615
+ # 重新加载环境变量
616
+ source .env
617
+
618
+ # 检查变量格式
619
+ echo $NODE_ENV
620
+ echo $DB_HOST
621
+
622
+ # 验证必需变量
623
+ required_vars=("NODE_ENV" "DB_HOST" "DB_PASSWORD" "JWT_SECRET")
624
+ for var in "${required_vars[@]}"; do
625
+ if [ -z "${!var}" ]; then
626
+ echo "Error: $var is not set"
627
+ exit 1
628
+ fi
629
+ done
630
+ ```
631
+
632
+ ### 2. 配置文件错误
633
+
634
+ #### 症状
635
+ - 应用启动失败
636
+ - 功能异常
637
+ - 日志显示配置错误
638
+
639
+ #### 诊断步骤
640
+
641
+ ```bash
642
+ # 检查配置文件语法
643
+ node -c dist/main.js
644
+
645
+ # 检查 JSON 配置
646
+ jq . config.json
647
+
648
+ # 检查 YAML 配置
649
+ yamllint config.yml
650
+ ```
651
+
652
+ #### 常见解决方案
653
+
654
+ **修复配置文件**:
655
+ ```bash
656
+ # 备份配置文件
657
+ cp config.json config.json.backup
658
+
659
+ # 验证配置
660
+ node -e "console.log(JSON.parse(require('fs').readFileSync('config.json', 'utf8')))"
661
+
662
+ # 重置为默认配置
663
+ cp config.json.example config.json
664
+ ```
665
+
666
+ ## 📊 日志分析
667
+
668
+ ### 1. 日志文件位置
669
+
670
+ ```bash
671
+ # 应用日志
672
+ logs/log.$(date +%Y-%m-%d).log
673
+ logs/error.$(date +%Y-%m-%d).log
674
+ logs/performance.$(date +%Y-%m-%d).log
675
+
676
+ # 系统日志
677
+ /var/log/syslog
678
+ /var/log/nginx/access.log
679
+ /var/log/nginx/error.log
680
+
681
+ # Docker 日志
682
+ docker logs <container-name>
683
+ ```
684
+
685
+ ### 2. 日志分析工具
686
+
687
+ ```bash
688
+ # 实时查看日志
689
+ tail -f logs/error.$(date +%Y-%m-%d).log
690
+
691
+ # 搜索错误
692
+ grep -i "error" logs/*.log
693
+
694
+ # 统计错误数量
695
+ grep -c "ERROR" logs/error.$(date +%Y-%m-%d).log
696
+
697
+ # 分析访问模式
698
+ awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr
699
+ ```
700
+
701
+ ### 3. 常见错误模式
702
+
703
+ **数据库连接错误**:
704
+ ```
705
+ ERROR: Database connection failed: connect ECONNREFUSED 127.0.0.1:3306
706
+ ```
707
+
708
+ **内存不足错误**:
709
+ ```
710
+ FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
711
+ ```
712
+
713
+ **权限错误**:
714
+ ```
715
+ Error: EACCES: permission denied, open '/app/logs/app.log'
716
+ ```
717
+
718
+ ## 🚨 监控和告警
719
+
720
+ ### 1. 健康检查
721
+
722
+ ```bash
723
+ # 基础健康检查
724
+ curl -f http://localhost:3000/health
725
+
726
+ # 详细健康检查
727
+ curl -f http://localhost:3000/health/detailed
728
+
729
+ # 数据库健康检查
730
+ curl -f http://localhost:3000/health/db
731
+
732
+ # 缓存健康检查
733
+ curl -f http://localhost:3000/health/cache
734
+ ```
735
+
736
+ ### 2. 监控脚本
737
+
738
+ ```bash
739
+ #!/bin/bash
740
+ # monitor.sh
741
+
742
+ APP_URL="http://localhost:3000"
743
+ EMAIL="admin@example.com"
744
+
745
+ # 检查应用状态
746
+ if ! curl -f $APP_URL/health > /dev/null 2>&1; then
747
+ echo "Application is down!" | mail -s "Alert: Application Down" $EMAIL
748
+ fi
749
+
750
+ # 检查磁盘空间
751
+ DISK_USAGE=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
752
+ if [ $DISK_USAGE -gt 80 ]; then
753
+ echo "Disk usage is ${DISK_USAGE}%" | mail -s "Alert: High Disk Usage" $EMAIL
754
+ fi
755
+
756
+ # 检查内存使用
757
+ MEMORY_USAGE=$(free | awk 'NR==2{printf "%.0f", $3*100/$2}')
758
+ if [ $MEMORY_USAGE -gt 90 ]; then
759
+ echo "Memory usage is ${MEMORY_USAGE}%" | mail -s "Alert: High Memory Usage" $EMAIL
760
+ fi
761
+ ```
762
+
763
+ ### 3. 告警配置
764
+
765
+ ```bash
766
+ # 设置定时任务
767
+ crontab -e
768
+
769
+ # 每5分钟检查一次
770
+ */5 * * * * /path/to/monitor.sh
771
+
772
+ # 每小时检查一次磁盘
773
+ 0 * * * * df -h | awk '$5 > 80 {print $0}' | mail -s "Disk Usage Alert" admin@example.com
774
+ ```
775
+
776
+ ## 🆘 紧急恢复
777
+
778
+ ### 1. 应用崩溃恢复
779
+
780
+ ```bash
781
+ # 重启应用
782
+ pm2 restart dp-koa-app
783
+
784
+ # 或使用 Docker
785
+ docker restart dp-koa-container
786
+
787
+ # 检查恢复状态
788
+ curl http://localhost:3000/health
789
+ ```
790
+
791
+ ### 2. 数据库恢复
792
+
793
+ ```bash
794
+ # 从备份恢复
795
+ mysql -u root -p app_db < backup_$(date +%Y%m%d).sql
796
+
797
+ # 检查数据完整性
798
+ mysql -u root -p -e "SELECT COUNT(*) FROM users;"
799
+ ```
800
+
801
+ ### 3. 系统恢复
802
+
803
+ ```bash
804
+ # 重启服务
805
+ sudo systemctl restart nginx
806
+ sudo systemctl restart mysql
807
+
808
+ # 清理临时文件
809
+ sudo rm -rf /tmp/*
810
+ sudo rm -rf /var/tmp/*
811
+
812
+ # 释放内存
813
+ echo 3 | sudo tee /proc/sys/vm/drop_caches
814
+ ```
815
+
816
+ ### 4. 回滚策略
817
+
818
+ ```bash
819
+ # 代码回滚
820
+ git checkout previous-stable-commit
821
+ npm install
822
+ npm run build
823
+ pm2 restart dp-koa-app
824
+
825
+ # 配置回滚
826
+ cp config.json.backup config.json
827
+ pm2 restart dp-koa-app
828
+
829
+ # 数据库回滚
830
+ mysql -u root -p app_db < backup_before_update.sql
831
+ ```
832
+
833
+ ## 📞 获取帮助
834
+
835
+ ### 1. 内部资源
836
+
837
+ - **日志文件**: `logs/` 目录
838
+ - **配置文件**: `.env`, `config.json`
839
+ - **监控面板**: http://localhost:3000/metrics
840
+ - **健康检查**: http://localhost:3000/health
841
+
842
+ ### 2. 外部资源
843
+
844
+ - **GitHub Issues**: [项目问题跟踪](https://github.com/your-repo/issues)
845
+ - **Stack Overflow**: [技术问答社区](https://stackoverflow.com/questions/tagged/node.js)
846
+ - **Node.js 文档**: [官方文档](https://nodejs.org/docs/)
847
+
848
+ ### 3. 联系支持
849
+
850
+ - **邮箱**: support@example.com
851
+ - **紧急联系**: +86-xxx-xxxx-xxxx
852
+ - **工作时间**: 周一至周五 9:00-18:00
853
+
854
+ ## 📚 相关文档
855
+
856
+ - [快速开始指南](QUICK_START.md) - 5分钟快速上手
857
+ - [安装配置指南](INSTALLATION_GUIDE.md) - 详细安装步骤
858
+ - [开发指南](DEVELOPMENT_GUIDE.md) - 开发规范和最佳实践
859
+ - [部署指南](DEPLOYMENT_GUIDE.md) - 生产环境部署
860
+
861
+ ---
862
+
863
+ **紧急情况**:
864
+ - 应用完全无法访问: 立即重启服务
865
+ - 数据库连接失败: 检查数据库服务状态
866
+ - 内存使用过高: 重启应用释放内存
867
+ - 磁盘空间不足: 清理日志和临时文件
868
+
869
+