cloud-native-patterns

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cloud-Native Patterns

云原生架构模式

Twelve-Factor App

十二要素应用

  1. Codebase: One codebase, many deploys
  2. Dependencies: Explicitly declare and isolate
  3. Config: Store in environment
  4. Backing Services: Treat as attached resources
  5. Build, Release, Run: Strictly separate stages
  6. Processes: Execute as stateless processes
  7. Port Binding: Export services via port
  8. Concurrency: Scale out via process model
  9. Disposability: Fast startup and graceful shutdown
  10. Dev/Prod Parity: Keep environments similar
  11. Logs: Treat as event streams
  12. Admin Processes: Run as one-off processes
  1. 代码库:一个代码库,多份部署
  2. 依赖:显式声明并隔离依赖
  3. 配置:存储在环境变量中
  4. 后端服务:视为附加资源
  5. 构建、发布、运行:严格分离各阶段
  6. 进程:以无状态进程执行
  7. 端口绑定:通过端口对外提供服务
  8. 并发:基于进程模型横向扩展
  9. 可处置性:快速启动与优雅关闭
  10. 开发/生产环境一致性:保持环境尽可能相似
  11. 日志:视为事件流
  12. 管理进程:作为一次性进程运行

Resilience Patterns

弹性模式

Circuit Breaker

断路器

Prevent cascading failures by failing fast.
typescript
class CircuitBreaker {
  private failures = 0;
  private lastFailure?: Date;

  async call<T>(fn: () => Promise<T>): Promise<T> {
    if (this.isOpen()) {
      throw new Error('Circuit is open');
    }
    try {
      const result = await fn();
      this.reset();
      return result;
    } catch (error) {
      this.recordFailure();
      throw error;
    }
  }
}
通过快速失败防止级联故障。
typescript
class CircuitBreaker {
  private failures = 0;
  private lastFailure?: Date;

  async call<T>(fn: () => Promise<T>): Promise<T> {
    if (this.isOpen()) {
      throw new Error('Circuit is open');
    }
    try {
      const result = await fn();
      this.reset();
      return result;
    } catch (error) {
      this.recordFailure();
      throw error;
    }
  }
}

Retry with Backoff

带退避的重试

typescript
async function withRetry<T>(
  fn: () => Promise<T>,
  maxRetries = 3
): Promise<T> {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await fn();
    } catch (error) {
      if (i === maxRetries - 1) throw error;
      await sleep(Math.pow(2, i) * 1000); // Exponential backoff
    }
  }
}
typescript
async function withRetry<T>(
  fn: () => Promise<T>,
  maxRetries = 3
): Promise<T> {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await fn();
    } catch (error) {
      if (i === maxRetries - 1) throw error;
      await sleep(Math.pow(2, i) * 1000); // Exponential backoff
    }
  }
}

Bulkhead

舱壁模式

Isolate failures to prevent system-wide impact.
隔离故障,防止影响整个系统。

Service Communication

服务通信

Synchronous

同步通信

  • REST/HTTP
  • gRPC
  • REST/HTTP
  • gRPC

Asynchronous

异步通信

  • Message queues (RabbitMQ, SQS)
  • Event streaming (Kafka)
  • 消息队列(RabbitMQ、SQS)
  • 事件流(Kafka)

Health Checks

健康检查

typescript
app.get('/health', (req, res) => {
  res.json({ status: 'healthy' });
});

app.get('/ready', async (req, res) => {
  const dbHealthy = await checkDatabase();
  const cacheHealthy = await checkCache();

  if (dbHealthy && cacheHealthy) {
    res.json({ status: 'ready' });
  } else {
    res.status(503).json({ status: 'not ready' });
  }
});
typescript
app.get('/health', (req, res) => {
  res.json({ status: 'healthy' });
});

app.get('/ready', async (req, res) => {
  const dbHealthy = await checkDatabase();
  const cacheHealthy = await checkCache();

  if (dbHealthy && cacheHealthy) {
    res.json({ status: 'ready' });
  } else {
    res.status(503).json({ status: 'not ready' });
  }
});

Container Best Practices

容器最佳实践

  • One process per container
  • Use multi-stage builds
  • Run as non-root user
  • Use health checks
  • Keep images small
  • 每个容器运行一个进程
  • 使用多阶段构建
  • 以非root用户运行
  • 配置健康检查
  • 保持镜像体积轻量化