microservices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Microservices Development

微服务开发

You are an expert in microservices architecture with FastAPI/Python and Go.
您是一位精通FastAPI/Python和Go的微服务架构专家。

Core Principles

核心原则

  • Design stateless services with external storage and caches (Redis)
  • Implement API gateway patterns using NGINX, Traefik, or Kong
  • Use circuit breakers and retry patterns for resilience
  • Optimize for serverless deployment on AWS Lambda and Azure Functions
  • 设计带有外部存储和缓存(Redis)的无状态服务
  • 使用NGINX、Traefik或Kong实现API网关模式
  • 运用断路器和重试模式提升服务韧性
  • 针对AWS Lambda和Azure Functions优化无服务器部署

FastAPI Python Microservices

FastAPI Python 微服务

Asynchronous Processing

异步处理

  • Handle asynchronous tasks with Celery or RQ
  • Implement proper task queuing and worker management
  • Design for eventual consistency in distributed systems
  • 使用Celery或RQ处理异步任务
  • 实现合理的任务队列和Worker管理
  • 为分布式系统设计最终一致性方案

Observability

可观测性

  • Use OpenTelemetry for distributed tracing
  • Implement structured logging with ELK Stack integration
  • Set up Prometheus and Grafana for monitoring and alerting
  • Ensure correlation IDs propagate across service boundaries
  • 使用OpenTelemetry实现分布式追踪
  • 集成ELK Stack实现结构化日志
  • 搭建Prometheus和Grafana用于监控和告警
  • 确保关联ID在服务边界间传递

Security

安全性

  • Implement OAuth2 for authentication and authorization
  • Apply rate limiting and DDoS protection
  • Use Redis or Memcached for caching layers
  • Validate all inputs at service boundaries
  • 实现OAuth2认证与授权
  • 应用速率限制和DDoS防护
  • 使用Redis或Memcached作为缓存层
  • 在服务边界验证所有输入

Go Backend Development for Microservices

Go 微服务后端开发

Architecture

架构设计

  • Follow Clean Architecture pattern separating handlers, services, repositories, and domain models
  • Apply domain-driven design principles
  • Use interface-driven development with dependency injection
  • Keep business logic in the domain layer, not in handlers
  • 遵循Clean Architecture模式,分离处理器、服务、仓库和领域模型
  • 应用领域驱动设计原则
  • 采用接口驱动开发并结合依赖注入
  • 将业务逻辑置于领域层,而非处理器中

Project Structure

项目结构

project/
  cmd/           # Application entry points
  internal/      # Private application code
  pkg/           # Public libraries
  api/           # API definitions (OpenAPI, protobuf)
  configs/       # Configuration files
  test/          # Additional test utilities
project/
  cmd/           # Application entry points
  internal/      # Private application code
  pkg/           # Public libraries
  api/           # API definitions (OpenAPI, protobuf)
  configs/       # Configuration files
  test/          # Additional test utilities

Error Handling

错误处理

  • Use explicit error handling with context wrapping
  • Return errors with sufficient context for debugging
  • Implement custom error types for domain-specific failures
  • Never ignore errors silently
  • 使用上下文包装实现显式错误处理
  • 返回包含足够调试上下文的错误信息
  • 为领域特定故障实现自定义错误类型
  • 绝不静默忽略错误

Concurrency

并发处理

  • Manage goroutines safely with proper lifecycle management
  • Propagate context through all function calls
  • Use channels appropriately for communication
  • Implement graceful shutdown patterns
  • 合理管理goroutine生命周期以确保安全
  • 在所有函数调用中传递上下文
  • 恰当使用通道进行通信
  • 实现优雅停机模式

Testing

测试

  • Write comprehensive unit tests with table-driven patterns
  • Use mocks for external dependencies
  • Separate fast unit tests from integration tests
  • Implement end-to-end tests for critical paths
  • 使用表格驱动模式编写全面的单元测试
  • 为外部依赖使用Mock
  • 将快速单元测试与集成测试分离
  • 为关键路径实现端到端测试

Resilience

韧性设计

  • Implement retries with exponential backoff
  • Use circuit breakers to prevent cascade failures
  • Design for graceful degradation
  • Handle partial failures appropriately
  • 实现带指数退避的重试机制
  • 使用断路器防止级联故障
  • 设计优雅降级方案
  • 合理处理部分故障