@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.
- package/00-governance/governance-capabilities.md +557 -0
- package/00-governance/knowledge-map.md +39 -0
- package/00-governance/maintenance-policy.md +76 -0
- package/00-governance/review-checklist.md +81 -0
- package/README.md +13 -0
- package/ai/01-standards/agent-development-complete.md +691 -0
- package/ai/01-standards/llm-application-complete.md +488 -0
- package/ai/01-standards/mlops-complete.md +798 -0
- package/ai/01-standards/prompt-engineering-complete.md +646 -0
- package/ai/01-standards/rag-architecture-complete.md +649 -0
- package/ai/02-playbooks/llm-evaluation-playbook.md +847 -0
- package/ai/03-checklists/ai-project-checklist.md +215 -0
- package/ai/04-antipatterns/ai-antipatterns.md +661 -0
- package/ai/05-cases/case-rag-production.md +147 -0
- package/ai/06-glossary/ai-glossary.md +162 -0
- package/ai/agent-evaluation-benchmark.md +53 -0
- package/ai/ai-agent-memory-context-management.md +41 -0
- package/ai/ai-cost-capacity-optimization-playbook.md +42 -0
- package/ai/ai-data-security-and-compliance-playbook.md +37 -0
- package/ai/ai-domain-index-and-checklist.md +40 -0
- package/ai/ai-governance-maturity-model.md +50 -0
- package/ai/ai-model-selection-and-routing-strategy.md +47 -0
- package/ai/ai-observability-and-oncall-runbook.md +52 -0
- package/ai/ai-rag-engineering-playbook.md +42 -0
- package/ai/ai-red-team-and-safety-evaluation.md +42 -0
- package/ai/ai-release-readiness-and-rollback-gate.md +42 -0
- package/ai/llm-agent-engineering-deep-dive.md +57 -0
- package/ai/prompt-and-tool-guardrails.md +52 -0
- package/api/01-standards/enterprise-api-standards.md +198 -0
- package/api/01-standards/rest-api-design-guide.md +63 -0
- package/api/02-playbooks/api-pagination-playbook.md +93 -0
- package/api/02-playbooks/graphql-production-playbook.md +176 -0
- package/api/03-checklists/api-review-checklist.md +55 -0
- package/api/04-antipatterns/api-antipatterns.md +112 -0
- package/architecture/01-standards/api-gateway-patterns.md +496 -0
- package/architecture/01-standards/cloud-native-patterns.md +644 -0
- package/architecture/01-standards/distributed-systems-patterns.md +591 -0
- package/architecture/01-standards/event-driven-architecture.md +595 -0
- package/architecture/01-standards/microservices-patterns-complete.md +968 -0
- package/architecture/01-standards/microservices-patterns.md +495 -0
- package/architecture/01-standards/system-design-interview.md +664 -0
- package/architecture/02-playbooks/microservices-patterns-playbook.md +137 -0
- package/architecture/02-playbooks/migration-playbook.md +780 -0
- package/architecture/02-playbooks/system-design-playbook.md +779 -0
- package/architecture/03-checklists/architecture-decision-checklist.md +297 -0
- package/architecture/04-antipatterns/architecture-antipatterns.md +417 -0
- package/architecture/05-cases/case-netflix-microservices.md +413 -0
- package/architecture/06-glossary/architecture-glossary.md +164 -0
- package/architecture/adr-template-and-examples.md +38 -0
- package/architecture/api-gateway-deep-dive.md +1291 -0
- package/architecture/configuration-management.md +1162 -0
- package/architecture/distributed-transactions.md +1220 -0
- package/architecture/microservices-complete.md +735 -0
- package/architecture/resilience-and-disaster-patterns.md +37 -0
- package/architecture/service-governance.md +1198 -0
- package/architecture/system-architecture-deep-dive.md +37 -0
- package/backend/01-standards/analytics-and-growth.md +65 -0
- package/backend/01-standards/api-and-error-conventions.md +120 -0
- package/backend/01-standards/application-layering-and-packaging.md +160 -0
- package/backend/01-standards/auth-implementation.md +104 -0
- package/backend/01-standards/backend-framework-idioms.md +74 -0
- package/backend/01-standards/background-jobs-and-async.md +66 -0
- package/backend/01-standards/caching-strategies-complete.md +390 -0
- package/backend/01-standards/config-and-observability.md +77 -0
- package/backend/01-standards/data-modeling-and-persistence.md +94 -0
- package/backend/01-standards/django-complete.md +1765 -0
- package/backend/01-standards/email-and-notifications.md +64 -0
- package/backend/01-standards/fastapi-complete.md +925 -0
- package/backend/01-standards/file-upload-and-storage.md +66 -0
- package/backend/01-standards/graphql-api-complete.md +416 -0
- package/backend/01-standards/llm-application-standard.md +78 -0
- package/backend/01-standards/message-queue-patterns.md +379 -0
- package/backend/01-standards/microservices-and-distributed.md +78 -0
- package/backend/01-standards/nestjs-complete.md +2167 -0
- package/backend/01-standards/payment-integration.md +80 -0
- package/backend/01-standards/rate-limiting-complete.md +451 -0
- package/backend/01-standards/realtime-and-websocket.md +65 -0
- package/backend/01-standards/search-and-filtering.md +64 -0
- package/backend/01-standards/spring-boot-complete.md +445 -0
- package/backend/02-playbooks/api-design-playbook.md +718 -0
- package/backend/02-playbooks/email-send-playbook.md +130 -0
- package/backend/02-playbooks/file-upload-s3-playbook.md +153 -0
- package/backend/02-playbooks/typescript-enterprise-playbook.md +133 -0
- package/backend/02-playbooks/websocket-realtime-playbook.md +154 -0
- package/backend/03-checklists/api-launch-checklist.md +189 -0
- package/backend/04-antipatterns/backend-antipatterns.md +1051 -0
- package/blockchain/01-standards/blockchain-basics.md +557 -0
- package/blockchain/01-standards/smart-contract-development.md +1315 -0
- package/cicd/01-standards/deployment-and-delivery-standard.md +96 -0
- package/cicd/01-standards/github-actions-complete.md +473 -0
- package/cicd/01-standards/release-and-store-submission.md +75 -0
- package/cicd/02-playbooks/cicd-pipeline-playbook.md +144 -0
- package/cicd/02-playbooks/release-management-playbook.md +605 -0
- package/cicd/03-checklists/pipeline-security-checklist.md +168 -0
- package/cicd/04-antipatterns/cicd-antipatterns.md +589 -0
- package/cicd/05-cases/case-deployment-automation.md +221 -0
- package/cicd/05-cases/case-gitops-transformation.md +212 -0
- package/cicd/06-glossary/cicd-glossary.md +114 -0
- package/cicd/cicd-blueprint-deep-dive.md +38 -0
- package/cicd/release-readiness-gate.md +37 -0
- package/cloud-native/01-standards/container-security.md +741 -0
- package/cloud-native/01-standards/kubernetes-complete.md +812 -0
- package/cloud-native/02-playbooks/api-gateway-playbook.md +155 -0
- package/cloud-native/02-playbooks/gitops-with-argocd.md +760 -0
- package/cloud-native/02-playbooks/k8s-troubleshooting-playbook.md +1942 -0
- package/cloud-native/02-playbooks/message-queue-playbook.md +129 -0
- package/cloud-native/02-playbooks/multicloud-governance.md +726 -0
- package/cloud-native/02-playbooks/serverless-patterns.md +788 -0
- package/cloud-native/02-playbooks/service-mesh-playbook.md +612 -0
- package/cloud-native/02-playbooks/terraform-iac-playbook.md +143 -0
- package/cloud-native/03-checklists/container-security-checklist.md +431 -0
- package/cloud-native/03-checklists/k8s-production-readiness-checklist.md +460 -0
- package/cloud-native/04-antipatterns/container-antipatterns.md +660 -0
- package/cloud-native/04-antipatterns/k8s-antipatterns.md +743 -0
- package/cloud-native/05-cases/case-k8s-migration.md +478 -0
- package/cloud-native/05-cases/case-k8s-scaling.md +642 -0
- package/cloud-native/05-cases/case-k8s-security-incident.md +397 -0
- package/cloud-native/06-glossary/cloud-native-glossary.md +337 -0
- package/cross-platform/01-standards/cross-platform-frameworks.md +83 -0
- package/cross-platform/01-standards/platform-selection-and-architecture.md +77 -0
- package/data/01-standards/elasticsearch-complete.md +2098 -0
- package/data/01-standards/postgresql-complete.md +1613 -0
- package/data/01-standards/redis-complete.md +1527 -0
- package/data/02-playbooks/database-optimization-playbook.md +403 -0
- package/data/02-playbooks/elasticsearch-production-playbook.md +132 -0
- package/data/03-checklists/database-launch-checklist.md +187 -0
- package/data/04-antipatterns/database-antipatterns.md +873 -0
- package/data/05-cases/case-database-migration.md +310 -0
- package/data/06-glossary/database-glossary.md +440 -0
- package/data/data-governance-and-modeling-deep-dive.md +39 -0
- package/data-engineering/01-standards/airflow-complete.md +523 -0
- package/data-engineering/01-standards/kafka-complete.md +1521 -0
- package/data-engineering/02-playbooks/spark-etl-playbook.md +496 -0
- package/data-engineering/03-checklists/pipeline-launch-checklist.md +194 -0
- package/data-engineering/04-antipatterns/data-pipeline-antipatterns.md +684 -0
- package/data-engineering/05-cases/case-real-time-pipeline.md +355 -0
- package/data-engineering/06-glossary/data-engineering-glossary.md +429 -0
- package/database/01-standards/database-schema-standards.md +147 -0
- package/database/02-playbooks/postgresql-optimization-quick.md +52 -0
- package/database/02-playbooks/postgresql-performance-optimization.md +58 -0
- package/database/02-playbooks/postgresql-production-playbook.md +146 -0
- package/database/02-playbooks/redis-caching-playbook.md +117 -0
- package/database/03-checklists/database-review-checklist.md +50 -0
- package/database/04-antipatterns/database-antipatterns.md +112 -0
- package/design/01-standards/ui-design-system-complete.md +423 -0
- package/design/02-playbooks/design-handoff-playbook.md +254 -0
- package/design/02-playbooks/design-review-playbook.md +388 -0
- package/design/03-checklists/design-review-checklist.md +246 -0
- package/design/04-antipatterns/design-antipatterns.md +378 -0
- package/design/05-cases/case-design-system-adoption.md +328 -0
- package/design/06-glossary/design-glossary.md +329 -0
- package/design/ui-full-lifecycle-cross-platform-playbook.md +571 -0
- package/design/ux-system-deep-dive.md +38 -0
- package/design-systems/00-craft-rules.md +71 -0
- package/design-systems/aesthetic-families.md +43 -0
- package/design-systems/anti-ai-slop.md +162 -0
- package/design-systems/bold-geometric.md +120 -0
- package/design-systems/brutalist-bold.md +103 -0
- package/design-systems/editorial-clean.md +109 -0
- package/design-systems/glass-aurora.md +108 -0
- package/design-systems/modern-minimal.md +145 -0
- package/design-systems/premium-luxury.md +106 -0
- package/design-systems/product-type-design-map.md +48 -0
- package/design-systems/soft-warm.md +123 -0
- package/design-systems/tech-utility.md +113 -0
- package/desktop/01-standards/desktop-app-standard.md +72 -0
- package/desktop/01-standards/desktop-design.md +71 -0
- package/development/00-governance/document-template.md +41 -0
- package/development/01-standards/api-versioning-strategies.md +432 -0
- package/development/01-standards/authentication-patterns-complete.md +479 -0
- package/development/01-standards/css-architecture-complete.md +550 -0
- package/development/01-standards/database-migration-strategies.md +484 -0
- package/development/01-standards/elasticsearch-complete.md +347 -0
- package/development/01-standards/git-complete.md +371 -0
- package/development/01-standards/golang-complete.md +1565 -0
- package/development/01-standards/graphql-complete.md +298 -0
- package/development/01-standards/javascript-bundlers-complete.md +469 -0
- package/development/01-standards/javascript-typescript-complete.md +528 -0
- package/development/01-standards/jest-complete.md +275 -0
- package/development/01-standards/linux-complete.md +234 -0
- package/development/01-standards/logging-observability-complete.md +526 -0
- package/development/01-standards/microservices-communication.md +502 -0
- package/development/01-standards/mongodb-complete.md +406 -0
- package/development/01-standards/oauth2-complete.md +285 -0
- package/development/01-standards/performance-optimization-complete.md +289 -0
- package/development/01-standards/playwright-complete.md +247 -0
- package/development/01-standards/postgresql-complete.md +456 -0
- package/development/01-standards/pytest-complete.md +340 -0
- package/development/01-standards/python-async-programming.md +902 -0
- package/development/01-standards/python-complete.md +956 -0
- package/development/01-standards/python-decorators-complete.md +799 -0
- package/development/01-standards/python-design-patterns.md +2854 -0
- package/development/01-standards/python-packaging-distribution.md +420 -0
- package/development/01-standards/python-testing-strategies.md +607 -0
- package/development/01-standards/python-web-frameworks-comparison.md +471 -0
- package/development/01-standards/redis-complete.md +317 -0
- package/development/01-standards/rest-api-complete.md +316 -0
- package/development/01-standards/rust-complete.md +578 -0
- package/development/01-standards/typescript-advanced-types.md +1513 -0
- package/development/01-standards/web-security-complete.md +292 -0
- package/development/02-playbooks/api-design-playbook.md +810 -0
- package/development/02-playbooks/database-migration-playbook.md +580 -0
- package/development/02-playbooks/debugging-playbook.md +692 -0
- package/development/02-playbooks/feature-delivery-playbook.md +430 -0
- package/development/02-playbooks/incident-hotfix-playbook.md +387 -0
- package/development/02-playbooks/performance-optimization-playbook.md +531 -0
- package/development/02-playbooks/performance-tuning-playbook.md +652 -0
- package/development/02-playbooks/refactor-playbook.md +403 -0
- package/development/02-playbooks/release-playbook.md +469 -0
- package/development/03-checklists/architecture-review-checklist.md +168 -0
- package/development/03-checklists/data-migration-checklist.md +157 -0
- package/development/03-checklists/oncall-handover-checklist.md +173 -0
- package/development/03-checklists/pr-checklist.md +158 -0
- package/development/03-checklists/production-readiness-checklist.md +190 -0
- package/development/03-checklists/release-readiness-checklist.md +154 -0
- package/development/03-checklists/security-review-checklist.md +182 -0
- package/development/04-antipatterns/api-antipatterns.md +657 -0
- package/development/04-antipatterns/architecture-antipatterns.md +686 -0
- package/development/04-antipatterns/backend-antipatterns.md +648 -0
- package/development/04-antipatterns/cicd-antipatterns.md +540 -0
- package/development/04-antipatterns/code-smell-antipatterns.md +571 -0
- package/development/04-antipatterns/data-antipatterns.md +658 -0
- package/development/04-antipatterns/database-antipatterns.md +578 -0
- package/development/04-antipatterns/frontend-antipatterns.md +635 -0
- package/development/04-antipatterns/reliability-antipatterns.md +700 -0
- package/development/04-antipatterns/security-antipatterns.md +747 -0
- package/development/05-cases/case-api-version-migration.md +428 -0
- package/development/05-cases/case-authorization-hardening.md +383 -0
- package/development/05-cases/case-bluegreen-rollback.md +466 -0
- package/development/05-cases/case-cache-snowball-protection.md +485 -0
- package/development/05-cases/case-ci-cd-pipeline.md +544 -0
- package/development/05-cases/case-database-scaling.md +500 -0
- package/development/05-cases/case-db-hotspot-optimization.md +487 -0
- package/development/05-cases/case-incident-mttr-reduction.md +563 -0
- package/development/05-cases/case-microservice-migration.md +375 -0
- package/development/05-cases/case-performance-optimization.md +406 -0
- package/development/05-cases/case-security-incident-response.md +345 -0
- package/development/06-glossary/full-stack-glossary.md +166 -0
- package/development/09-maturity/quarterly-audit-template.md +35 -0
- package/development/11-ui-excellence/ui-aesthetic-system.md +41 -0
- package/development/11-ui-excellence/ui-engineering-excellence.md +435 -0
- package/development/12-scenarios/development-scenarios-guide.md +565 -0
- package/development/13-implementation-assets/implementation-toolkit.md +282 -0
- package/development/13-implementation-assets/knowledge-gates-execution.md +43 -0
- package/development/14-full-lifecycle/software-lifecycle-gates.md +511 -0
- package/development/15-lifecycle-templates/project-templates-collection.md +791 -0
- package/development/api-contract-and-versioning-guide.md +36 -0
- package/development/api-governance-complete.md +43 -0
- package/development/backend-engineering-complete.md +43 -0
- package/development/code-review-quality-complete.md +43 -0
- package/development/concurrency-reliability-complete.md +43 -0
- package/development/database-engineering-complete.md +43 -0
- package/development/engineering-effectiveness-complete.md +43 -0
- package/development/engineering-standards-deep-dive.md +38 -0
- package/development/frontend-engineering-complete.md +43 -0
- package/development/performance-capacity-complete.md +43 -0
- package/development/refactor-migration-complete.md +42 -0
- package/development/refactoring-and-techdebt-playbook.md +37 -0
- package/development/security-in-development-complete.md +43 -0
- package/devops/01-standards/cicd-pipeline-complete.md +262 -0
- package/devops/01-standards/docker-complete.md +1490 -0
- package/devops/01-standards/github-actions-complete.md +337 -0
- package/devops/01-standards/kubernetes-complete.md +638 -0
- package/devops/01-standards/terraform-complete.md +2117 -0
- package/devops/02-playbooks/docker-compose-playbook.md +233 -0
- package/devops/02-playbooks/docker-k8s-production-playbook.md +186 -0
- package/devops/02-playbooks/docker-production-playbook.md +952 -0
- package/edge-iot/01-standards/edge-iot-complete.md +473 -0
- package/experts/architect/api-design.md +178 -0
- package/experts/architect/methodology.md +124 -0
- package/experts/architect/security.md +75 -0
- package/experts/backend-lead/methodology.md +216 -0
- package/experts/devops/methodology.md +160 -0
- package/experts/frontend-lead/methodology.md +178 -0
- package/experts/product-manager/industry/ecommerce.md +43 -0
- package/experts/product-manager/industry/saas.md +40 -0
- package/experts/product-manager/methodology.md +97 -0
- package/experts/qa-lead/methodology.md +123 -0
- package/experts/qa-lead/test-strategy.md +128 -0
- package/experts/uiux-designer/methodology.md +125 -0
- package/frontend/01-standards/accessibility-complete.md +532 -0
- package/frontend/01-standards/accessibility-standard.md +74 -0
- package/frontend/01-standards/admin-dashboard-and-crud.md +72 -0
- package/frontend/01-standards/design-tokens-complete.md +444 -0
- package/frontend/01-standards/forms-and-validation.md +77 -0
- package/frontend/01-standards/frontend-architecture-and-layering.md +119 -0
- package/frontend/01-standards/i18n-and-localization.md +65 -0
- package/frontend/01-standards/nextjs-complete.md +451 -0
- package/frontend/01-standards/react-complete.md +713 -0
- package/frontend/01-standards/react-hooks-complete-guide.md +1100 -0
- package/frontend/01-standards/react-hooks-complete.md +1171 -0
- package/frontend/01-standards/seo-and-web-vitals.md +77 -0
- package/frontend/01-standards/state-management-complete.md +444 -0
- package/frontend/01-standards/vue-complete.md +499 -0
- package/frontend/01-standards/vue3-complete.md +2002 -0
- package/frontend/01-standards/web-framework-best-practices.md +64 -0
- package/frontend/01-standards/web-performance-complete.md +495 -0
- package/frontend/02-playbooks/accessibility-a11y-playbook.md +161 -0
- package/frontend/02-playbooks/frontend-performance-playbook.md +707 -0
- package/frontend/02-playbooks/i18n-internationalization-playbook.md +120 -0
- package/frontend/02-playbooks/performance-optimization-playbook.md +163 -0
- package/frontend/02-playbooks/react-nextjs-production-playbook.md +167 -0
- package/frontend/02-playbooks/react-state-management-playbook.md +173 -0
- package/frontend/03-checklists/component-quality-checklist.md +166 -0
- package/frontend/03-checklists/frontend-launch-checklist.md +299 -0
- package/frontend/04-antipatterns/frontend-antipatterns.md +886 -0
- package/frontend/05-cases/case-performance-optimization.md +274 -0
- package/harmony/01-standards/harmonyos-arkts-standard.md +75 -0
- package/harmony/01-standards/harmonyos-design.md +65 -0
- package/high-quality-engineering-playbook.md +54 -0
- package/incident/01-standards/incident-response-complete.md +303 -0
- package/incident/02-playbooks/chaos-engineering-playbook.md +883 -0
- package/incident/02-playbooks/postmortem-playbook.md +398 -0
- package/incident/03-checklists/incident-readiness-checklist.md +181 -0
- package/incident/04-antipatterns/incident-antipatterns.md +490 -0
- package/incident/05-cases/case-cascade-failure.md +176 -0
- package/incident/06-glossary/incident-glossary.md +114 -0
- package/incident/postmortem-and-response-deep-dive.md +39 -0
- package/industries/ecommerce/ecommerce-complete.md +631 -0
- package/industries/education/education-complete.md +555 -0
- package/industries/fintech/fintech-complete.md +501 -0
- package/industries/gaming/gaming-complete.md +587 -0
- package/industries/healthcare/healthcare-complete.md +452 -0
- package/low-code/01-standards/low-code-complete.md +944 -0
- package/miniprogram/01-standards/ai-common-mistakes.md +61 -0
- package/miniprogram/01-standards/miniprogram-custom-navbar-capsule.md +77 -0
- package/miniprogram/01-standards/miniprogram-design.md +61 -0
- package/miniprogram/01-standards/miniprogram-standard.md +81 -0
- package/mobile/01-standards/android-material-design.md +70 -0
- package/mobile/01-standards/flutter-complete.md +384 -0
- package/mobile/01-standards/ios-design-hig.md +78 -0
- package/mobile/01-standards/mobile-app-standard.md +85 -0
- package/mobile/01-standards/react-native-complete.md +352 -0
- package/mobile/02-playbooks/mobile-cross-platform-playbook.md +175 -0
- package/mobile/02-playbooks/mobile-performance.md +473 -0
- package/mobile/03-checklists/mobile-release-checklist.md +234 -0
- package/mobile/04-antipatterns/mobile-antipatterns.md +798 -0
- package/mobile/05-cases/case-app-performance.md +500 -0
- package/mobile/05-cases/case-app-startup-optimization.md +218 -0
- package/mobile/06-glossary/mobile-glossary.md +484 -0
- package/observability/01-standards/observability-standards.md +103 -0
- package/observability/02-playbooks/prometheus-grafana-playbook.md +135 -0
- package/observability/02-playbooks/structured-logging-playbook.md +73 -0
- package/observability/03-checklists/observability-checklist.md +54 -0
- package/observability/04-antipatterns/observability-antipatterns.md +106 -0
- package/operations/01-standards/prometheus-monitoring-complete.md +1578 -0
- package/operations/02-playbooks/capacity-planning-playbook.md +620 -0
- package/operations/03-checklists/production-launch-checklist.md +365 -0
- package/operations/04-antipatterns/operations-antipatterns.md +664 -0
- package/operations/05-cases/case-sre-practices.md +581 -0
- package/operations/06-glossary/operations-glossary.md +120 -0
- package/operations/aiops-anomaly-detection.md +758 -0
- package/operations/capacity-planning.md +1061 -0
- package/operations/chaos-engineering.md +659 -0
- package/operations/incident-command-system.md +38 -0
- package/operations/observability-complete.md +442 -0
- package/operations/slo-sli-playbook.md +517 -0
- package/operations/sre-operations-deep-dive.md +39 -0
- package/package.json +8 -0
- package/performance/01-standards/performance-and-scalability.md +80 -0
- package/performance/01-standards/performance-standards.md +156 -0
- package/performance/02-playbooks/query-optimization-playbook.md +103 -0
- package/performance/03-checklists/performance-checklist.md +56 -0
- package/performance/04-antipatterns/performance-antipatterns.md +146 -0
- package/product/01-standards/product-management-complete.md +285 -0
- package/product/02-playbooks/feature-launch-playbook.md +207 -0
- package/product/02-playbooks/user-research-playbook.md +532 -0
- package/product/03-checklists/feature-launch-checklist.md +275 -0
- package/product/04-antipatterns/product-antipatterns.md +355 -0
- package/product/05-cases/case-mvp-to-scale.md +384 -0
- package/product/06-glossary/product-glossary.md +462 -0
- package/product/feature-prioritization-framework.md +40 -0
- package/product/kpi-and-metric-tree.md +37 -0
- package/product/product-discovery-and-prd-deep-dive.md +41 -0
- package/quantum/01-standards/quantum-complete.md +1186 -0
- package/security/01-standards/api-security-complete.md +511 -0
- package/security/01-standards/container-runtime-security.md +574 -0
- package/security/01-standards/data-protection-gdpr.md +543 -0
- package/security/01-standards/owasp-top10-complete.md +1890 -0
- package/security/01-standards/secure-coding-baseline.md +90 -0
- package/security/01-standards/supply-chain-security.md +441 -0
- package/security/01-standards/web-security-checklist.md +108 -0
- package/security/01-standards/zero-trust-architecture.md +521 -0
- package/security/02-playbooks/auth-sso-playbook.md +166 -0
- package/security/02-playbooks/incident-response-security-playbook.md +588 -0
- package/security/02-playbooks/owasp-api-security-playbook.md +129 -0
- package/security/02-playbooks/payment-integration-playbook.md +119 -0
- package/security/02-playbooks/penetration-testing-playbook.md +517 -0
- package/security/03-checklists/security-audit-checklist.md +356 -0
- package/security/04-antipatterns/security-coding-antipatterns.md +580 -0
- package/security/05-cases/case-log4shell-incident.md +537 -0
- package/security/05-cases/case-major-breaches.md +468 -0
- package/security/06-glossary/security-glossary.md +212 -0
- package/security/compliance-automation.md +993 -0
- package/security/container-security.md +680 -0
- package/security/devsecops-complete.md +426 -0
- package/security/sast-dast-sca.md +775 -0
- package/security/secrets-management.md +594 -0
- package/security/security-architecture-deep-dive.md +37 -0
- package/security/threat-modeling-stride-playbook.md +40 -0
- package/seed-templates/auth-system.md +59 -0
- package/seed-templates/blog-content.md +94 -0
- package/seed-templates/dashboard.md +89 -0
- package/seed-templates/docs-site.md +73 -0
- package/seed-templates/e-commerce.md +50 -0
- package/seed-templates/saas-landing.md +92 -0
- package/seed-templates/settings-page.md +51 -0
- package/testing/01-standards/test-strategy-and-layering.md +83 -0
- package/testing/01-standards/testing-strategy-complete.md +422 -0
- package/testing/01-standards/unit-testing-best-practices.md +118 -0
- package/testing/02-playbooks/e2e-testing-playbook.md +988 -0
- package/testing/02-playbooks/testing-strategy-playbook.md +126 -0
- package/testing/03-checklists/test-strategy-checklist.md +208 -0
- package/testing/04-antipatterns/testing-antipatterns.md +718 -0
- package/testing/05-cases/case-testing-transformation.md +300 -0
- package/testing/06-glossary/testing-glossary.md +110 -0
- package/testing/risk-based-test-matrix.md +36 -0
- package/testing/testing-strategy-deep-dive.md +37 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: event-driven-architecture
|
|
3
|
+
title: 事件驱动架构完整指南
|
|
4
|
+
domain: architecture
|
|
5
|
+
category: 01-standards
|
|
6
|
+
difficulty: intermediate
|
|
7
|
+
tags: [agent, architecture, checklist, driven, event, 实战代码示例, 常见陷阱, 最佳实践]
|
|
8
|
+
quality_score: 70
|
|
9
|
+
last_updated: 2026-06-15
|
|
10
|
+
---
|
|
11
|
+
# 事件驱动架构完整指南
|
|
12
|
+
|
|
13
|
+
## 概述
|
|
14
|
+
事件驱动架构(EDA)是一种以事件的产生、检测、消费为核心的软件架构模式。本指南覆盖事件溯源(Event Sourcing)、CQRS、Saga模式、消息代理选型和幂等性设计,帮助团队构建松耦合、高可扩展的分布式系统。
|
|
15
|
+
|
|
16
|
+
## 核心概念
|
|
17
|
+
|
|
18
|
+
### 1. 事件驱动模式分类
|
|
19
|
+
- **事件通知(Event Notification)**: 通知某事发生,接收方自行获取详情
|
|
20
|
+
- **事件携带状态转移(Event-Carried State Transfer)**: 事件中包含完整状态变更
|
|
21
|
+
- **事件溯源(Event Sourcing)**: 将状态变更存储为事件序列,而非当前状态
|
|
22
|
+
- **CQRS(Command Query Responsibility Segregation)**: 读写模型分离
|
|
23
|
+
|
|
24
|
+
### 2. 消息代理对比
|
|
25
|
+
|
|
26
|
+
| 特性 | Kafka | RabbitMQ | NATS | Pulsar | Redis Streams |
|
|
27
|
+
|------|-------|----------|------|--------|--------------|
|
|
28
|
+
| 吞吐量 | 极高(百万/秒) | 中(万级/秒) | 高(十万/秒) | 极高 | 中高 |
|
|
29
|
+
| 延迟 | 低(ms级) | 极低(亚ms) | 极低 | 低 | 极低 |
|
|
30
|
+
| 持久化 | 磁盘(日志) | 内存+磁盘 | 可选 | 分层存储 | AOF/RDB |
|
|
31
|
+
| 消息重放 | 支持 | 不支持 | JetStream支持 | 支持 | 有限支持 |
|
|
32
|
+
| 消费模式 | Pull | Push/Pull | Push/Pull | Push/Pull | Pull |
|
|
33
|
+
| 顺序保证 | 分区内有序 | 队列内有序 | 主题内有序 | 分区内有序 | 流内有序 |
|
|
34
|
+
| 适用场景 | 大数据流/日志 | 任务队列/RPC | 微服务/IoT | 多租户/云原生 | 简单事件流 |
|
|
35
|
+
|
|
36
|
+
### 3. 事件设计原则
|
|
37
|
+
- **不可变(Immutable)**: 事件一旦发布不可修改
|
|
38
|
+
- **自描述(Self-describing)**: 包含足够上下文理解事件含义
|
|
39
|
+
- **有版本(Versioned)**: Schema可演进,向后兼容
|
|
40
|
+
- **有因果(Causal)**: 记录因果关系(correlation_id/causation_id)
|
|
41
|
+
|
|
42
|
+
## 实战代码示例
|
|
43
|
+
|
|
44
|
+
### 事件溯源(Event Sourcing)
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
# 事件基类与聚合根
|
|
48
|
+
from dataclasses import dataclass, field
|
|
49
|
+
from datetime import datetime
|
|
50
|
+
from typing import Any
|
|
51
|
+
import uuid
|
|
52
|
+
import json
|
|
53
|
+
|
|
54
|
+
@dataclass
|
|
55
|
+
class DomainEvent:
|
|
56
|
+
"""领域事件基类"""
|
|
57
|
+
event_id: str = field(default_factory=lambda: str(uuid.uuid4()))
|
|
58
|
+
event_type: str = ""
|
|
59
|
+
aggregate_id: str = ""
|
|
60
|
+
aggregate_type: str = ""
|
|
61
|
+
version: int = 0
|
|
62
|
+
timestamp: datetime = field(default_factory=datetime.utcnow)
|
|
63
|
+
data: dict = field(default_factory=dict)
|
|
64
|
+
metadata: dict = field(default_factory=dict)
|
|
65
|
+
|
|
66
|
+
def to_dict(self) -> dict:
|
|
67
|
+
return {
|
|
68
|
+
"event_id": self.event_id,
|
|
69
|
+
"event_type": self.event_type,
|
|
70
|
+
"aggregate_id": self.aggregate_id,
|
|
71
|
+
"aggregate_type": self.aggregate_type,
|
|
72
|
+
"version": self.version,
|
|
73
|
+
"timestamp": self.timestamp.isoformat(),
|
|
74
|
+
"data": self.data,
|
|
75
|
+
"metadata": self.metadata,
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
# 具体事件
|
|
79
|
+
@dataclass
|
|
80
|
+
class OrderCreated(DomainEvent):
|
|
81
|
+
event_type: str = "OrderCreated"
|
|
82
|
+
aggregate_type: str = "Order"
|
|
83
|
+
|
|
84
|
+
@dataclass
|
|
85
|
+
class OrderItemAdded(DomainEvent):
|
|
86
|
+
event_type: str = "OrderItemAdded"
|
|
87
|
+
aggregate_type: str = "Order"
|
|
88
|
+
|
|
89
|
+
@dataclass
|
|
90
|
+
class OrderConfirmed(DomainEvent):
|
|
91
|
+
event_type: str = "OrderConfirmed"
|
|
92
|
+
aggregate_type: str = "Order"
|
|
93
|
+
|
|
94
|
+
@dataclass
|
|
95
|
+
class OrderCancelled(DomainEvent):
|
|
96
|
+
event_type: str = "OrderCancelled"
|
|
97
|
+
aggregate_type: str = "Order"
|
|
98
|
+
|
|
99
|
+
# 聚合根
|
|
100
|
+
class Order:
|
|
101
|
+
"""订单聚合根 — 事件溯源"""
|
|
102
|
+
|
|
103
|
+
def __init__(self):
|
|
104
|
+
self.id: str = ""
|
|
105
|
+
self.user_id: str = ""
|
|
106
|
+
self.items: list = []
|
|
107
|
+
self.status: str = ""
|
|
108
|
+
self.total: float = 0.0
|
|
109
|
+
self.version: int = 0
|
|
110
|
+
self._pending_events: list[DomainEvent] = []
|
|
111
|
+
|
|
112
|
+
# === 命令处理 ===
|
|
113
|
+
@classmethod
|
|
114
|
+
def create(cls, order_id: str, user_id: str) -> "Order":
|
|
115
|
+
order = cls()
|
|
116
|
+
order._apply(OrderCreated(
|
|
117
|
+
aggregate_id=order_id,
|
|
118
|
+
data={"user_id": user_id},
|
|
119
|
+
))
|
|
120
|
+
return order
|
|
121
|
+
|
|
122
|
+
def add_item(self, product_id: str, quantity: int, price: float):
|
|
123
|
+
if self.status != "created":
|
|
124
|
+
raise ValueError("Cannot add items to a non-created order")
|
|
125
|
+
self._apply(OrderItemAdded(
|
|
126
|
+
aggregate_id=self.id,
|
|
127
|
+
data={"product_id": product_id, "quantity": quantity, "price": price},
|
|
128
|
+
))
|
|
129
|
+
|
|
130
|
+
def confirm(self):
|
|
131
|
+
if self.status != "created":
|
|
132
|
+
raise ValueError("Cannot confirm order in current state")
|
|
133
|
+
if not self.items:
|
|
134
|
+
raise ValueError("Cannot confirm empty order")
|
|
135
|
+
self._apply(OrderConfirmed(
|
|
136
|
+
aggregate_id=self.id,
|
|
137
|
+
data={"total": self.total},
|
|
138
|
+
))
|
|
139
|
+
|
|
140
|
+
def cancel(self, reason: str):
|
|
141
|
+
if self.status in ("cancelled", "shipped"):
|
|
142
|
+
raise ValueError(f"Cannot cancel order in {self.status} state")
|
|
143
|
+
self._apply(OrderCancelled(
|
|
144
|
+
aggregate_id=self.id,
|
|
145
|
+
data={"reason": reason},
|
|
146
|
+
))
|
|
147
|
+
|
|
148
|
+
# === 事件应用 ===
|
|
149
|
+
def _apply(self, event: DomainEvent):
|
|
150
|
+
self.version += 1
|
|
151
|
+
event.version = self.version
|
|
152
|
+
self._on(event)
|
|
153
|
+
self._pending_events.append(event)
|
|
154
|
+
|
|
155
|
+
def _on(self, event: DomainEvent):
|
|
156
|
+
handler = getattr(self, f"_on_{event.event_type}", None)
|
|
157
|
+
if handler:
|
|
158
|
+
handler(event)
|
|
159
|
+
|
|
160
|
+
def _on_OrderCreated(self, event: DomainEvent):
|
|
161
|
+
self.id = event.aggregate_id
|
|
162
|
+
self.user_id = event.data["user_id"]
|
|
163
|
+
self.status = "created"
|
|
164
|
+
|
|
165
|
+
def _on_OrderItemAdded(self, event: DomainEvent):
|
|
166
|
+
self.items.append(event.data)
|
|
167
|
+
self.total += event.data["price"] * event.data["quantity"]
|
|
168
|
+
|
|
169
|
+
def _on_OrderConfirmed(self, event: DomainEvent):
|
|
170
|
+
self.status = "confirmed"
|
|
171
|
+
|
|
172
|
+
def _on_OrderCancelled(self, event: DomainEvent):
|
|
173
|
+
self.status = "cancelled"
|
|
174
|
+
|
|
175
|
+
# === 从事件流重建 ===
|
|
176
|
+
@classmethod
|
|
177
|
+
def from_events(cls, events: list[DomainEvent]) -> "Order":
|
|
178
|
+
order = cls()
|
|
179
|
+
for event in events:
|
|
180
|
+
order.version = event.version
|
|
181
|
+
order._on(event)
|
|
182
|
+
return order
|
|
183
|
+
|
|
184
|
+
def get_pending_events(self) -> list[DomainEvent]:
|
|
185
|
+
events = self._pending_events.copy()
|
|
186
|
+
self._pending_events.clear()
|
|
187
|
+
return events
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### 事件存储
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
# 事件存储(PostgreSQL)
|
|
194
|
+
from sqlalchemy import text
|
|
195
|
+
|
|
196
|
+
class EventStore:
|
|
197
|
+
"""事件存储"""
|
|
198
|
+
|
|
199
|
+
def __init__(self, db):
|
|
200
|
+
self.db = db
|
|
201
|
+
|
|
202
|
+
async def append(self, aggregate_id: str, events: list[DomainEvent], expected_version: int):
|
|
203
|
+
"""追加事件(乐观并发控制)"""
|
|
204
|
+
async with self.db.begin() as conn:
|
|
205
|
+
# 检查版本冲突
|
|
206
|
+
result = await conn.execute(text("""
|
|
207
|
+
SELECT MAX(version) FROM events
|
|
208
|
+
WHERE aggregate_id = :aggregate_id
|
|
209
|
+
"""), {"aggregate_id": aggregate_id})
|
|
210
|
+
current_version = result.scalar() or 0
|
|
211
|
+
|
|
212
|
+
if current_version != expected_version:
|
|
213
|
+
raise ConcurrencyError(
|
|
214
|
+
f"Expected version {expected_version}, but found {current_version}"
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
# 追加事件
|
|
218
|
+
for event in events:
|
|
219
|
+
await conn.execute(text("""
|
|
220
|
+
INSERT INTO events (
|
|
221
|
+
event_id, event_type, aggregate_id, aggregate_type,
|
|
222
|
+
version, timestamp, data, metadata
|
|
223
|
+
) VALUES (
|
|
224
|
+
:event_id, :event_type, :aggregate_id, :aggregate_type,
|
|
225
|
+
:version, :timestamp, :data, :metadata
|
|
226
|
+
)
|
|
227
|
+
"""), {
|
|
228
|
+
"event_id": event.event_id,
|
|
229
|
+
"event_type": event.event_type,
|
|
230
|
+
"aggregate_id": event.aggregate_id,
|
|
231
|
+
"aggregate_type": event.aggregate_type,
|
|
232
|
+
"version": event.version,
|
|
233
|
+
"timestamp": event.timestamp,
|
|
234
|
+
"data": json.dumps(event.data),
|
|
235
|
+
"metadata": json.dumps(event.metadata),
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
async def load(self, aggregate_id: str) -> list[DomainEvent]:
|
|
239
|
+
"""加载聚合的事件流"""
|
|
240
|
+
async with self.db.connect() as conn:
|
|
241
|
+
result = await conn.execute(text("""
|
|
242
|
+
SELECT * FROM events
|
|
243
|
+
WHERE aggregate_id = :aggregate_id
|
|
244
|
+
ORDER BY version
|
|
245
|
+
"""), {"aggregate_id": aggregate_id})
|
|
246
|
+
|
|
247
|
+
events = []
|
|
248
|
+
for row in result:
|
|
249
|
+
events.append(DomainEvent(
|
|
250
|
+
event_id=row.event_id,
|
|
251
|
+
event_type=row.event_type,
|
|
252
|
+
aggregate_id=row.aggregate_id,
|
|
253
|
+
aggregate_type=row.aggregate_type,
|
|
254
|
+
version=row.version,
|
|
255
|
+
timestamp=row.timestamp,
|
|
256
|
+
data=json.loads(row.data),
|
|
257
|
+
metadata=json.loads(row.metadata),
|
|
258
|
+
))
|
|
259
|
+
return events
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### CQRS实现
|
|
263
|
+
|
|
264
|
+
```python
|
|
265
|
+
# CQRS — 命令端
|
|
266
|
+
class OrderCommandHandler:
|
|
267
|
+
"""订单命令处理器(写端)"""
|
|
268
|
+
|
|
269
|
+
def __init__(self, event_store: EventStore, event_bus: EventBus):
|
|
270
|
+
self.event_store = event_store
|
|
271
|
+
self.event_bus = event_bus
|
|
272
|
+
|
|
273
|
+
async def handle_create_order(self, cmd: CreateOrderCommand):
|
|
274
|
+
order = Order.create(cmd.order_id, cmd.user_id)
|
|
275
|
+
for item in cmd.items:
|
|
276
|
+
order.add_item(item.product_id, item.quantity, item.price)
|
|
277
|
+
|
|
278
|
+
events = order.get_pending_events()
|
|
279
|
+
await self.event_store.append(order.id, events, expected_version=0)
|
|
280
|
+
await self.event_bus.publish(events)
|
|
281
|
+
return order.id
|
|
282
|
+
|
|
283
|
+
async def handle_confirm_order(self, cmd: ConfirmOrderCommand):
|
|
284
|
+
events = await self.event_store.load(cmd.order_id)
|
|
285
|
+
order = Order.from_events(events)
|
|
286
|
+
order.confirm()
|
|
287
|
+
|
|
288
|
+
new_events = order.get_pending_events()
|
|
289
|
+
await self.event_store.append(order.id, new_events, expected_version=order.version - len(new_events))
|
|
290
|
+
await self.event_bus.publish(new_events)
|
|
291
|
+
|
|
292
|
+
# CQRS — 查询端(投影/读模型)
|
|
293
|
+
class OrderProjection:
|
|
294
|
+
"""订单查询投影(读端)"""
|
|
295
|
+
|
|
296
|
+
def __init__(self, read_db):
|
|
297
|
+
self.read_db = read_db
|
|
298
|
+
|
|
299
|
+
async def handle_event(self, event: DomainEvent):
|
|
300
|
+
"""处理事件,更新读模型"""
|
|
301
|
+
handler = getattr(self, f"_on_{event.event_type}", None)
|
|
302
|
+
if handler:
|
|
303
|
+
await handler(event)
|
|
304
|
+
|
|
305
|
+
async def _on_OrderCreated(self, event: DomainEvent):
|
|
306
|
+
await self.read_db.execute(text("""
|
|
307
|
+
INSERT INTO order_summary (id, user_id, status, total, item_count, created_at)
|
|
308
|
+
VALUES (:id, :user_id, 'created', 0, 0, :created_at)
|
|
309
|
+
"""), {
|
|
310
|
+
"id": event.aggregate_id,
|
|
311
|
+
"user_id": event.data["user_id"],
|
|
312
|
+
"created_at": event.timestamp,
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
async def _on_OrderItemAdded(self, event: DomainEvent):
|
|
316
|
+
await self.read_db.execute(text("""
|
|
317
|
+
UPDATE order_summary
|
|
318
|
+
SET total = total + :amount,
|
|
319
|
+
item_count = item_count + 1,
|
|
320
|
+
updated_at = :ts
|
|
321
|
+
WHERE id = :id
|
|
322
|
+
"""), {
|
|
323
|
+
"id": event.aggregate_id,
|
|
324
|
+
"amount": event.data["price"] * event.data["quantity"],
|
|
325
|
+
"ts": event.timestamp,
|
|
326
|
+
})
|
|
327
|
+
|
|
328
|
+
async def _on_OrderConfirmed(self, event: DomainEvent):
|
|
329
|
+
await self.read_db.execute(text("""
|
|
330
|
+
UPDATE order_summary SET status = 'confirmed', updated_at = :ts WHERE id = :id
|
|
331
|
+
"""), {"id": event.aggregate_id, "ts": event.timestamp})
|
|
332
|
+
|
|
333
|
+
# 查询服务
|
|
334
|
+
class OrderQueryService:
|
|
335
|
+
"""订单查询服务(读端)"""
|
|
336
|
+
|
|
337
|
+
def __init__(self, read_db):
|
|
338
|
+
self.read_db = read_db
|
|
339
|
+
|
|
340
|
+
async def get_order_summary(self, order_id: str) -> dict:
|
|
341
|
+
result = await self.read_db.execute(text(
|
|
342
|
+
"SELECT * FROM order_summary WHERE id = :id"
|
|
343
|
+
), {"id": order_id})
|
|
344
|
+
return dict(result.fetchone()._mapping)
|
|
345
|
+
|
|
346
|
+
async def list_user_orders(self, user_id: str, page: int = 1, size: int = 20):
|
|
347
|
+
result = await self.read_db.execute(text("""
|
|
348
|
+
SELECT * FROM order_summary
|
|
349
|
+
WHERE user_id = :user_id
|
|
350
|
+
ORDER BY created_at DESC
|
|
351
|
+
LIMIT :size OFFSET :offset
|
|
352
|
+
"""), {"user_id": user_id, "size": size, "offset": (page - 1) * size})
|
|
353
|
+
return [dict(row._mapping) for row in result]
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### Saga模式(编排式)
|
|
357
|
+
|
|
358
|
+
```python
|
|
359
|
+
# Saga编排器 — 管理跨服务事务
|
|
360
|
+
from enum import Enum
|
|
361
|
+
from dataclasses import dataclass
|
|
362
|
+
|
|
363
|
+
class SagaStatus(str, Enum):
|
|
364
|
+
STARTED = "started"
|
|
365
|
+
COMPLETED = "completed"
|
|
366
|
+
COMPENSATING = "compensating"
|
|
367
|
+
FAILED = "failed"
|
|
368
|
+
|
|
369
|
+
@dataclass
|
|
370
|
+
class SagaStep:
|
|
371
|
+
name: str
|
|
372
|
+
action: str # 正向操作
|
|
373
|
+
compensation: str # 补偿操作
|
|
374
|
+
status: str = "pending"
|
|
375
|
+
|
|
376
|
+
class OrderSaga:
|
|
377
|
+
"""订单创建Saga"""
|
|
378
|
+
|
|
379
|
+
def __init__(self, saga_id: str, order_data: dict):
|
|
380
|
+
self.saga_id = saga_id
|
|
381
|
+
self.order_data = order_data
|
|
382
|
+
self.status = SagaStatus.STARTED
|
|
383
|
+
self.completed_steps: list[str] = []
|
|
384
|
+
self.steps = [
|
|
385
|
+
SagaStep("create_order", "order.create", "order.cancel"),
|
|
386
|
+
SagaStep("reserve_inventory", "inventory.reserve", "inventory.release"),
|
|
387
|
+
SagaStep("process_payment", "payment.charge", "payment.refund"),
|
|
388
|
+
SagaStep("confirm_order", "order.confirm", "order.cancel"),
|
|
389
|
+
]
|
|
390
|
+
|
|
391
|
+
class SagaOrchestrator:
|
|
392
|
+
"""Saga编排器"""
|
|
393
|
+
|
|
394
|
+
def __init__(self, event_bus, saga_store):
|
|
395
|
+
self.event_bus = event_bus
|
|
396
|
+
self.saga_store = saga_store
|
|
397
|
+
|
|
398
|
+
async def start_saga(self, saga: OrderSaga):
|
|
399
|
+
"""开始执行Saga"""
|
|
400
|
+
await self.saga_store.save(saga)
|
|
401
|
+
await self._execute_next_step(saga)
|
|
402
|
+
|
|
403
|
+
async def _execute_next_step(self, saga: OrderSaga):
|
|
404
|
+
"""执行下一步"""
|
|
405
|
+
for step in saga.steps:
|
|
406
|
+
if step.status == "pending":
|
|
407
|
+
step.status = "executing"
|
|
408
|
+
await self.saga_store.save(saga)
|
|
409
|
+
await self.event_bus.publish_command(step.action, {
|
|
410
|
+
"saga_id": saga.saga_id,
|
|
411
|
+
"step": step.name,
|
|
412
|
+
**saga.order_data,
|
|
413
|
+
})
|
|
414
|
+
return
|
|
415
|
+
|
|
416
|
+
# 所有步骤完成
|
|
417
|
+
saga.status = SagaStatus.COMPLETED
|
|
418
|
+
await self.saga_store.save(saga)
|
|
419
|
+
|
|
420
|
+
async def handle_step_success(self, saga_id: str, step_name: str):
|
|
421
|
+
"""步骤成功"""
|
|
422
|
+
saga = await self.saga_store.load(saga_id)
|
|
423
|
+
for step in saga.steps:
|
|
424
|
+
if step.name == step_name:
|
|
425
|
+
step.status = "completed"
|
|
426
|
+
saga.completed_steps.append(step_name)
|
|
427
|
+
break
|
|
428
|
+
await self._execute_next_step(saga)
|
|
429
|
+
|
|
430
|
+
async def handle_step_failure(self, saga_id: str, step_name: str, error: str):
|
|
431
|
+
"""步骤失败,开始补偿"""
|
|
432
|
+
saga = await self.saga_store.load(saga_id)
|
|
433
|
+
saga.status = SagaStatus.COMPENSATING
|
|
434
|
+
|
|
435
|
+
# 逆序补偿已完成的步骤
|
|
436
|
+
for step_name in reversed(saga.completed_steps):
|
|
437
|
+
step = next(s for s in saga.steps if s.name == step_name)
|
|
438
|
+
await self.event_bus.publish_command(step.compensation, {
|
|
439
|
+
"saga_id": saga.saga_id,
|
|
440
|
+
**saga.order_data,
|
|
441
|
+
})
|
|
442
|
+
|
|
443
|
+
saga.status = SagaStatus.FAILED
|
|
444
|
+
await self.saga_store.save(saga)
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
### 幂等消费者
|
|
448
|
+
|
|
449
|
+
```python
|
|
450
|
+
# 幂等事件消费
|
|
451
|
+
class IdempotentConsumer:
|
|
452
|
+
"""幂等消费者 — 防止重复处理"""
|
|
453
|
+
|
|
454
|
+
def __init__(self, redis, handler):
|
|
455
|
+
self.redis = redis
|
|
456
|
+
self.handler = handler
|
|
457
|
+
self.ttl = 7 * 24 * 3600 # 7天去重窗口
|
|
458
|
+
|
|
459
|
+
async def process(self, event: DomainEvent) -> bool:
|
|
460
|
+
"""处理事件(幂等)"""
|
|
461
|
+
dedup_key = f"processed:{event.event_id}"
|
|
462
|
+
|
|
463
|
+
# 原子检查并标记
|
|
464
|
+
was_set = await self.redis.set(dedup_key, "1", nx=True, ex=self.ttl)
|
|
465
|
+
if not was_set:
|
|
466
|
+
# 已处理过,跳过
|
|
467
|
+
logger.info("Duplicate event skipped", event_id=event.event_id)
|
|
468
|
+
return False
|
|
469
|
+
|
|
470
|
+
try:
|
|
471
|
+
await self.handler(event)
|
|
472
|
+
return True
|
|
473
|
+
except Exception:
|
|
474
|
+
# 处理失败,移除标记以允许重试
|
|
475
|
+
await self.redis.delete(dedup_key)
|
|
476
|
+
raise
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
## 最佳实践
|
|
480
|
+
|
|
481
|
+
### 1. 事件设计
|
|
482
|
+
- 事件名使用过去式(OrderCreated,不是CreateOrder)
|
|
483
|
+
- 包含correlation_id追踪因果链
|
|
484
|
+
- 事件Schema有版本号,支持演进
|
|
485
|
+
- 避免过大的事件(考虑事件携带vs事件通知)
|
|
486
|
+
|
|
487
|
+
### 2. 事件存储
|
|
488
|
+
- 使用追加写入(append-only)
|
|
489
|
+
- 乐观并发控制(version check)
|
|
490
|
+
- 快照机制优化长事件流(每100个事件做快照)
|
|
491
|
+
- 事件存储需要备份和归档策略
|
|
492
|
+
|
|
493
|
+
### 3. CQRS部署
|
|
494
|
+
- 接受读模型的最终一致性
|
|
495
|
+
- 投影失败时可以从事件流重建
|
|
496
|
+
- 读模型可以有多个(针对不同查询场景)
|
|
497
|
+
- 写模型和读模型可以用不同数据库
|
|
498
|
+
|
|
499
|
+
### 4. Saga最佳实践
|
|
500
|
+
- 每个步骤的补偿操作必须是幂等的
|
|
501
|
+
- 记录Saga状态便于故障恢复
|
|
502
|
+
- 设置超时机制处理步骤无响应
|
|
503
|
+
- 补偿操作也可能失败,需要人工介入机制
|
|
504
|
+
|
|
505
|
+
### 5. 幂等性
|
|
506
|
+
- 消费者必须实现幂等(消息可能重复投递)
|
|
507
|
+
- 使用事件ID作为去重键
|
|
508
|
+
- 去重窗口至少覆盖消息保留时间
|
|
509
|
+
- 数据库操作使用UPSERT而非INSERT
|
|
510
|
+
|
|
511
|
+
## 常见陷阱
|
|
512
|
+
|
|
513
|
+
### 陷阱1: 事件中包含命令
|
|
514
|
+
```python
|
|
515
|
+
# 错误: 事件不应该指示下游做什么
|
|
516
|
+
event = {"type": "OrderCreated", "data": {"send_email": True}}
|
|
517
|
+
|
|
518
|
+
# 正确: 事件只描述发生了什么,消费者自行决策
|
|
519
|
+
event = {"type": "OrderCreated", "data": {"order_id": "123", "user_id": "456"}}
|
|
520
|
+
# 邮件服务订阅OrderCreated事件,自行决定是否发邮件
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
### 陷阱2: 双重写入问题
|
|
524
|
+
```python
|
|
525
|
+
# 错误: 先写数据库再发事件,可能不一致
|
|
526
|
+
await db.save(order)
|
|
527
|
+
await event_bus.publish(OrderCreated(...)) # 如果这里失败?数据库有,事件没有
|
|
528
|
+
|
|
529
|
+
# 正确: Transactional Outbox模式
|
|
530
|
+
async with db.begin() as tx:
|
|
531
|
+
await tx.save(order)
|
|
532
|
+
await tx.save_outbox_event(OrderCreated(...)) # 同一事务
|
|
533
|
+
# 后台进程轮询outbox表发送到消息代理
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
### 陷阱3: 事件顺序依赖
|
|
537
|
+
```python
|
|
538
|
+
# 错误: 假设事件总是按时间顺序到达
|
|
539
|
+
# 分布式系统中事件可能乱序
|
|
540
|
+
|
|
541
|
+
# 正确: 使用版本号检测乱序
|
|
542
|
+
async def handle_event(event):
|
|
543
|
+
current = await read_db.get_version(event.aggregate_id)
|
|
544
|
+
if event.version <= current:
|
|
545
|
+
return # 旧事件,跳过
|
|
546
|
+
if event.version > current + 1:
|
|
547
|
+
# 丢失了中间事件,请求重放
|
|
548
|
+
await request_replay(event.aggregate_id, current + 1)
|
|
549
|
+
return
|
|
550
|
+
await process(event)
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
### 陷阱4: 投影重建成本过高
|
|
554
|
+
```python
|
|
555
|
+
# 错误: 百万级事件流每次都从头重建投影
|
|
556
|
+
# 正确: 使用快照 + 增量重建
|
|
557
|
+
async def rebuild_projection(aggregate_id: str):
|
|
558
|
+
snapshot = await snapshot_store.get_latest(aggregate_id)
|
|
559
|
+
if snapshot:
|
|
560
|
+
state = snapshot.state
|
|
561
|
+
events = await event_store.load_after(aggregate_id, snapshot.version)
|
|
562
|
+
else:
|
|
563
|
+
state = initial_state()
|
|
564
|
+
events = await event_store.load(aggregate_id)
|
|
565
|
+
|
|
566
|
+
for event in events:
|
|
567
|
+
state = apply_event(state, event)
|
|
568
|
+
return state
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
## Agent Checklist
|
|
572
|
+
|
|
573
|
+
### 事件设计
|
|
574
|
+
- [ ] 事件使用过去式命名
|
|
575
|
+
- [ ] 包含event_id/correlation_id/timestamp
|
|
576
|
+
- [ ] 事件Schema有版本管理
|
|
577
|
+
- [ ] 事件大小合理(不过大)
|
|
578
|
+
|
|
579
|
+
### 事件存储
|
|
580
|
+
- [ ] 追加写入(不可修改)
|
|
581
|
+
- [ ] 乐观并发控制
|
|
582
|
+
- [ ] 快照机制(长事件流)
|
|
583
|
+
- [ ] 备份和归档策略
|
|
584
|
+
|
|
585
|
+
### CQRS
|
|
586
|
+
- [ ] 读写模型分离
|
|
587
|
+
- [ ] 投影可从事件流重建
|
|
588
|
+
- [ ] 最终一致性可接受
|
|
589
|
+
- [ ] 多个读模型按查询场景优化
|
|
590
|
+
|
|
591
|
+
### 可靠性
|
|
592
|
+
- [ ] 消费者幂等处理
|
|
593
|
+
- [ ] 死信队列处理失败消息
|
|
594
|
+
- [ ] Saga补偿操作已实现
|
|
595
|
+
- [ ] 分布式追踪贯穿事件链
|