@umacloud/knowledge 1.0.1

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 (418) hide show
  1. package/00-governance/governance-capabilities.md +557 -0
  2. package/00-governance/knowledge-map.md +39 -0
  3. package/00-governance/maintenance-policy.md +76 -0
  4. package/00-governance/review-checklist.md +81 -0
  5. package/README.md +13 -0
  6. package/ai/01-standards/agent-development-complete.md +691 -0
  7. package/ai/01-standards/llm-application-complete.md +488 -0
  8. package/ai/01-standards/mlops-complete.md +798 -0
  9. package/ai/01-standards/prompt-engineering-complete.md +646 -0
  10. package/ai/01-standards/rag-architecture-complete.md +649 -0
  11. package/ai/02-playbooks/llm-evaluation-playbook.md +847 -0
  12. package/ai/03-checklists/ai-project-checklist.md +215 -0
  13. package/ai/04-antipatterns/ai-antipatterns.md +661 -0
  14. package/ai/05-cases/case-rag-production.md +147 -0
  15. package/ai/06-glossary/ai-glossary.md +162 -0
  16. package/ai/agent-evaluation-benchmark.md +53 -0
  17. package/ai/ai-agent-memory-context-management.md +41 -0
  18. package/ai/ai-cost-capacity-optimization-playbook.md +42 -0
  19. package/ai/ai-data-security-and-compliance-playbook.md +37 -0
  20. package/ai/ai-domain-index-and-checklist.md +40 -0
  21. package/ai/ai-governance-maturity-model.md +50 -0
  22. package/ai/ai-model-selection-and-routing-strategy.md +47 -0
  23. package/ai/ai-observability-and-oncall-runbook.md +52 -0
  24. package/ai/ai-rag-engineering-playbook.md +42 -0
  25. package/ai/ai-red-team-and-safety-evaluation.md +42 -0
  26. package/ai/ai-release-readiness-and-rollback-gate.md +42 -0
  27. package/ai/llm-agent-engineering-deep-dive.md +57 -0
  28. package/ai/prompt-and-tool-guardrails.md +52 -0
  29. package/api/01-standards/enterprise-api-standards.md +198 -0
  30. package/api/01-standards/rest-api-design-guide.md +63 -0
  31. package/api/02-playbooks/api-pagination-playbook.md +93 -0
  32. package/api/02-playbooks/graphql-production-playbook.md +176 -0
  33. package/api/03-checklists/api-review-checklist.md +55 -0
  34. package/api/04-antipatterns/api-antipatterns.md +112 -0
  35. package/architecture/01-standards/api-gateway-patterns.md +496 -0
  36. package/architecture/01-standards/cloud-native-patterns.md +644 -0
  37. package/architecture/01-standards/distributed-systems-patterns.md +591 -0
  38. package/architecture/01-standards/event-driven-architecture.md +595 -0
  39. package/architecture/01-standards/microservices-patterns-complete.md +968 -0
  40. package/architecture/01-standards/microservices-patterns.md +495 -0
  41. package/architecture/01-standards/system-design-interview.md +664 -0
  42. package/architecture/02-playbooks/microservices-patterns-playbook.md +137 -0
  43. package/architecture/02-playbooks/migration-playbook.md +780 -0
  44. package/architecture/02-playbooks/system-design-playbook.md +779 -0
  45. package/architecture/03-checklists/architecture-decision-checklist.md +297 -0
  46. package/architecture/04-antipatterns/architecture-antipatterns.md +417 -0
  47. package/architecture/05-cases/case-netflix-microservices.md +413 -0
  48. package/architecture/06-glossary/architecture-glossary.md +164 -0
  49. package/architecture/adr-template-and-examples.md +38 -0
  50. package/architecture/api-gateway-deep-dive.md +1291 -0
  51. package/architecture/configuration-management.md +1162 -0
  52. package/architecture/distributed-transactions.md +1220 -0
  53. package/architecture/microservices-complete.md +735 -0
  54. package/architecture/resilience-and-disaster-patterns.md +37 -0
  55. package/architecture/service-governance.md +1198 -0
  56. package/architecture/system-architecture-deep-dive.md +37 -0
  57. package/backend/01-standards/analytics-and-growth.md +65 -0
  58. package/backend/01-standards/api-and-error-conventions.md +120 -0
  59. package/backend/01-standards/application-layering-and-packaging.md +160 -0
  60. package/backend/01-standards/auth-implementation.md +104 -0
  61. package/backend/01-standards/backend-framework-idioms.md +74 -0
  62. package/backend/01-standards/background-jobs-and-async.md +66 -0
  63. package/backend/01-standards/caching-strategies-complete.md +390 -0
  64. package/backend/01-standards/config-and-observability.md +77 -0
  65. package/backend/01-standards/data-modeling-and-persistence.md +94 -0
  66. package/backend/01-standards/django-complete.md +1765 -0
  67. package/backend/01-standards/email-and-notifications.md +64 -0
  68. package/backend/01-standards/fastapi-complete.md +925 -0
  69. package/backend/01-standards/file-upload-and-storage.md +66 -0
  70. package/backend/01-standards/graphql-api-complete.md +416 -0
  71. package/backend/01-standards/llm-application-standard.md +78 -0
  72. package/backend/01-standards/message-queue-patterns.md +379 -0
  73. package/backend/01-standards/microservices-and-distributed.md +78 -0
  74. package/backend/01-standards/nestjs-complete.md +2167 -0
  75. package/backend/01-standards/payment-integration.md +80 -0
  76. package/backend/01-standards/rate-limiting-complete.md +451 -0
  77. package/backend/01-standards/realtime-and-websocket.md +65 -0
  78. package/backend/01-standards/search-and-filtering.md +64 -0
  79. package/backend/01-standards/spring-boot-complete.md +445 -0
  80. package/backend/02-playbooks/api-design-playbook.md +718 -0
  81. package/backend/02-playbooks/email-send-playbook.md +130 -0
  82. package/backend/02-playbooks/file-upload-s3-playbook.md +153 -0
  83. package/backend/02-playbooks/typescript-enterprise-playbook.md +133 -0
  84. package/backend/02-playbooks/websocket-realtime-playbook.md +154 -0
  85. package/backend/03-checklists/api-launch-checklist.md +189 -0
  86. package/backend/04-antipatterns/backend-antipatterns.md +1051 -0
  87. package/blockchain/01-standards/blockchain-basics.md +557 -0
  88. package/blockchain/01-standards/smart-contract-development.md +1315 -0
  89. package/cicd/01-standards/deployment-and-delivery-standard.md +96 -0
  90. package/cicd/01-standards/github-actions-complete.md +473 -0
  91. package/cicd/01-standards/release-and-store-submission.md +75 -0
  92. package/cicd/02-playbooks/cicd-pipeline-playbook.md +144 -0
  93. package/cicd/02-playbooks/release-management-playbook.md +605 -0
  94. package/cicd/03-checklists/pipeline-security-checklist.md +168 -0
  95. package/cicd/04-antipatterns/cicd-antipatterns.md +589 -0
  96. package/cicd/05-cases/case-deployment-automation.md +221 -0
  97. package/cicd/05-cases/case-gitops-transformation.md +212 -0
  98. package/cicd/06-glossary/cicd-glossary.md +114 -0
  99. package/cicd/cicd-blueprint-deep-dive.md +38 -0
  100. package/cicd/release-readiness-gate.md +37 -0
  101. package/cloud-native/01-standards/container-security.md +741 -0
  102. package/cloud-native/01-standards/kubernetes-complete.md +812 -0
  103. package/cloud-native/02-playbooks/api-gateway-playbook.md +155 -0
  104. package/cloud-native/02-playbooks/gitops-with-argocd.md +760 -0
  105. package/cloud-native/02-playbooks/k8s-troubleshooting-playbook.md +1942 -0
  106. package/cloud-native/02-playbooks/message-queue-playbook.md +129 -0
  107. package/cloud-native/02-playbooks/multicloud-governance.md +726 -0
  108. package/cloud-native/02-playbooks/serverless-patterns.md +788 -0
  109. package/cloud-native/02-playbooks/service-mesh-playbook.md +612 -0
  110. package/cloud-native/02-playbooks/terraform-iac-playbook.md +143 -0
  111. package/cloud-native/03-checklists/container-security-checklist.md +431 -0
  112. package/cloud-native/03-checklists/k8s-production-readiness-checklist.md +460 -0
  113. package/cloud-native/04-antipatterns/container-antipatterns.md +660 -0
  114. package/cloud-native/04-antipatterns/k8s-antipatterns.md +743 -0
  115. package/cloud-native/05-cases/case-k8s-migration.md +478 -0
  116. package/cloud-native/05-cases/case-k8s-scaling.md +642 -0
  117. package/cloud-native/05-cases/case-k8s-security-incident.md +397 -0
  118. package/cloud-native/06-glossary/cloud-native-glossary.md +337 -0
  119. package/cross-platform/01-standards/cross-platform-frameworks.md +83 -0
  120. package/cross-platform/01-standards/platform-selection-and-architecture.md +77 -0
  121. package/data/01-standards/elasticsearch-complete.md +2098 -0
  122. package/data/01-standards/postgresql-complete.md +1613 -0
  123. package/data/01-standards/redis-complete.md +1527 -0
  124. package/data/02-playbooks/database-optimization-playbook.md +403 -0
  125. package/data/02-playbooks/elasticsearch-production-playbook.md +132 -0
  126. package/data/03-checklists/database-launch-checklist.md +187 -0
  127. package/data/04-antipatterns/database-antipatterns.md +873 -0
  128. package/data/05-cases/case-database-migration.md +310 -0
  129. package/data/06-glossary/database-glossary.md +440 -0
  130. package/data/data-governance-and-modeling-deep-dive.md +39 -0
  131. package/data-engineering/01-standards/airflow-complete.md +523 -0
  132. package/data-engineering/01-standards/kafka-complete.md +1521 -0
  133. package/data-engineering/02-playbooks/spark-etl-playbook.md +496 -0
  134. package/data-engineering/03-checklists/pipeline-launch-checklist.md +194 -0
  135. package/data-engineering/04-antipatterns/data-pipeline-antipatterns.md +684 -0
  136. package/data-engineering/05-cases/case-real-time-pipeline.md +355 -0
  137. package/data-engineering/06-glossary/data-engineering-glossary.md +429 -0
  138. package/database/01-standards/database-schema-standards.md +147 -0
  139. package/database/02-playbooks/postgresql-optimization-quick.md +52 -0
  140. package/database/02-playbooks/postgresql-performance-optimization.md +58 -0
  141. package/database/02-playbooks/postgresql-production-playbook.md +146 -0
  142. package/database/02-playbooks/redis-caching-playbook.md +117 -0
  143. package/database/03-checklists/database-review-checklist.md +50 -0
  144. package/database/04-antipatterns/database-antipatterns.md +112 -0
  145. package/design/01-standards/ui-design-system-complete.md +423 -0
  146. package/design/02-playbooks/design-handoff-playbook.md +254 -0
  147. package/design/02-playbooks/design-review-playbook.md +388 -0
  148. package/design/03-checklists/design-review-checklist.md +246 -0
  149. package/design/04-antipatterns/design-antipatterns.md +378 -0
  150. package/design/05-cases/case-design-system-adoption.md +328 -0
  151. package/design/06-glossary/design-glossary.md +329 -0
  152. package/design/ui-full-lifecycle-cross-platform-playbook.md +571 -0
  153. package/design/ux-system-deep-dive.md +38 -0
  154. package/design-systems/00-craft-rules.md +71 -0
  155. package/design-systems/aesthetic-families.md +43 -0
  156. package/design-systems/anti-ai-slop.md +162 -0
  157. package/design-systems/bold-geometric.md +120 -0
  158. package/design-systems/brutalist-bold.md +103 -0
  159. package/design-systems/editorial-clean.md +109 -0
  160. package/design-systems/glass-aurora.md +108 -0
  161. package/design-systems/modern-minimal.md +145 -0
  162. package/design-systems/premium-luxury.md +106 -0
  163. package/design-systems/product-type-design-map.md +48 -0
  164. package/design-systems/soft-warm.md +123 -0
  165. package/design-systems/tech-utility.md +113 -0
  166. package/desktop/01-standards/desktop-app-standard.md +72 -0
  167. package/desktop/01-standards/desktop-design.md +71 -0
  168. package/development/00-governance/document-template.md +41 -0
  169. package/development/01-standards/api-versioning-strategies.md +432 -0
  170. package/development/01-standards/authentication-patterns-complete.md +479 -0
  171. package/development/01-standards/css-architecture-complete.md +550 -0
  172. package/development/01-standards/database-migration-strategies.md +484 -0
  173. package/development/01-standards/elasticsearch-complete.md +347 -0
  174. package/development/01-standards/git-complete.md +371 -0
  175. package/development/01-standards/golang-complete.md +1565 -0
  176. package/development/01-standards/graphql-complete.md +298 -0
  177. package/development/01-standards/javascript-bundlers-complete.md +469 -0
  178. package/development/01-standards/javascript-typescript-complete.md +528 -0
  179. package/development/01-standards/jest-complete.md +275 -0
  180. package/development/01-standards/linux-complete.md +234 -0
  181. package/development/01-standards/logging-observability-complete.md +526 -0
  182. package/development/01-standards/microservices-communication.md +502 -0
  183. package/development/01-standards/mongodb-complete.md +406 -0
  184. package/development/01-standards/oauth2-complete.md +285 -0
  185. package/development/01-standards/performance-optimization-complete.md +289 -0
  186. package/development/01-standards/playwright-complete.md +247 -0
  187. package/development/01-standards/postgresql-complete.md +456 -0
  188. package/development/01-standards/pytest-complete.md +340 -0
  189. package/development/01-standards/python-async-programming.md +902 -0
  190. package/development/01-standards/python-complete.md +956 -0
  191. package/development/01-standards/python-decorators-complete.md +799 -0
  192. package/development/01-standards/python-design-patterns.md +2854 -0
  193. package/development/01-standards/python-packaging-distribution.md +420 -0
  194. package/development/01-standards/python-testing-strategies.md +607 -0
  195. package/development/01-standards/python-web-frameworks-comparison.md +471 -0
  196. package/development/01-standards/redis-complete.md +317 -0
  197. package/development/01-standards/rest-api-complete.md +316 -0
  198. package/development/01-standards/rust-complete.md +578 -0
  199. package/development/01-standards/typescript-advanced-types.md +1513 -0
  200. package/development/01-standards/web-security-complete.md +292 -0
  201. package/development/02-playbooks/api-design-playbook.md +810 -0
  202. package/development/02-playbooks/database-migration-playbook.md +580 -0
  203. package/development/02-playbooks/debugging-playbook.md +692 -0
  204. package/development/02-playbooks/feature-delivery-playbook.md +430 -0
  205. package/development/02-playbooks/incident-hotfix-playbook.md +387 -0
  206. package/development/02-playbooks/performance-optimization-playbook.md +531 -0
  207. package/development/02-playbooks/performance-tuning-playbook.md +652 -0
  208. package/development/02-playbooks/refactor-playbook.md +403 -0
  209. package/development/02-playbooks/release-playbook.md +469 -0
  210. package/development/03-checklists/architecture-review-checklist.md +168 -0
  211. package/development/03-checklists/data-migration-checklist.md +157 -0
  212. package/development/03-checklists/oncall-handover-checklist.md +173 -0
  213. package/development/03-checklists/pr-checklist.md +158 -0
  214. package/development/03-checklists/production-readiness-checklist.md +190 -0
  215. package/development/03-checklists/release-readiness-checklist.md +154 -0
  216. package/development/03-checklists/security-review-checklist.md +182 -0
  217. package/development/04-antipatterns/api-antipatterns.md +657 -0
  218. package/development/04-antipatterns/architecture-antipatterns.md +686 -0
  219. package/development/04-antipatterns/backend-antipatterns.md +648 -0
  220. package/development/04-antipatterns/cicd-antipatterns.md +540 -0
  221. package/development/04-antipatterns/code-smell-antipatterns.md +571 -0
  222. package/development/04-antipatterns/data-antipatterns.md +658 -0
  223. package/development/04-antipatterns/database-antipatterns.md +578 -0
  224. package/development/04-antipatterns/frontend-antipatterns.md +635 -0
  225. package/development/04-antipatterns/reliability-antipatterns.md +700 -0
  226. package/development/04-antipatterns/security-antipatterns.md +747 -0
  227. package/development/05-cases/case-api-version-migration.md +428 -0
  228. package/development/05-cases/case-authorization-hardening.md +383 -0
  229. package/development/05-cases/case-bluegreen-rollback.md +466 -0
  230. package/development/05-cases/case-cache-snowball-protection.md +485 -0
  231. package/development/05-cases/case-ci-cd-pipeline.md +544 -0
  232. package/development/05-cases/case-database-scaling.md +500 -0
  233. package/development/05-cases/case-db-hotspot-optimization.md +487 -0
  234. package/development/05-cases/case-incident-mttr-reduction.md +563 -0
  235. package/development/05-cases/case-microservice-migration.md +375 -0
  236. package/development/05-cases/case-performance-optimization.md +406 -0
  237. package/development/05-cases/case-security-incident-response.md +345 -0
  238. package/development/06-glossary/full-stack-glossary.md +166 -0
  239. package/development/09-maturity/quarterly-audit-template.md +35 -0
  240. package/development/11-ui-excellence/ui-aesthetic-system.md +41 -0
  241. package/development/11-ui-excellence/ui-engineering-excellence.md +435 -0
  242. package/development/12-scenarios/development-scenarios-guide.md +565 -0
  243. package/development/13-implementation-assets/implementation-toolkit.md +282 -0
  244. package/development/13-implementation-assets/knowledge-gates-execution.md +43 -0
  245. package/development/14-full-lifecycle/software-lifecycle-gates.md +511 -0
  246. package/development/15-lifecycle-templates/project-templates-collection.md +791 -0
  247. package/development/api-contract-and-versioning-guide.md +36 -0
  248. package/development/api-governance-complete.md +43 -0
  249. package/development/backend-engineering-complete.md +43 -0
  250. package/development/code-review-quality-complete.md +43 -0
  251. package/development/concurrency-reliability-complete.md +43 -0
  252. package/development/database-engineering-complete.md +43 -0
  253. package/development/engineering-effectiveness-complete.md +43 -0
  254. package/development/engineering-standards-deep-dive.md +38 -0
  255. package/development/frontend-engineering-complete.md +43 -0
  256. package/development/performance-capacity-complete.md +43 -0
  257. package/development/refactor-migration-complete.md +42 -0
  258. package/development/refactoring-and-techdebt-playbook.md +37 -0
  259. package/development/security-in-development-complete.md +43 -0
  260. package/devops/01-standards/cicd-pipeline-complete.md +262 -0
  261. package/devops/01-standards/docker-complete.md +1490 -0
  262. package/devops/01-standards/github-actions-complete.md +337 -0
  263. package/devops/01-standards/kubernetes-complete.md +638 -0
  264. package/devops/01-standards/terraform-complete.md +2117 -0
  265. package/devops/02-playbooks/docker-compose-playbook.md +233 -0
  266. package/devops/02-playbooks/docker-k8s-production-playbook.md +186 -0
  267. package/devops/02-playbooks/docker-production-playbook.md +952 -0
  268. package/edge-iot/01-standards/edge-iot-complete.md +473 -0
  269. package/experts/architect/api-design.md +178 -0
  270. package/experts/architect/methodology.md +124 -0
  271. package/experts/architect/security.md +75 -0
  272. package/experts/backend-lead/methodology.md +216 -0
  273. package/experts/devops/methodology.md +160 -0
  274. package/experts/frontend-lead/methodology.md +178 -0
  275. package/experts/product-manager/industry/ecommerce.md +43 -0
  276. package/experts/product-manager/industry/saas.md +40 -0
  277. package/experts/product-manager/methodology.md +97 -0
  278. package/experts/qa-lead/methodology.md +123 -0
  279. package/experts/qa-lead/test-strategy.md +128 -0
  280. package/experts/uiux-designer/methodology.md +125 -0
  281. package/frontend/01-standards/accessibility-complete.md +532 -0
  282. package/frontend/01-standards/accessibility-standard.md +74 -0
  283. package/frontend/01-standards/admin-dashboard-and-crud.md +72 -0
  284. package/frontend/01-standards/design-tokens-complete.md +444 -0
  285. package/frontend/01-standards/forms-and-validation.md +77 -0
  286. package/frontend/01-standards/frontend-architecture-and-layering.md +119 -0
  287. package/frontend/01-standards/i18n-and-localization.md +65 -0
  288. package/frontend/01-standards/nextjs-complete.md +451 -0
  289. package/frontend/01-standards/react-complete.md +713 -0
  290. package/frontend/01-standards/react-hooks-complete-guide.md +1100 -0
  291. package/frontend/01-standards/react-hooks-complete.md +1171 -0
  292. package/frontend/01-standards/seo-and-web-vitals.md +77 -0
  293. package/frontend/01-standards/state-management-complete.md +444 -0
  294. package/frontend/01-standards/vue-complete.md +499 -0
  295. package/frontend/01-standards/vue3-complete.md +2002 -0
  296. package/frontend/01-standards/web-framework-best-practices.md +64 -0
  297. package/frontend/01-standards/web-performance-complete.md +495 -0
  298. package/frontend/02-playbooks/accessibility-a11y-playbook.md +161 -0
  299. package/frontend/02-playbooks/frontend-performance-playbook.md +707 -0
  300. package/frontend/02-playbooks/i18n-internationalization-playbook.md +120 -0
  301. package/frontend/02-playbooks/performance-optimization-playbook.md +163 -0
  302. package/frontend/02-playbooks/react-nextjs-production-playbook.md +167 -0
  303. package/frontend/02-playbooks/react-state-management-playbook.md +173 -0
  304. package/frontend/03-checklists/component-quality-checklist.md +166 -0
  305. package/frontend/03-checklists/frontend-launch-checklist.md +299 -0
  306. package/frontend/04-antipatterns/frontend-antipatterns.md +886 -0
  307. package/frontend/05-cases/case-performance-optimization.md +274 -0
  308. package/harmony/01-standards/harmonyos-arkts-standard.md +75 -0
  309. package/harmony/01-standards/harmonyos-design.md +65 -0
  310. package/high-quality-engineering-playbook.md +54 -0
  311. package/incident/01-standards/incident-response-complete.md +303 -0
  312. package/incident/02-playbooks/chaos-engineering-playbook.md +883 -0
  313. package/incident/02-playbooks/postmortem-playbook.md +398 -0
  314. package/incident/03-checklists/incident-readiness-checklist.md +181 -0
  315. package/incident/04-antipatterns/incident-antipatterns.md +490 -0
  316. package/incident/05-cases/case-cascade-failure.md +176 -0
  317. package/incident/06-glossary/incident-glossary.md +114 -0
  318. package/incident/postmortem-and-response-deep-dive.md +39 -0
  319. package/industries/ecommerce/ecommerce-complete.md +631 -0
  320. package/industries/education/education-complete.md +555 -0
  321. package/industries/fintech/fintech-complete.md +501 -0
  322. package/industries/gaming/gaming-complete.md +587 -0
  323. package/industries/healthcare/healthcare-complete.md +452 -0
  324. package/low-code/01-standards/low-code-complete.md +944 -0
  325. package/miniprogram/01-standards/ai-common-mistakes.md +61 -0
  326. package/miniprogram/01-standards/miniprogram-custom-navbar-capsule.md +77 -0
  327. package/miniprogram/01-standards/miniprogram-design.md +61 -0
  328. package/miniprogram/01-standards/miniprogram-standard.md +81 -0
  329. package/mobile/01-standards/android-material-design.md +70 -0
  330. package/mobile/01-standards/flutter-complete.md +384 -0
  331. package/mobile/01-standards/ios-design-hig.md +78 -0
  332. package/mobile/01-standards/mobile-app-standard.md +85 -0
  333. package/mobile/01-standards/react-native-complete.md +352 -0
  334. package/mobile/02-playbooks/mobile-cross-platform-playbook.md +175 -0
  335. package/mobile/02-playbooks/mobile-performance.md +473 -0
  336. package/mobile/03-checklists/mobile-release-checklist.md +234 -0
  337. package/mobile/04-antipatterns/mobile-antipatterns.md +798 -0
  338. package/mobile/05-cases/case-app-performance.md +500 -0
  339. package/mobile/05-cases/case-app-startup-optimization.md +218 -0
  340. package/mobile/06-glossary/mobile-glossary.md +484 -0
  341. package/observability/01-standards/observability-standards.md +103 -0
  342. package/observability/02-playbooks/prometheus-grafana-playbook.md +135 -0
  343. package/observability/02-playbooks/structured-logging-playbook.md +73 -0
  344. package/observability/03-checklists/observability-checklist.md +54 -0
  345. package/observability/04-antipatterns/observability-antipatterns.md +106 -0
  346. package/operations/01-standards/prometheus-monitoring-complete.md +1578 -0
  347. package/operations/02-playbooks/capacity-planning-playbook.md +620 -0
  348. package/operations/03-checklists/production-launch-checklist.md +365 -0
  349. package/operations/04-antipatterns/operations-antipatterns.md +664 -0
  350. package/operations/05-cases/case-sre-practices.md +581 -0
  351. package/operations/06-glossary/operations-glossary.md +120 -0
  352. package/operations/aiops-anomaly-detection.md +758 -0
  353. package/operations/capacity-planning.md +1061 -0
  354. package/operations/chaos-engineering.md +659 -0
  355. package/operations/incident-command-system.md +38 -0
  356. package/operations/observability-complete.md +442 -0
  357. package/operations/slo-sli-playbook.md +517 -0
  358. package/operations/sre-operations-deep-dive.md +39 -0
  359. package/package.json +8 -0
  360. package/performance/01-standards/performance-and-scalability.md +80 -0
  361. package/performance/01-standards/performance-standards.md +156 -0
  362. package/performance/02-playbooks/query-optimization-playbook.md +103 -0
  363. package/performance/03-checklists/performance-checklist.md +56 -0
  364. package/performance/04-antipatterns/performance-antipatterns.md +146 -0
  365. package/product/01-standards/product-management-complete.md +285 -0
  366. package/product/02-playbooks/feature-launch-playbook.md +207 -0
  367. package/product/02-playbooks/user-research-playbook.md +532 -0
  368. package/product/03-checklists/feature-launch-checklist.md +275 -0
  369. package/product/04-antipatterns/product-antipatterns.md +355 -0
  370. package/product/05-cases/case-mvp-to-scale.md +384 -0
  371. package/product/06-glossary/product-glossary.md +462 -0
  372. package/product/feature-prioritization-framework.md +40 -0
  373. package/product/kpi-and-metric-tree.md +37 -0
  374. package/product/product-discovery-and-prd-deep-dive.md +41 -0
  375. package/quantum/01-standards/quantum-complete.md +1186 -0
  376. package/security/01-standards/api-security-complete.md +511 -0
  377. package/security/01-standards/container-runtime-security.md +574 -0
  378. package/security/01-standards/data-protection-gdpr.md +543 -0
  379. package/security/01-standards/owasp-top10-complete.md +1890 -0
  380. package/security/01-standards/secure-coding-baseline.md +90 -0
  381. package/security/01-standards/supply-chain-security.md +441 -0
  382. package/security/01-standards/web-security-checklist.md +108 -0
  383. package/security/01-standards/zero-trust-architecture.md +521 -0
  384. package/security/02-playbooks/auth-sso-playbook.md +166 -0
  385. package/security/02-playbooks/incident-response-security-playbook.md +588 -0
  386. package/security/02-playbooks/owasp-api-security-playbook.md +129 -0
  387. package/security/02-playbooks/payment-integration-playbook.md +119 -0
  388. package/security/02-playbooks/penetration-testing-playbook.md +517 -0
  389. package/security/03-checklists/security-audit-checklist.md +356 -0
  390. package/security/04-antipatterns/security-coding-antipatterns.md +580 -0
  391. package/security/05-cases/case-log4shell-incident.md +537 -0
  392. package/security/05-cases/case-major-breaches.md +468 -0
  393. package/security/06-glossary/security-glossary.md +212 -0
  394. package/security/compliance-automation.md +993 -0
  395. package/security/container-security.md +680 -0
  396. package/security/devsecops-complete.md +426 -0
  397. package/security/sast-dast-sca.md +775 -0
  398. package/security/secrets-management.md +594 -0
  399. package/security/security-architecture-deep-dive.md +37 -0
  400. package/security/threat-modeling-stride-playbook.md +40 -0
  401. package/seed-templates/auth-system.md +59 -0
  402. package/seed-templates/blog-content.md +94 -0
  403. package/seed-templates/dashboard.md +89 -0
  404. package/seed-templates/docs-site.md +73 -0
  405. package/seed-templates/e-commerce.md +50 -0
  406. package/seed-templates/saas-landing.md +92 -0
  407. package/seed-templates/settings-page.md +51 -0
  408. package/testing/01-standards/test-strategy-and-layering.md +83 -0
  409. package/testing/01-standards/testing-strategy-complete.md +422 -0
  410. package/testing/01-standards/unit-testing-best-practices.md +118 -0
  411. package/testing/02-playbooks/e2e-testing-playbook.md +988 -0
  412. package/testing/02-playbooks/testing-strategy-playbook.md +126 -0
  413. package/testing/03-checklists/test-strategy-checklist.md +208 -0
  414. package/testing/04-antipatterns/testing-antipatterns.md +718 -0
  415. package/testing/05-cases/case-testing-transformation.md +300 -0
  416. package/testing/06-glossary/testing-glossary.md +110 -0
  417. package/testing/risk-based-test-matrix.md +36 -0
  418. package/testing/testing-strategy-deep-dive.md +37 -0
@@ -0,0 +1,1198 @@
1
+ ---
2
+ id: service-governance
3
+ title: 服务治理完全指南
4
+ domain: architecture
5
+ category: service-governance.md
6
+ difficulty: intermediate
7
+ tags: [architecture, governance, service, 服务注册与发现, 核心能力, 概述, 熔断与降级, 负载均衡]
8
+ quality_score: 70
9
+ last_updated: 2026-06-15
10
+ ---
11
+ # 服务治理完全指南
12
+
13
+ ## 概述
14
+
15
+ 服务治理是一套用于管理微服务架构中服务间交互的方法论和技术体系。它涵盖服务注册发现、负载均衡、流量控制、熔断降级、服务容错、服务监控等各个方面,确保微服务系统的稳定性、可靠性和高性能。
16
+
17
+ ## 核心能力
18
+
19
+ ### 1. 服务生命周期管理
20
+ - 服务注册与发现
21
+ - 服务上下线管理
22
+ - 服务健康检查
23
+
24
+ ### 2. 流量治理
25
+ - 负载均衡
26
+ - 流量分配
27
+ - 灰度发布
28
+ - 流量镜像
29
+
30
+ ### 3. 可靠性保障
31
+ - 熔断降级
32
+ - 限流控制
33
+ - 超时控制
34
+ - 重试机制
35
+
36
+ ### 4. 安全治理
37
+ - 认证授权
38
+ - 访问控制
39
+ - 通信加密
40
+ - 审计日志
41
+
42
+ ### 5. 可观测性
43
+ - 指标监控
44
+ - 日志采集
45
+ - 分布式追踪
46
+ - 服务依赖分析
47
+
48
+ ## 服务注册与发现
49
+
50
+ ### 核心概念
51
+ ```
52
+ 服务提供者(Provider): 提供服务的应用
53
+ 服务消费者(Consumer): 调用服务的应用
54
+ 服务注册中心(Registry): 存储服务实例信息
55
+ ```
56
+
57
+ ### 注册中心对比
58
+
59
+ #### Consul
60
+ ```
61
+ 架构:
62
+ - Server节点(Raft协议)
63
+ - Client节点
64
+ - Agent(每个节点运行)
65
+
66
+ 特性:
67
+ - 服务发现
68
+ - 健康检查
69
+ - KV存储
70
+ - 多数据中心
71
+ - DNS/HTTP接口
72
+
73
+ 优点:
74
+ - 功能全面
75
+ - 支持多语言
76
+ - 配置中心集成
77
+
78
+ 缺点:
79
+ - 需要部署Agent
80
+ - 运维复杂度
81
+
82
+ 适用场景:
83
+ - 异构技术栈
84
+ - 多数据中心
85
+ - 配置管理需求
86
+ ```
87
+
88
+ #### Nacos
89
+ ```
90
+ 架构:
91
+ - Nacos Server(集群)
92
+ - Nacos Client
93
+ - 数据存储(MySQL/嵌入式)
94
+
95
+ 特性:
96
+ - 服务发现
97
+ - 配置管理
98
+ - 动态DNS
99
+ - 服务元数据管理
100
+
101
+ 优点:
102
+ - 阿里开源,生产验证
103
+ - Spring Cloud Alibaba集成
104
+ - 中文文档完善
105
+ - 简单易用
106
+
107
+ 缺点:
108
+ - 社区生态相对较小
109
+ - 性能不如Consul
110
+
111
+ 适用场景:
112
+ - Spring Cloud生态
113
+ - 国内环境
114
+ - 中小型系统
115
+ ```
116
+
117
+ #### Eureka
118
+ ```
119
+ 架构:
120
+ - Eureka Server(集群)
121
+ - Eureka Client
122
+ - AP架构
123
+
124
+ 特性:
125
+ - 服务注册发现
126
+ - 自我保护模式
127
+ - 区域感知
128
+
129
+ 优点:
130
+ - Spring Cloud原生支持
131
+ - 高可用设计
132
+ - 简单易用
133
+
134
+ 缺点:
135
+ - 已进入维护模式
136
+ - 功能单一
137
+ - 性能一般
138
+
139
+ 适用场景:
140
+ - Spring Cloud Netflix生态
141
+ - 已有Eureka系统
142
+ ```
143
+
144
+ #### ZooKeeper
145
+ ```
146
+ 架构:
147
+ - Leader节点
148
+ - Follower节点
149
+ - Observer节点
150
+ - CP架构
151
+
152
+ 特性:
153
+ - 服务注册
154
+ - 配置管理
155
+ - 分布式锁
156
+ - Leader选举
157
+
158
+ 优点:
159
+ - 成熟稳定
160
+ - 强一致性
161
+ - 功能丰富
162
+
163
+ 缺点:
164
+ - 重,复杂
165
+ - 临时节点会导致注册频繁
166
+ - 运维成本高
167
+
168
+ 适用场景:
169
+ - Hadoop生态
170
+ - 已有ZK集群
171
+ ```
172
+
173
+ ### 实现示例(Spring Cloud + Nacos)
174
+
175
+ #### 服务注册
176
+ ```java
177
+ // application.yml
178
+ spring:
179
+ application:
180
+ name: order-service
181
+ cloud:
182
+ nacos:
183
+ discovery:
184
+ server-addr: localhost:8848
185
+ namespace: dev
186
+ group: DEFAULT_GROUP
187
+ metadata:
188
+ version: 1.0.0
189
+ region: cn-east-1
190
+
191
+ // 启动类
192
+ @SpringBootApplication
193
+ @EnableDiscoveryClient
194
+ public class OrderServiceApplication {
195
+ public static void main(String[] args) {
196
+ SpringApplication.run(OrderServiceApplication.class, args);
197
+ }
198
+ }
199
+ ```
200
+
201
+ #### 服务发现
202
+ ```java
203
+ @Service
204
+ public class OrderService {
205
+ @Autowired
206
+ private DiscoveryClient discoveryClient;
207
+
208
+ @Autowired
209
+ private RestTemplate restTemplate;
210
+
211
+ public List<ServiceInstance> getInventoryServiceInstances() {
212
+ return discoveryClient.getInstances("inventory-service");
213
+ }
214
+
215
+ public String callInventoryService() {
216
+ // 使用负载均衡
217
+ ServiceInstance instance = loadBalancer.choose("inventory-service");
218
+ String url = instance.getUri().toString() + "/inventory/check";
219
+
220
+ return restTemplate.getForObject(url, String.class);
221
+ }
222
+ }
223
+
224
+ // 使用Feign(自动集成负载均衡)
225
+ @FeignClient(name = "inventory-service")
226
+ public interface InventoryClient {
227
+ @GetMapping("/inventory/check")
228
+ String checkInventory();
229
+ }
230
+ ```
231
+
232
+ ## 负载均衡
233
+
234
+ ### 负载均衡策略
235
+
236
+ #### 轮询(Round Robin)
237
+ ```
238
+ 特点:
239
+ - 依次分发请求
240
+ - 简单公平
241
+
242
+ 适用:
243
+ - 服务器性能相近
244
+ - 无状态服务
245
+ ```
246
+
247
+ #### 加权轮询(Weighted Round Robin)
248
+ ```
249
+ 特点:
250
+ - 根据权重分配流量
251
+ - 权重高的实例获得更多请求
252
+
253
+ 适用:
254
+ - 服务器性能不均
255
+ - 灰度发布
256
+ ```
257
+
258
+ #### 最少连接(Least Connections)
259
+ ```
260
+ 特点:
261
+ - 选择当前连接数最少的服务器
262
+ - 动态调整
263
+
264
+ 适用:
265
+ - 长连接场景
266
+ - 请求处理时间差异大
267
+ ```
268
+
269
+ #### 一致性哈希(Consistent Hash)
270
+ ```
271
+ 特点:
272
+ - 根据请求特征(如用户ID)哈希
273
+ - 同一特征请求路由到同一服务器
274
+
275
+ 适用:
276
+ - 有状态服务
277
+ - 缓存场景
278
+ ```
279
+
280
+ #### 随机(Random)
281
+ ```
282
+ 特点:
283
+ - 随机选择服务器
284
+ - 简单
285
+
286
+ 适用:
287
+ - 无特殊要求场景
288
+ ```
289
+
290
+ ### 客户端负载均衡
291
+
292
+ #### Spring Cloud LoadBalancer
293
+ ```java
294
+ // 配置
295
+ @Configuration
296
+ public class LoadBalancerConfig {
297
+ @Bean
298
+ ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(
299
+ Environment environment,
300
+ LoadBalancerClientFactory factory) {
301
+ String serviceId = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
302
+ return new RandomLoadBalancer(
303
+ factory.getLazyProvider(serviceId, ServiceInstanceListSupplier.class),
304
+ serviceId
305
+ );
306
+ }
307
+ }
308
+
309
+ // 自定义负载均衡策略
310
+ public class CustomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
311
+ @Override
312
+ public Mono<Response<ServiceInstance>> choose(Request request) {
313
+ ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
314
+ .getIfAvailable(NoopServiceInstanceListSupplier::new);
315
+
316
+ return supplier.get()
317
+ .next()
318
+ .map(instances -> {
319
+ // 自定义选择逻辑
320
+ ServiceInstance instance = selectInstance(instances);
321
+ return new DefaultResponse(instance);
322
+ });
323
+ }
324
+
325
+ private ServiceInstance selectInstance(List<ServiceInstance> instances) {
326
+ // 根据实例权重、响应时间等选择
327
+ // ...
328
+ }
329
+ }
330
+ ```
331
+
332
+ ### 服务端负载均衡
333
+
334
+ #### Nginx
335
+ ```nginx
336
+ upstream backend {
337
+ # 加权轮询
338
+ server backend1.example.com weight=5;
339
+ server backend2.example.com weight=3;
340
+ server backend3.example.com backup;
341
+
342
+ # 健康检查
343
+ server backend4.example.com max_fails=3 fail_timeout=30s;
344
+
345
+ # 一致性哈希
346
+ hash $request_uri consistent;
347
+ }
348
+
349
+ server {
350
+ location / {
351
+ proxy_pass http://backend;
352
+ proxy_set_header Host $host;
353
+ proxy_set_header X-Real-IP $remote_addr;
354
+ }
355
+ }
356
+ ```
357
+
358
+ ## 熔断与降级
359
+
360
+ ### 熔断器模式(Circuit Breaker)
361
+
362
+ #### 状态机
363
+ ```
364
+ 关闭状态(Closed):
365
+ - 正常调用
366
+ - 监控失败率
367
+
368
+ 打开状态(Open):
369
+ - 快速失败,不调用下游
370
+ - 等待超时后进入半开
371
+
372
+ 半开状态(Half-Open):
373
+ - 允许少量请求
374
+ - 测试下游是否恢复
375
+ - 成功则关闭,失败则打开
376
+ ```
377
+
378
+ #### 实现示例(Resilience4j)
379
+ ```java
380
+ // 配置
381
+ @Bean
382
+ public CircuitBreakerConfig circuitBreakerConfig() {
383
+ return CircuitBreakerConfig.custom()
384
+ .failureRateThreshold(50) // 失败率阈值50%
385
+ .waitDurationInOpenState(Duration.ofMillis(1000)) // 开启状态等待时间
386
+ .permittedNumberOfCallsInHalfOpenState(2) // 半开状态允许的调用次数
387
+ .slidingWindowSize(10) // 滑动窗口大小
388
+ .slidingWindowType(SlidingWindowType.COUNT_BASED)
389
+ .build();
390
+ }
391
+
392
+ // 使用
393
+ @Service
394
+ public class OrderService {
395
+ private final CircuitBreaker circuitBreaker;
396
+
397
+ public OrderService(CircuitBreakerRegistry registry) {
398
+ this.circuitBreaker = registry.circuitBreaker("inventoryService");
399
+ }
400
+
401
+ public InventoryResponse checkInventory(String productId) {
402
+ return circuitBreaker.executeSupplier(() -> {
403
+ return inventoryClient.checkInventory(productId);
404
+ });
405
+ }
406
+
407
+ // 带降级方法
408
+ public InventoryResponse checkInventoryWithFallback(String productId) {
409
+ return circuitBreaker.executeSupplier(
410
+ () -> inventoryClient.checkInventory(productId),
411
+ () -> fallbackCheckInventory(productId)
412
+ );
413
+ }
414
+
415
+ private InventoryResponse fallbackCheckInventory(String productId) {
416
+ // 降级逻辑: 返回默认值或从缓存读取
417
+ return InventoryResponse.defaultResponse();
418
+ }
419
+ }
420
+ ```
421
+
422
+ #### 配置详解
423
+ ```yaml
424
+ resilience4j:
425
+ circuitbreaker:
426
+ configs:
427
+ default:
428
+ failureRateThreshold: 50
429
+ waitDurationInOpenState: 1000
430
+ slidingWindowSize: 10
431
+ slidingWindowType: COUNT_BASED
432
+ permittedNumberOfCallsInHalfOpenState: 2
433
+ minimumNumberOfCalls: 5
434
+ recordExceptions:
435
+ - java.io.IOException
436
+ - java.net.SocketTimeoutException
437
+ ignoreExceptions:
438
+ - com.example.BusinessException
439
+ instances:
440
+ inventoryService:
441
+ baseConfig: default
442
+ failureRateThreshold: 60
443
+ paymentService:
444
+ baseConfig: default
445
+ waitDurationInOpenState: 5000
446
+ ```
447
+
448
+ ### 降级策略
449
+
450
+ #### 返回默认值
451
+ ```java
452
+ public Product getProduct(String productId) {
453
+ return circuitBreaker.executeSupplier(
454
+ () -> productClient.getProduct(productId),
455
+ () -> Product.defaultProduct() // 返回默认商品
456
+ );
457
+ }
458
+ ```
459
+
460
+ #### 返回缓存数据
461
+ ```java
462
+ public Product getProduct(String productId) {
463
+ return circuitBreaker.executeSupplier(
464
+ () -> {
465
+ Product product = productClient.getProduct(productId);
466
+ cache.put(productId, product);
467
+ return product;
468
+ },
469
+ () -> cache.get(productId) // 降级返回缓存
470
+ );
471
+ }
472
+ ```
473
+
474
+ #### 返回空数据
475
+ ```java
476
+ public List<Order> getUserOrders(String userId) {
477
+ return circuitBreaker.executeSupplier(
478
+ () -> orderClient.getUserOrders(userId),
479
+ () -> Collections.emptyList() // 降级返回空列表
480
+ );
481
+ }
482
+ ```
483
+
484
+ #### 页面降级
485
+ ```java
486
+ @Controller
487
+ public class PageController {
488
+ @GetMapping("/product/{id}")
489
+ public String getProductPage(@PathVariable String id, Model model) {
490
+ try {
491
+ Product product = productService.getProduct(id);
492
+ model.addAttribute("product", product);
493
+ return "product-detail";
494
+ } catch (Exception e) {
495
+ // 降级到静态页面
496
+ return "product-unavailable";
497
+ }
498
+ }
499
+ }
500
+ ```
501
+
502
+ ## 限流控制
503
+
504
+ ### 限流算法
505
+
506
+ #### 固定窗口计数器(Fixed Window)
507
+ ```
508
+ 原理:
509
+ - 将时间划分为固定窗口
510
+ - 每个窗口统计请求数
511
+ - 超过阈值则拒绝
512
+
513
+ 优点:
514
+ - 实现简单
515
+ - 内存占用小
516
+
517
+ 缺点:
518
+ - 临界时刻可能超限(突刺现象)
519
+ - 不够平滑
520
+
521
+ 实现:
522
+ if (counter.increment(windowKey) > limit) {
523
+ reject();
524
+ }
525
+ ```
526
+
527
+ #### 滑动窗口(Sliding Window)
528
+ ```
529
+ 原理:
530
+ - 将窗口细分为多个小格
531
+ - 滑动统计最近N个小格
532
+ - 平滑限流
533
+
534
+ 优点:
535
+ - 平滑限流
536
+ - 避免突刺
537
+
538
+ 缺点:
539
+ - 实现复杂
540
+ - 内存占用较大
541
+
542
+ 实现(Redis):
543
+ current_window = timestamp / granularity
544
+ count = sum(redis.get(window_i)) for i in [current_window - window_size, current_window]
545
+ if count > limit:
546
+ reject()
547
+ ```
548
+
549
+ #### 令牌桶(Token Bucket)
550
+ ```
551
+ 原理:
552
+ - 以固定速率生成令牌放入桶
553
+ - 桶有最大容量
554
+ - 请求获取令牌,获取不到则拒绝
555
+
556
+ 优点:
557
+ - 允许突发流量
558
+ - 平滑限流
559
+
560
+ 缺点:
561
+ - 需要维护令牌生成
562
+
563
+ 实现(Guava RateLimiter):
564
+ RateLimiter rateLimiter = RateLimiter.create(100); // 100 QPS
565
+ if (rateLimiter.tryAcquire()) {
566
+ process();
567
+ } else {
568
+ reject();
569
+ }
570
+ ```
571
+
572
+ #### 漏桶(Leaky Bucket)
573
+ ```
574
+ 原理:
575
+ - 请求进入桶
576
+ - 桶以固定速率流出
577
+ - 桶满则拒绝
578
+
579
+ 优点:
580
+ - 平滑流量
581
+ - 保护下游
582
+
583
+ 缺点:
584
+ - 不允许突发流量
585
+
586
+ 实现:
587
+ if (queue.offer(request)) {
588
+ // 入队成功
589
+ } else {
590
+ // 队列满,拒绝
591
+ }
592
+ ```
593
+
594
+ ### 限流实现
595
+
596
+ #### 基于Redis + Lua
597
+ ```java
598
+ // Lua脚本
599
+ String script =
600
+ "local key = KEYS[1] " +
601
+ "local limit = tonumber(ARGV[1]) " +
602
+ "local window = tonumber(ARGV[2]) " +
603
+ "local current = redis.call('INCR', key) " +
604
+ "if current == 1 then " +
605
+ " redis.call('EXPIRE', key, window) " +
606
+ "end " +
607
+ "return current <= limit";
608
+
609
+ // 使用
610
+ public boolean allowRequest(String key, int limit, int window) {
611
+ DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>(script, Boolean.class);
612
+ return redisTemplate.execute(
613
+ redisScript,
614
+ Collections.singletonList(key),
615
+ String.valueOf(limit),
616
+ String.valueOf(window)
617
+ );
618
+ }
619
+ ```
620
+
621
+ #### Spring Cloud Gateway限流
622
+ ```yaml
623
+ spring:
624
+ cloud:
625
+ gateway:
626
+ routes:
627
+ - id: order-service
628
+ uri: lb://order-service
629
+ predicates:
630
+ - Path=/orders/**
631
+ filters:
632
+ - name: RequestRateLimiter
633
+ args:
634
+ redis-rate-limiter.replenishRate: 10 # 每秒生成令牌数
635
+ redis-rate-limiter.burstCapacity: 20 # 桶容量
636
+ key-resolver: "#{@userKeyResolver}"
637
+
638
+ // KeyResolver
639
+ @Bean
640
+ public KeyResolver userKeyResolver() {
641
+ return exchange -> Mono.just(
642
+ exchange.getRequest().getHeaders().getFirst("X-User-Id")
643
+ );
644
+ }
645
+ ```
646
+
647
+ #### Sentinel限流
648
+ ```java
649
+ // 配置
650
+ FlowRule rule = new FlowRule();
651
+ rule.setResource("createOrder");
652
+ rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
653
+ rule.setCount(100); // 100 QPS
654
+ FlowRuleManager.loadRules(Collections.singletonList(rule));
655
+
656
+ // 使用
657
+ public Order createOrder(OrderRequest request) {
658
+ try (Entry entry = SphU.entry("createOrder")) {
659
+ // 业务逻辑
660
+ return orderService.create(request);
661
+ } catch (BlockException e) {
662
+ // 被限流
663
+ throw new RateLimitException("请求过于频繁");
664
+ }
665
+ }
666
+
667
+ // 注解方式
668
+ @SentinelResource(value = "createOrder", blockHandler = "handleBlock")
669
+ public Order createOrder(OrderRequest request) {
670
+ return orderService.create(request);
671
+ }
672
+
673
+ public Order handleBlock(OrderRequest request, BlockException e) {
674
+ // 限流降级逻辑
675
+ throw new RateLimitException("请求过于频繁");
676
+ }
677
+ ```
678
+
679
+ ### 分布式限流
680
+
681
+ #### Redis + Lua实现分布式令牌桶
682
+ ```lua
683
+ -- distributed_rate_limiter.lua
684
+ local key = KEYS[1]
685
+ local permits = tonumber(ARGV[1]) -- 请求数量
686
+ local max_burst = tonumber(ARGV[2]) -- 最大突发
687
+ local rate = tonumber(ARGV[3]) -- 速率
688
+ local now = tonumber(ARGV[4])
689
+
690
+ local info = redis.call("HMGET", key, "tokens", "last_refill")
691
+ local tokens = tonumber(info[1])
692
+ local last_refill = tonumber(info[2])
693
+
694
+ if tokens == nil then
695
+ tokens = max_burst
696
+ last_refill = now
697
+ end
698
+
699
+ -- 计算新令牌
700
+ local interval = now - last_refill
701
+ local new_tokens = interval * rate
702
+ tokens = math.min(max_burst, tokens + new_tokens)
703
+
704
+ -- 检查是否足够
705
+ if tokens < permits then
706
+ return 0 -- 拒绝
707
+ end
708
+
709
+ -- 扣减令牌
710
+ tokens = tokens - permits
711
+ redis.call("HMSET", key, "tokens", tokens, "last_refill", now)
712
+ redis.call("EXPIRE", key, math.ceil(max_burst / rate) + 1)
713
+
714
+ return 1 -- 允许
715
+ ```
716
+
717
+ ```java
718
+ public class DistributedRateLimiter {
719
+ private RedisTemplate<String, String> redisTemplate;
720
+ private String script;
721
+
722
+ public boolean acquire(String key, int permits, int maxBurst, double rate) {
723
+ DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(script, Long.class);
724
+ Long result = redisTemplate.execute(
725
+ redisScript,
726
+ Collections.singletonList(key),
727
+ String.valueOf(permits),
728
+ String.valueOf(maxBurst),
729
+ String.valueOf(rate),
730
+ String.valueOf(System.currentTimeMillis())
731
+ );
732
+ return result != null && result == 1;
733
+ }
734
+ }
735
+ ```
736
+
737
+ ## 超时控制
738
+
739
+ ### 超时设置原则
740
+ ```
741
+ 连接超时(Connection Timeout):
742
+ - 建立连接的超时时间
743
+ - 建议: 1-3秒
744
+
745
+ 读取超时(Read Timeout):
746
+ - 等待响应的超时时间
747
+ - 建议: 根据业务RT设置,通常3-10秒
748
+
749
+ 写超时(Write Timeout):
750
+ - 发送数据的超时时间
751
+ - 建议: 1-3秒
752
+ ```
753
+
754
+ ### 实现示例
755
+
756
+ #### RestTemplate
757
+ ```java
758
+ @Bean
759
+ public RestTemplate restTemplate() {
760
+ HttpComponentsClientHttpRequestFactory factory =
761
+ new HttpComponentsClientHttpRequestFactory();
762
+ factory.setConnectTimeout(3000); // 连接超时3秒
763
+ factory.setReadTimeout(5000); // 读取超时5秒
764
+ return new RestTemplate(factory);
765
+ }
766
+ ```
767
+
768
+ #### Feign
769
+ ```yaml
770
+ feign:
771
+ client:
772
+ config:
773
+ default:
774
+ connectTimeout: 3000
775
+ readTimeout: 5000
776
+ inventory-service:
777
+ connectTimeout: 2000
778
+ readTimeout: 3000
779
+ ```
780
+
781
+ #### OkHttp
782
+ ```java
783
+ @Bean
784
+ public OkHttpClient okHttpClient() {
785
+ return new OkHttpClient.Builder()
786
+ .connectTimeout(3, TimeUnit.SECONDS)
787
+ .readTimeout(5, TimeUnit.SECONDS)
788
+ .writeTimeout(3, TimeUnit.SECONDS)
789
+ .retryOnConnectionFailure(true)
790
+ .build();
791
+ }
792
+ ```
793
+
794
+ ## 重试机制
795
+
796
+ ### 重试策略
797
+
798
+ #### 固定间隔重试
799
+ ```java
800
+ @Retryable(
801
+ value = {RemoteServiceException.class},
802
+ maxAttempts = 3,
803
+ backoff = @Backoff(delay = 1000) // 固定1秒
804
+ )
805
+ public Product getProduct(String productId) {
806
+ return productClient.getProduct(productId);
807
+ }
808
+ ```
809
+
810
+ #### 指数退避重试
811
+ ```java
812
+ @Retryable(
813
+ value = {RemoteServiceException.class},
814
+ maxAttempts = 3,
815
+ backoff = @Backoff(delay = 1000, multiplier = 2) // 1s, 2s, 4s
816
+ )
817
+ public Product getProduct(String productId) {
818
+ return productClient.getProduct(productId);
819
+ }
820
+ ```
821
+
822
+ ### 重试注意事项
823
+ ```
824
+ 必须条件:
825
+ - 幂等性: 重试必须保证操作幂等
826
+ - 可重试异常: 只对可恢复异常重试
827
+ - 最大重试次数: 避免无限重试
828
+ - 退避策略: 避免重试风暴
829
+
830
+ 不适合重试的场景:
831
+ - 非幂等操作(如扣款)
832
+ - 业务异常(如余额不足)
833
+ - 资源不存在(404)
834
+ ```
835
+
836
+ ## 服务容错
837
+
838
+ ### 舱壁模式(Bulkhead)
839
+ ```
840
+ 原理:
841
+ - 隔离资源,防止故障扩散
842
+ - 为每个服务分配独立资源池
843
+
844
+ 实现(Resilience4j):
845
+ @Bean
846
+ public BulkheadConfig bulkheadConfig() {
847
+ return BulkheadConfig.custom()
848
+ .maxConcurrentCalls(10) // 最大并发数
849
+ .maxWaitDuration(Duration.ofMillis(500)) // 等待时间
850
+ .build();
851
+ }
852
+
853
+ @Service
854
+ public class OrderService {
855
+ @Bulkhead(name = "inventoryService", fallbackMethod = "fallback")
856
+ public InventoryResponse checkInventory(String productId) {
857
+ return inventoryClient.checkInventory(productId);
858
+ }
859
+
860
+ public InventoryResponse fallback(String productId) {
861
+ return InventoryResponse.defaultResponse();
862
+ }
863
+ }
864
+ ```
865
+
866
+ ### 故障隔离
867
+ ```
868
+ 线程池隔离:
869
+ - 每个服务使用独立线程池
870
+ - 故障不会影响其他服务
871
+
872
+ 信号量隔离:
873
+ - 共享线程池,使用信号量限制并发
874
+ - 轻量级,适合内部调用
875
+
876
+ 选择:
877
+ - 网络调用: 线程池隔离
878
+ - 本地调用: 信号量隔离
879
+ ```
880
+
881
+ ## 灰度发布
882
+
883
+ ### 基于权重的灰度
884
+ ```yaml
885
+ spring:
886
+ cloud:
887
+ nacos:
888
+ discovery:
889
+ metadata:
890
+ version: v2
891
+ weight: 20 # 20%流量
892
+ ```
893
+
894
+ ### 基于Header的灰度
895
+ ```java
896
+ @Configuration
897
+ public class GrayLoadBalancerConfig {
898
+ @Bean
899
+ ReactorLoadBalancer<ServiceInstance> grayLoadBalancer(
900
+ Environment environment,
901
+ LoadBalancerClientFactory factory) {
902
+ return new GrayLoadBalancer(
903
+ factory.getLazyProvider(environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME), ServiceInstanceListSupplier.class),
904
+ environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME)
905
+ );
906
+ }
907
+ }
908
+
909
+ public class GrayLoadBalancer implements ReactorServiceInstanceLoadBalancer {
910
+ @Override
911
+ public Mono<Response<ServiceInstance>> choose(Request request) {
912
+ DefaultRequestContext context = (DefaultRequestContext) request.getContext();
913
+ HttpHeaders headers = (HttpHeaders) context.getClientRequest().getHeaders();
914
+
915
+ String version = headers.getFirst("X-Service-Version");
916
+
917
+ return serviceInstanceListSupplierProvider.getIfAvailable()
918
+ .get()
919
+ .next()
920
+ .map(instances -> {
921
+ List<ServiceInstance> filtered = instances.stream()
922
+ .filter(instance -> version == null ||
923
+ version.equals(instance.getMetadata().get("version")))
924
+ .collect(Collectors.toList());
925
+
926
+ if (filtered.isEmpty()) {
927
+ filtered = instances;
928
+ }
929
+
930
+ ServiceInstance instance = selectInstance(filtered);
931
+ return new DefaultResponse(instance);
932
+ });
933
+ }
934
+ }
935
+ ```
936
+
937
+ ## 服务监控
938
+
939
+ ### 健康检查
940
+
941
+ #### Spring Boot Actuator
942
+ ```yaml
943
+ management:
944
+ endpoints:
945
+ web:
946
+ exposure:
947
+ include: health,info,metrics
948
+ endpoint:
949
+ health:
950
+ show-details: always
951
+ ```
952
+
953
+ #### 自定义健康检查
954
+ ```java
955
+ @Component
956
+ public class InventoryServiceHealthIndicator implements HealthIndicator {
957
+ @Autowired
958
+ private InventoryClient inventoryClient;
959
+
960
+ @Override
961
+ public Health health() {
962
+ try {
963
+ HealthStatus status = inventoryClient.checkHealth();
964
+ if (status.isHealthy()) {
965
+ return Health.up()
966
+ .withDetail("inventory-service", "available")
967
+ .build();
968
+ } else {
969
+ return Health.down()
970
+ .withDetail("inventory-service", "unavailable")
971
+ .build();
972
+ }
973
+ } catch (Exception e) {
974
+ return Health.down(e).build();
975
+ }
976
+ }
977
+ }
978
+ ```
979
+
980
+ ### 指标采集
981
+
982
+ #### Prometheus + Micrometer
983
+ ```java
984
+ // 依赖
985
+ implementation 'io.micrometer:micrometer-registry-prometheus'
986
+
987
+ // 自定义指标
988
+ @Service
989
+ public class OrderService {
990
+ private final Counter orderCounter;
991
+ private final Timer orderTimer;
992
+
993
+ public OrderService(MeterRegistry registry) {
994
+ this.orderCounter = Counter.builder("order.count")
995
+ .description("Total order count")
996
+ .tag("type", "normal")
997
+ .register(registry);
998
+
999
+ this.orderTimer = Timer.builder("order.latency")
1000
+ .description("Order processing latency")
1001
+ .register(registry);
1002
+ }
1003
+
1004
+ public Order createOrder(OrderRequest request) {
1005
+ return orderTimer.record(() -> {
1006
+ Order order = // 业务逻辑
1007
+ orderCounter.increment();
1008
+ return order;
1009
+ });
1010
+ }
1011
+ }
1012
+ ```
1013
+
1014
+ ```yaml
1015
+ # application.yml
1016
+ management:
1017
+ endpoints:
1018
+ web:
1019
+ exposure:
1020
+ include: prometheus
1021
+ metrics:
1022
+ tags:
1023
+ application: ${spring.application.name}
1024
+ export:
1025
+ prometheus:
1026
+ enabled: true
1027
+ ```
1028
+
1029
+ ## 服务网格治理
1030
+
1031
+ ### Istio流量管理
1032
+
1033
+ #### 虚拟服务
1034
+ ```yaml
1035
+ apiVersion: networking.istio.io/v1beta1
1036
+ kind: VirtualService
1037
+ metadata:
1038
+ name: order-service
1039
+ spec:
1040
+ hosts:
1041
+ - order-service
1042
+ http:
1043
+ - match:
1044
+ - headers:
1045
+ x-user-type:
1046
+ exact: vip
1047
+ route:
1048
+ - destination:
1049
+ host: order-service
1050
+ subset: v2
1051
+ weight: 100
1052
+ - route:
1053
+ - destination:
1054
+ host: order-service
1055
+ subset: v1
1056
+ weight: 90
1057
+ - destination:
1058
+ host: order-service
1059
+ subset: v2
1060
+ weight: 10
1061
+ ```
1062
+
1063
+ #### 目标规则
1064
+ ```yaml
1065
+ apiVersion: networking.istio.io/v1beta1
1066
+ kind: DestinationRule
1067
+ metadata:
1068
+ name: order-service
1069
+ spec:
1070
+ host: order-service
1071
+ trafficPolicy:
1072
+ connectionPool:
1073
+ tcp:
1074
+ maxConnections: 100
1075
+ http:
1076
+ h2UpgradePolicy: UPGRADE
1077
+ http1MaxPendingRequests: 100
1078
+ http2MaxRequests: 1000
1079
+ outlierDetection:
1080
+ consecutive5xxErrors: 5
1081
+ interval: 30s
1082
+ baseEjectionTime: 30s
1083
+ maxEjectionPercent: 50
1084
+ subsets:
1085
+ - name: v1
1086
+ labels:
1087
+ version: v1
1088
+ - name: v2
1089
+ labels:
1090
+ version: v2
1091
+ ```
1092
+
1093
+ ### Envoy过滤器
1094
+ ```yaml
1095
+ apiVersion: networking.istio.io/v1alpha3
1096
+ kind: EnvoyFilter
1097
+ metadata:
1098
+ name: custom-filter
1099
+ spec:
1100
+ workloadLabels:
1101
+ app: order-service
1102
+ filters:
1103
+ - filterName: envoy.lua
1104
+ filterType: HTTP
1105
+ filterConfig:
1106
+ inline_code: |
1107
+ function envoy_on_request(request_handle)
1108
+ -- 自定义逻辑
1109
+ end
1110
+ ```
1111
+
1112
+ ## 最佳实践
1113
+
1114
+ ### 1. 服务治理分层
1115
+ ```
1116
+ 基础设施层:
1117
+ - Kubernetes Service
1118
+ - Istio Service Mesh
1119
+
1120
+ 应用层:
1121
+ - Spring Cloud
1122
+ - Dubbo
1123
+
1124
+ 混合治理:
1125
+ - 基础设施层 + 应用层
1126
+ - 渐进式演进
1127
+ ```
1128
+
1129
+ ### 2. 容错设计原则
1130
+ ```
1131
+ 快速失败(Fail Fast):
1132
+ - 及时返回错误
1133
+ - 避免资源占用
1134
+
1135
+ 优雅降级(Graceful Degradation):
1136
+ - 提供有损服务
1137
+ - 保证核心功能
1138
+
1139
+ 自我保护:
1140
+ - 限流熔断
1141
+ - 资源隔离
1142
+
1143
+ 自我恢复:
1144
+ - 自动重试
1145
+ - 熔断器自动恢复
1146
+ ```
1147
+
1148
+ ### 3. 监控告警
1149
+ ```
1150
+ 关键指标:
1151
+ - 服务可用性(99.9%+)
1152
+ - 响应时间(P99 < 500ms)
1153
+ - 错误率(< 0.1%)
1154
+ - QPS
1155
+
1156
+ 告警级别:
1157
+ - P0: 服务不可用(短信+电话)
1158
+ - P1: 性能下降(短信)
1159
+ - P2: 异常趋势(邮件)
1160
+ ```
1161
+
1162
+ ### 4. 演进策略
1163
+ ```
1164
+ 阶段一: 基础治理
1165
+ - 服务注册发现
1166
+ - 负载均衡
1167
+ - 健康检查
1168
+
1169
+ 阶段二: 容错治理
1170
+ - 熔断降级
1171
+ - 限流控制
1172
+ - 重试机制
1173
+
1174
+ 阶段三: 流量治理
1175
+ - 灰度发布
1176
+ - 流量镜像
1177
+ - A/B测试
1178
+
1179
+ 阶段四: 智能治理
1180
+ - 自适应限流
1181
+ - 智能路由
1182
+ - AIOps
1183
+ ```
1184
+
1185
+ ## 参考资源
1186
+
1187
+ ### 开源框架
1188
+ - Spring Cloud: https://spring.io/projects/spring-cloud
1189
+ - Dubbo: https://dubbo.apache.org/
1190
+ - Sentinel: https://sentinelguard.io/
1191
+ - Resilience4j: https://resilience4j.readme.io/
1192
+ - Istio: https://istio.io/
1193
+
1194
+ ### 学习资料
1195
+ - 《微服务设计》
1196
+ - 《Release It!》
1197
+ - Google SRE Book
1198
+ - Netflix技术博客