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,547 @@
1
+ # 🏢 企业级应用部署指南
2
+
3
+ ## 📋 目录
4
+
5
+ - [概述](#概述)
6
+ - [环境要求](#环境要求)
7
+ - [Docker部署](#docker部署)
8
+ - [Kubernetes部署](#kubernetes部署)
9
+ - [监控配置](#监控配置)
10
+ - [安全配置](#安全配置)
11
+ - [性能优化](#性能优化)
12
+ - [故障排除](#故障排除)
13
+
14
+ ## 🎯 概述
15
+
16
+ 本指南提供了dp-koa-framework企业级应用的完整部署方案,包括容器化部署、负载均衡、监控告警、安全防护等企业级特性。
17
+
18
+ ## 🔧 环境要求
19
+
20
+ ### 最低要求
21
+ - **Node.js**: 18.x 或更高版本
22
+ - **内存**: 2GB RAM
23
+ - **CPU**: 2核心
24
+ - **存储**: 10GB 可用空间
25
+
26
+ ### 推荐配置
27
+ - **Node.js**: 18.x LTS
28
+ - **内存**: 8GB RAM
29
+ - **CPU**: 4核心
30
+ - **存储**: 50GB SSD
31
+ - **PM2**: 进程管理器
32
+
33
+ ### 依赖服务
34
+ - **MySQL**: 8.0 或更高版本
35
+ - **Redis**: 7.0 或更高版本
36
+ - **Nginx**: 1.20 或更高版本
37
+
38
+ ## 🐳 Docker部署
39
+
40
+ ### 1. 构建镜像
41
+
42
+ ```bash
43
+ # 构建应用镜像
44
+ docker build -t dp-koa-framework:latest .
45
+
46
+ # 查看镜像
47
+ docker images dp-koa-framework
48
+ ```
49
+
50
+ ### 2. 单容器运行
51
+
52
+ ```bash
53
+ # 运行应用容器
54
+ docker run -d \
55
+ --name dp-koa-app \
56
+ -p 3000:3000 \
57
+ -e NODE_ENV=production \
58
+ -e ENABLE_CLUSTER=true \
59
+ -e DB_HOST=your-mysql-host \
60
+ -e DB_PASSWORD=your-password \
61
+ dp-koa-framework:latest
62
+ ```
63
+
64
+ ### 3. Docker Compose部署
65
+
66
+ ```bash
67
+ # 启动完整服务栈
68
+ docker-compose up -d
69
+
70
+ # 查看服务状态
71
+ docker-compose ps
72
+
73
+ # 查看日志
74
+ docker-compose logs -f app
75
+
76
+ # 停止服务
77
+ docker-compose down
78
+ ```
79
+
80
+ ### 4. PM2进程管理
81
+
82
+ ```bash
83
+ # 安装PM2
84
+ npm install -g pm2
85
+
86
+ # 启动应用
87
+ pm2 start dist/bundle.js --name "dp-koa-framework"
88
+
89
+ # 查看进程状态
90
+ pm2 status
91
+
92
+ # 查看日志
93
+ pm2 logs dp-koa-framework
94
+
95
+ # 重启应用
96
+ pm2 restart dp-koa-framework
97
+
98
+ # 停止应用
99
+ pm2 stop dp-koa-framework
100
+ ```
101
+
102
+ ### 5. 服务验证
103
+
104
+ ```bash
105
+ # 健康检查
106
+ curl http://localhost:3000/health
107
+
108
+ # 指标监控
109
+ curl http://localhost:3000/metrics
110
+ ```
111
+
112
+ ## ☸️ Kubernetes部署
113
+
114
+ ### 1. 创建命名空间
115
+
116
+ ```yaml
117
+ apiVersion: v1
118
+ kind: Namespace
119
+ metadata:
120
+ name: dp-koa-framework
121
+ ```
122
+
123
+ ### 2. 配置ConfigMap
124
+
125
+ ```yaml
126
+ apiVersion: v1
127
+ kind: ConfigMap
128
+ metadata:
129
+ name: app-config
130
+ namespace: dp-koa-framework
131
+ data:
132
+ NODE_ENV: "production"
133
+ ENABLE_CLUSTER: "true"
134
+ WORKER_COUNT: "4"
135
+ LOG_LEVEL: "info"
136
+ DB_HOST: "mysql-service"
137
+ REDIS_HOST: "redis-service"
138
+ ```
139
+
140
+ ### 3. 部署应用
141
+
142
+ ```yaml
143
+ apiVersion: apps/v1
144
+ kind: Deployment
145
+ metadata:
146
+ name: dp-koa-app
147
+ namespace: dp-koa-framework
148
+ spec:
149
+ replicas: 3
150
+ selector:
151
+ matchLabels:
152
+ app: dp-koa-app
153
+ template:
154
+ metadata:
155
+ labels:
156
+ app: dp-koa-app
157
+ spec:
158
+ containers:
159
+ - name: app
160
+ image: dp-koa-framework:latest
161
+ ports:
162
+ - containerPort: 3000
163
+ envFrom:
164
+ - configMapRef:
165
+ name: app-config
166
+ resources:
167
+ requests:
168
+ memory: "512Mi"
169
+ cpu: "250m"
170
+ limits:
171
+ memory: "1Gi"
172
+ cpu: "500m"
173
+ livenessProbe:
174
+ httpGet:
175
+ path: /live
176
+ port: 3000
177
+ initialDelaySeconds: 30
178
+ periodSeconds: 10
179
+ readinessProbe:
180
+ httpGet:
181
+ path: /ready
182
+ port: 3000
183
+ initialDelaySeconds: 5
184
+ periodSeconds: 5
185
+ ```
186
+
187
+ ### 4. 服务暴露
188
+
189
+ ```yaml
190
+ apiVersion: v1
191
+ kind: Service
192
+ metadata:
193
+ name: dp-koa-service
194
+ namespace: dp-koa-framework
195
+ spec:
196
+ selector:
197
+ app: dp-koa-app
198
+ ports:
199
+ - port: 80
200
+ targetPort: 3000
201
+ type: LoadBalancer
202
+ ```
203
+
204
+ ### 5. 水平Pod自动扩缩容
205
+
206
+ ```yaml
207
+ apiVersion: autoscaling/v2
208
+ kind: HorizontalPodAutoscaler
209
+ metadata:
210
+ name: dp-koa-hpa
211
+ namespace: dp-koa-framework
212
+ spec:
213
+ scaleTargetRef:
214
+ apiVersion: apps/v1
215
+ kind: Deployment
216
+ name: dp-koa-app
217
+ minReplicas: 3
218
+ maxReplicas: 10
219
+ metrics:
220
+ - type: Resource
221
+ resource:
222
+ name: cpu
223
+ target:
224
+ type: Utilization
225
+ averageUtilization: 70
226
+ - type: Resource
227
+ resource:
228
+ name: memory
229
+ target:
230
+ type: Utilization
231
+ averageUtilization: 80
232
+ ```
233
+
234
+ ## 📊 监控配置
235
+
236
+ ### 1. Prometheus配置
237
+
238
+ ```yaml
239
+ # prometheus.yml
240
+ global:
241
+ scrape_interval: 15s
242
+
243
+ scrape_configs:
244
+ - job_name: 'dp-koa-framework'
245
+ static_configs:
246
+ - targets: ['app:3000']
247
+ metrics_path: '/metrics'
248
+ scrape_interval: 5s
249
+ ```
250
+
251
+ ### 2. Grafana仪表板
252
+
253
+ ```json
254
+ {
255
+ "dashboard": {
256
+ "title": "DP-Koa Framework监控",
257
+ "panels": [
258
+ {
259
+ "title": "HTTP请求率",
260
+ "type": "graph",
261
+ "targets": [
262
+ {
263
+ "expr": "rate(http_requests_total[5m])",
264
+ "legendFormat": "{{method}} {{route}}"
265
+ }
266
+ ]
267
+ },
268
+ {
269
+ "title": "内存使用",
270
+ "type": "graph",
271
+ "targets": [
272
+ {
273
+ "expr": "memory_usage_bytes{type=\"heap_used\"}",
274
+ "legendFormat": "堆内存使用"
275
+ }
276
+ ]
277
+ },
278
+ {
279
+ "title": "PM2进程状态",
280
+ "type": "stat",
281
+ "targets": [
282
+ {
283
+ "expr": "up{job=\"dp-koa-framework\"}",
284
+ "legendFormat": "服务状态"
285
+ }
286
+ ]
287
+ }
288
+ ]
289
+ }
290
+ }
291
+ ```
292
+
293
+ ### 3. 告警规则
294
+
295
+ ```yaml
296
+ # alert-rules.yml
297
+ groups:
298
+ - name: dp-koa-framework
299
+ rules:
300
+ - alert: HighErrorRate
301
+ expr: rate(errors_total[5m]) > 0.1
302
+ for: 2m
303
+ labels:
304
+ severity: warning
305
+ annotations:
306
+ summary: "错误率过高"
307
+ description: "应用错误率超过阈值"
308
+
309
+ - alert: HighMemoryUsage
310
+ expr: memory_usage_bytes{type="heap_used"} > 500000000
311
+ for: 5m
312
+ labels:
313
+ severity: critical
314
+ annotations:
315
+ summary: "内存使用过高"
316
+ description: "应用内存使用超过500MB"
317
+
318
+ - alert: ServiceDown
319
+ expr: up{job="dp-koa-framework"} == 0
320
+ for: 1m
321
+ labels:
322
+ severity: critical
323
+ annotations:
324
+ summary: "服务不可用"
325
+ description: "应用服务完全不可用"
326
+ ```
327
+
328
+ ## 🔒 安全配置
329
+
330
+ ### 1. 网络安全
331
+
332
+ ```yaml
333
+ # NetworkPolicy
334
+ apiVersion: networking.k8s.io/v1
335
+ kind: NetworkPolicy
336
+ metadata:
337
+ name: dp-koa-network-policy
338
+ namespace: dp-koa-framework
339
+ spec:
340
+ podSelector:
341
+ matchLabels:
342
+ app: dp-koa-app
343
+ policyTypes:
344
+ - Ingress
345
+ - Egress
346
+ ingress:
347
+ - from:
348
+ - namespaceSelector:
349
+ matchLabels:
350
+ name: ingress-nginx
351
+ ports:
352
+ - protocol: TCP
353
+ port: 3000
354
+ egress:
355
+ - to:
356
+ - namespaceSelector:
357
+ matchLabels:
358
+ name: mysql
359
+ ports:
360
+ - protocol: TCP
361
+ port: 3306
362
+ - to:
363
+ - namespaceSelector:
364
+ matchLabels:
365
+ name: redis
366
+ ports:
367
+ - protocol: TCP
368
+ port: 6379
369
+ ```
370
+
371
+ ### 2. 安全上下文
372
+
373
+ ```yaml
374
+ securityContext:
375
+ runAsNonRoot: true
376
+ runAsUser: 1001
377
+ runAsGroup: 1001
378
+ fsGroup: 1001
379
+ capabilities:
380
+ drop:
381
+ - ALL
382
+ ```
383
+
384
+ ### 3. 密钥管理
385
+
386
+ ```yaml
387
+ apiVersion: v1
388
+ kind: Secret
389
+ metadata:
390
+ name: app-secrets
391
+ namespace: dp-koa-framework
392
+ type: Opaque
393
+ data:
394
+ DB_PASSWORD: <base64-encoded-password>
395
+ JWT_SECRET: <base64-encoded-secret>
396
+ REDIS_PASSWORD: <base64-encoded-redis-password>
397
+ ```
398
+
399
+ ## ⚡ 性能优化
400
+
401
+ ### 1. 资源限制
402
+
403
+ ```yaml
404
+ resources:
405
+ requests:
406
+ memory: "512Mi"
407
+ cpu: "250m"
408
+ limits:
409
+ memory: "1Gi"
410
+ cpu: "500m"
411
+ ```
412
+
413
+ ### 2. 缓存优化
414
+
415
+ ```yaml
416
+ # Redis配置优化
417
+ redis:
418
+ image: redis:7-alpine
419
+ command: >
420
+ redis-server
421
+ --maxmemory 256mb
422
+ --maxmemory-policy allkeys-lru
423
+ --save 900 1
424
+ --save 300 10
425
+ --save 60 10000
426
+ ```
427
+
428
+ ### 3. 数据库优化
429
+
430
+ ```yaml
431
+ # MySQL配置优化
432
+ mysql:
433
+ image: mysql:8.0
434
+ command: >
435
+ mysqld
436
+ --innodb-buffer-pool-size=256M
437
+ --innodb-log-file-size=64M
438
+ --max-connections=200
439
+ --query-cache-size=32M
440
+ ```
441
+
442
+ ## 🚨 故障排除
443
+
444
+ ### 1. 常见问题
445
+
446
+ #### 应用启动失败
447
+ ```bash
448
+ # 检查日志
449
+ kubectl logs -f deployment/dp-koa-app
450
+
451
+ # 检查配置
452
+ kubectl describe configmap app-config
453
+
454
+ # 检查资源
455
+ kubectl top pods -l app=dp-koa-app
456
+ ```
457
+
458
+ #### 数据库连接问题
459
+ ```bash
460
+ # 检查数据库状态
461
+ kubectl exec -it mysql-pod -- mysql -u root -p -e "SHOW PROCESSLIST;"
462
+
463
+ # 检查网络连接
464
+ kubectl exec -it app-pod -- nc -zv mysql-service 3306
465
+ ```
466
+
467
+ #### 内存泄漏
468
+ ```bash
469
+ # 检查内存使用
470
+ kubectl exec -it app-pod -- ps aux
471
+
472
+ # 生成堆转储
473
+ kubectl exec -it app-pod -- node --inspect=0.0.0.0:9229
474
+ ```
475
+
476
+ ### 2. 监控指标
477
+
478
+ #### 关键指标
479
+ - **QPS**: 每秒请求数
480
+ - **响应时间**: 平均响应时间
481
+ - **错误率**: 错误请求比例
482
+ - **内存使用**: 堆内存使用量
483
+ - **CPU使用**: CPU使用率
484
+ - **数据库连接**: 活跃连接数
485
+
486
+ #### 告警阈值
487
+ - **错误率**: > 1%
488
+ - **响应时间**: > 2秒
489
+ - **内存使用**: > 80%
490
+ - **CPU使用**: > 80%
491
+ - **数据库连接**: > 80%
492
+
493
+ ### 3. 应急响应
494
+
495
+ #### 服务降级
496
+ ```bash
497
+ # 减少副本数
498
+ kubectl scale deployment dp-koa-app --replicas=1
499
+
500
+ # 启用维护模式
501
+ kubectl patch deployment dp-koa-app -p '{"spec":{"template":{"spec":{"containers":[{"name":"app","env":[{"name":"MAINTENANCE_MODE","value":"true"}]}]}}}}'
502
+ ```
503
+
504
+ #### 回滚部署
505
+ ```bash
506
+ # 查看部署历史
507
+ kubectl rollout history deployment/dp-koa-app
508
+
509
+ # 回滚到上一版本
510
+ kubectl rollout undo deployment/dp-koa-app
511
+
512
+ # 回滚到指定版本
513
+ kubectl rollout undo deployment/dp-koa-app --to-revision=2
514
+ ```
515
+
516
+ ## 📈 扩展性
517
+
518
+ ### 1. 水平扩展
519
+ - 增加Pod副本数
520
+ - 配置HPA自动扩缩容
521
+ - 使用多可用区部署
522
+ - PM2集群模式
523
+
524
+ ### 2. 垂直扩展
525
+ - 增加CPU和内存限制
526
+ - 优化应用代码性能
527
+ - 使用更强大的硬件
528
+
529
+ ### 3. 功能扩展
530
+ - 添加新的监控指标
531
+ - 集成更多外部服务
532
+ - 实现微服务架构
533
+
534
+ ## 📚 最佳实践
535
+
536
+ 1. **资源管理**: 合理设置资源请求和限制
537
+ 2. **监控告警**: 建立完善的监控和告警体系
538
+ 3. **安全防护**: 实施多层安全防护策略
539
+ 4. **备份恢复**: 定期备份数据和配置
540
+ 5. **文档维护**: 保持部署文档的更新
541
+ 6. **测试验证**: 在生产部署前进行充分测试
542
+ 7. **版本管理**: 使用版本标签管理镜像
543
+ 8. **日志管理**: 集中化日志收集和分析
544
+
545
+ 通过遵循本指南,您可以成功部署和维护一个企业级的dp-koa-framework应用,确保其高可用性、高性能和高安全性。
546
+
547
+