cloud-native-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCloud-Native Patterns
云原生架构模式
Twelve-Factor App
十二要素应用
- Codebase: One codebase, many deploys
- Dependencies: Explicitly declare and isolate
- Config: Store in environment
- Backing Services: Treat as attached resources
- Build, Release, Run: Strictly separate stages
- Processes: Execute as stateless processes
- Port Binding: Export services via port
- Concurrency: Scale out via process model
- Disposability: Fast startup and graceful shutdown
- Dev/Prod Parity: Keep environments similar
- Logs: Treat as event streams
- Admin Processes: Run as one-off processes
- 代码库:一个代码库,多份部署
- 依赖:显式声明并隔离依赖
- 配置:存储在环境变量中
- 后端服务:视为附加资源
- 构建、发布、运行:严格分离各阶段
- 进程:以无状态进程执行
- 端口绑定:通过端口对外提供服务
- 并发:基于进程模型横向扩展
- 可处置性:快速启动与优雅关闭
- 开发/生产环境一致性:保持环境尽可能相似
- 日志:视为事件流
- 管理进程:作为一次性进程运行
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用户运行
- 配置健康检查
- 保持镜像体积轻量化