介绍
spring-rs是一个Rust编写的应用框架,强调约定大于配置,类似于java生态的SpringBoot。spring-rs提供了易于扩展的插件系统,用于整合Rust社区的优秀项目,例如axum、sqlx、sea-orm等。
相比于java生态的SpringBoot,spring-rs有更高的性能和更低的内存占用,让你彻底摆脱臃肿的JVM,轻装上阵。
特点
- ⚡️ 高性能: 得益于出色的Rust语言,spring-rs拥有与c/c++媲美的极致性能
- 🛡️ 高安全性: 相比C/C++,spring-rs使用的Rust语言提供了内存安全和线程安全的能力
- 🔨 轻量级: spring-rs的核心代码不超过5000行,打包的release版二进制文件也非常小巧
- 🔧 容易使用: spring-rs提供了清晰明了的API和可选的过程宏来简化开发
- 🔌 高可扩展性: spring-rs采用高扩展性的插件模式,用户可以自定义插件扩展程序功能
- ⚙️ 高可配置性: spring-rs用toml配置应用和插件,提升应用灵活性
简单的例子
web
use spring::{auto_config, App};
use spring_sqlx::{
sqlx::{self, Row},
ConnectPool, SqlxPlugin
};
use spring_web::{get, route};
use spring_web::{
error::Result, extractor::{Path, Component}, handler::TypeRouter, axum::response::IntoResponse, Router,
WebConfigurator, WebPlugin,
};
#[auto_config(WebConfigurator)]
#[tokio::main]
async fn main() {
App::new()
.add_plugin(SqlxPlugin)
.add_plugin(WebPlugin)
.run()
.await
}
#[get("/")]
async fn hello_world() -> impl IntoResponse {
"hello world"
}
#[route("/hello/{name}", method = "GET", method = "POST")]
async fn hello(Path(name): Path<String>) -> impl IntoResponse {
format!("hello {name}")
}
#[get("/version")]
async fn sqlx_request_handler(Component(pool): Component<ConnectPool>) -> Result<String> {
let version = sqlx::query("select version() as version")
.fetch_one(&pool)
.await
.context("sqlx query failed")?
.get("version");
Ok(version)
}
任务调度
use anyhow::Context;
use spring::{auto_config, App};
use spring_job::{cron, fix_delay, fix_rate};
use spring_job::{extractor::Component, JobConfigurator, JobPlugin};
use spring_sqlx::{
sqlx::{self, Row},
ConnectPool, SqlxPlugin,
};
use std::time::{Duration, SystemTime};
#[auto_config(JobConfigurator)]
#[tokio::main]
async fn main() {
App::new()
.add_plugin(JobPlugin)
.add_plugin(SqlxPlugin)
.run()
.await;
tokio::time::sleep(Duration::from_secs(100)).await;
}
#[cron("1/10 * * * * *")]
async fn cron_job(Component(db): Component<ConnectPool>) {
let time: String = sqlx::query("select TO_CHAR(now(),'YYYY-MM-DD HH24:MI:SS') as time")
.fetch_one(&db)
.await
.context("query failed")
.unwrap()
.get("time");
println!("cron scheduled: {:?}", time)
}
#[fix_delay(5)]
async fn fix_delay_job() {
let now = SystemTime::now();
let datetime: sqlx::types::chrono::DateTime<sqlx::types::chrono::Local> = now.into();
let formatted_time = datetime.format("%Y-%m-%d %H:%M:%S");
println!("fix delay scheduled: {}", formatted_time)
}
#[fix_rate(5)]
async fn fix_rate_job() {
tokio::time::sleep(Duration::from_secs(10)).await;
let now = SystemTime::now();
let datetime: sqlx::types::chrono::DateTime<sqlx::types::chrono::Local> = now.into();
let formatted_time = datetime.format("%Y-%m-%d %H:%M:%S");
println!("fix rate scheduled: {}", formatted_time)
}
支持的插件
| 插件 | Crate | 集成组件 | 说明 |
|---|---|---|---|
spring-web | axum | Web 框架,基于 axum | |
spring-sqlx | sqlx | 异步 SQL 访问 | |
spring-postgres | rust-postgres | PostgreSQL 客户端集成 | |
spring-sea-orm | sea-orm | ORM 支持 | |
spring-redis | redis | Redis 集成 | |
spring-mail | lettre | 邮件发送 | |
spring-job | tokio-cron-scheduler | 定时任务 / Cron | |
spring-stream | sea-streamer | 消息流处理(Redis Stream / Kafka) | |
spring-opentelemetry | opentelemetry | 日志 / 指标 / 链路追踪 | |
spring-grpc | tonic | gRPC 服务与调用 | |
spring-opendal | opendal | 统一对象存储 / 数据访问 |
生态
项目示例
请作者喝杯茶
交流群
贡献
也欢迎社区的大牛贡献自己的插件。 Contributing →
帮助
点击这里可以查看spring-rs使用过程中遇到的常见问题 Help →