关于 cloud-native-patterns
面向开发者的云原生应用设计参考,涵盖十二要素原则、韧性模式、服务通信、健康检查和容器实践。
主要功能
- 01十二要素原则
- 02熔断、重试与舱壁模式
- 03同步与异步通信
- 04健康与就绪检查
- 05容器实践
适用场景
- 01审视云原生应用设计
- 02为服务调用增加韧性
- 03规划健康端点与容器构建
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dralgorhythm/ claude-agentic-framework --skill cloud-native-patterns安装后在 Claude Code 中输入/cloud-native-patterns确认已加载。
详细介绍
云原生模式
十二要素应用
- 代码库:一个代码库,多次部署
- 依赖项:显式声明并隔离
- 配置:存储在环境中
- 后端服务:视为附加资源
- 构建、发布、运行:严格分离各阶段
- 进程:以无状态进程运行
- 端口绑定:通过端口导出服务
- 并发:通过进程模型横向扩展
- 可处置性:快速启动并优雅关闭
- 开发/生产环境一致性:保持环境相似
- 日志:视为事件流
- 管理进程:作为一次性进程运行
弹性模式
熔断器
快速失败,防止故障级联。
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;
}
}
}
带退避的重试
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
}
}
}
舱壁
隔离故障,防止影响整个系统。
服务通信
同步
- REST/HTTP
- gRPC
异步
- 消息队列(RabbitMQ、SQS)
- 事件流(Kafka)
健康检查
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' });
}
});
容器最佳实践
- 每个容器运行一个进程
- 使用多阶段构建
- 以非 root 用户运行
- 使用健康检查
- 保持镜像精简
评论
支持 Markdown