安装与使用 Ecat

从 cargo install 到运行中的 HTTP 与 gRPC 服务,再看 ORM、19 个数据后端与已知限制。示例与仓库 examples 使用同一套 API。

让服务跑起来

四个具体步骤。右栏保留完整命令序列。

准备 Rust 与 protoc

Ecat 使用 edition 2024,需要 Rust 1.85 或更新版本。CLI 和生成的代码还需要 protoc。

terminal
# Ubuntu / Debian
sudo apt install protobuf-compiler

# macOS
brew install protobuf

# verify
rustc --version
protoc --version

安装 CLI

从 crates.io 安装。二进制随包携带宠物资源与 ASSETS-LICENSE 说明。

terminal
cargo install ecat-cli
ecat --version

创建服务

创建项目,添加 proto 文件,再生成客户端与服务端代码。生成的 build.rs 已接好 tonic-build。

terminal
ecat new helloworld
cd helloworld
ecat proto add proto/service.proto
ecat proto client proto/service.proto
ecat proto server proto/service.proto -t internal/service

运行

默认 HTTP 服务监听 0.0.0.0:8000。使用 --watch 在 src/ 变更时重启。

terminal
ecat run
ecat run --watch
用 Docker、Kubernetes 或 Helm 部署

模板以仓库根目录作为构建上下文。

terminal
docker build -t ecat-app -f ecat-deploy/Dockerfile .
kubectl apply -f ecat-deploy/k8s-deployment.yaml
helm install my-ecat ecat-deploy/helm/

使用各个部件

聚合 crate 按 feature 重新导出模块;应用、中间件、数据、ORM、CLI 与参考表都在下面。

应用生命周期

AppBuilder 组合服务器、生命周期钩子和信号处理。run() 阻塞到 SIGTERM 或 SIGINT。

  • HTTP 与 gRPC 服务监听不同端口。
  • on_start 与 on_stop 钩子是异步的。
  • 聚合 crate 让导入路径更短。
  • features:http / grpc / middleware / auth / client / events / metrics / tracing / circuit-breaker / consul / remote / redis / orm / mssql,默认 http + grpc。
main.rs
use ecat::App;
use ecat_transport_http::HttpServer;
use ecat_transport_grpc::GrpcServer;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let app = App::builder()
        .name("my-service")
        .version("v1.0.0")
        .server(HttpServer::new("0.0.0.0:8000"))
        .server(GrpcServer::new("0.0.0.0:9000"))
        .on_start(|| async {
            tracing::info!("service started");
            Ok(())
        })
        .on_stop(|| async {
            tracing::info!("service stopped");
            Ok(())
        })
        .build()?;

    app.run().await?; // 阻塞到 SIGTERM 或 SIGINT
    Ok(())
}