domain-cloud-native
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCloud-Native Domain
云原生领域
Layer 3: Domain Constraints
第3层:领域约束
Domain Constraints → Design Implications
领域约束 → 设计影响
| Domain Rule | Design Constraint | Rust Implication |
|---|---|---|
| 12-Factor | Config from env | Environment-based config |
| Observability | Metrics + traces | tracing + opentelemetry |
| Health checks | Liveness/readiness | Dedicated endpoints |
| Graceful shutdown | Clean termination | Signal handling |
| Horizontal scale | Stateless design | No local state |
| Container-friendly | Small binaries | Release optimization |
| 领域规则 | 设计约束 | Rust 实践 |
|---|---|---|
| 12-Factor | 从环境变量读取配置 | 基于环境的配置 |
| 可观测性 | 指标 + 追踪 | tracing + opentelemetry |
| 健康检查 | 存活/就绪检查 | 专用端点 |
| 优雅停机 | 干净终止 | 信号处理 |
| 水平扩展 | 无状态设计 | 无本地状态 |
| 容器友好 | 小型二进制文件 | 发布优化 |
Critical Constraints
关键约束
Stateless Design
无状态设计
RULE: No local persistent state
WHY: Pods can be killed/rescheduled anytime
RUST: External state (Redis, DB), no static mutRULE: No local persistent state
WHY: Pods can be killed/rescheduled anytime
RUST: External state (Redis, DB), no static mutGraceful Shutdown
优雅停机
RULE: Handle SIGTERM, drain connections
WHY: Zero-downtime deployments
RUST: tokio::signal + graceful shutdownRULE: Handle SIGTERM, drain connections
WHY: Zero-downtime deployments
RUST: tokio::signal + graceful shutdownObservability
可观测性
RULE: Every request must be traceable
WHY: Debugging distributed systems
RUST: tracing spans, opentelemetry exportRULE: Every request must be traceable
WHY: Debugging distributed systems
RUST: tracing spans, opentelemetry exportTrace Down ↓
向下追溯 ↓
From constraints to design (Layer 2):
"Need distributed tracing"
↓ m12-lifecycle: Span lifecycle
↓ tracing + opentelemetry
"Need graceful shutdown"
↓ m07-concurrency: Signal handling
↓ m12-lifecycle: Connection draining
"Need health checks"
↓ domain-web: HTTP endpoints
↓ m06-error-handling: Health status从约束到设计(第2层):
"Need distributed tracing"
↓ m12-lifecycle: Span lifecycle
↓ tracing + opentelemetry
"Need graceful shutdown"
↓ m07-concurrency: Signal handling
↓ m12-lifecycle: Connection draining
"Need health checks"
↓ domain-web: HTTP endpoints
↓ m06-error-handling: Health statusKey Crates
关键Crates
| Purpose | Crate |
|---|---|
| gRPC | tonic |
| Kubernetes | kube, kube-runtime |
| Docker | bollard |
| Tracing | tracing, opentelemetry |
| Metrics | prometheus, metrics |
| Config | config, figment |
| Health | HTTP endpoints |
| 用途 | Crate |
|---|---|
| gRPC | tonic |
| Kubernetes | kube, kube-runtime |
| Docker | bollard |
| 追踪 | tracing, opentelemetry |
| 指标 | prometheus, metrics |
| 配置 | config, figment |
| 健康检查 | HTTP endpoints |
Design Patterns
设计模式
| Pattern | Purpose | Implementation |
|---|---|---|
| gRPC services | Service mesh | tonic + tower |
| K8s operators | Custom resources | kube-runtime Controller |
| Observability | Debugging | tracing + OTEL |
| Health checks | Orchestration | |
| Config | 12-factor | Env vars + secrets |
| 模式 | 用途 | 实现方式 |
|---|---|---|
| gRPC服务 | 服务网格 | tonic + tower |
| K8s Operator | 自定义资源 | kube-runtime Controller |
| 可观测性 | 调试 | tracing + OTEL |
| 健康检查 | 编排 | |
| 配置 | 12要素 | 环境变量 + 密钥 |
Code Pattern: Graceful Shutdown
代码模式:优雅停机
rust
use tokio::signal;
async fn run_server() -> anyhow::Result<()> {
let app = Router::new()
.route("/health", get(health))
.route("/ready", get(ready));
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await?;
Ok(())
}
async fn shutdown_signal() {
signal::ctrl_c().await.expect("failed to listen for ctrl+c");
tracing::info!("shutdown signal received");
}rust
use tokio::signal;
async fn run_server() -> anyhow::Result<()> {
let app = Router::new()
.route("/health", get(health))
.route("/ready", get(ready));
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await?;
Ok(())
}
async fn shutdown_signal() {
signal::ctrl_c().await.expect("failed to listen for ctrl+c");
tracing::info!("shutdown signal received");
}Health Check Pattern
健康检查模式
rust
async fn health() -> StatusCode {
StatusCode::OK
}
async fn ready(State(db): State<Arc<DbPool>>) -> StatusCode {
match db.ping().await {
Ok(_) => StatusCode::OK,
Err(_) => StatusCode::SERVICE_UNAVAILABLE,
}
}rust
async fn health() -> StatusCode {
StatusCode::OK
}
async fn ready(State(db): State<Arc<DbPool>>) -> StatusCode {
match db.ping().await {
Ok(_) => StatusCode::OK,
Err(_) => StatusCode::SERVICE_UNAVAILABLE,
}
}Common Mistakes
常见错误
| Mistake | Domain Violation | Fix |
|---|---|---|
| Local file state | Not stateless | External storage |
| No SIGTERM handling | Hard kills | Graceful shutdown |
| No tracing | Can't debug | tracing spans |
| Static config | Not 12-factor | Env vars |
| 错误 | 领域违规 | 修复方案 |
|---|---|---|
| 本地文件状态 | 不符合无状态要求 | 使用外部存储 |
| 未处理SIGTERM | 强制终止 | 实现优雅停机 |
| 无追踪功能 | 无法调试 | 添加tracing span |
| 静态配置 | 不符合12要素 | 使用环境变量 |
Trace to Layer 1
追溯到第1层
| Constraint | Layer 2 Pattern | Layer 1 Implementation |
|---|---|---|
| Stateless | External state | Arc<Client> for external |
| Graceful shutdown | Signal handling | tokio::signal |
| Tracing | Span lifecycle | tracing + OTEL |
| Health checks | HTTP endpoints | Dedicated routes |
| 约束 | 第2层模式 | 第1层实现 |
|---|---|---|
| 无状态 | 外部状态 | 使用Arc<Client>访问外部服务 |
| 优雅停机 | 信号处理 | tokio::signal |
| 追踪 | Span生命周期 | tracing + OTEL |
| 健康检查 | HTTP端点 | 专用路由 |
Related Skills
相关技能
| When | See |
|---|---|
| Async patterns | m07-concurrency |
| HTTP endpoints | domain-web |
| Error handling | m13-domain-error |
| Resource lifecycle | m12-lifecycle |
| 场景 | 参考内容 |
|---|---|
| 异步模式 | m07-concurrency |
| HTTP端点 | domain-web |
| 错误处理 | m13-domain-error |
| 资源生命周期 | m12-lifecycle |