spring rs
0.3.0
英语| |
Spring-RS是一个应用程序框架,强调了由Java的Springboot启发而不是配置而不是配置。 Spring-RS提供了一个易于扩展的插件系统,用于在Rust社区中集成出色的项目,例如Axum,SQLX,Sea-orm,等。
与Java中的Springboot相比,Spring-RS具有更高的性能和较低的内存使用情况,使您可以完全摆脱肿的JVM和Travel Light。
网络
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 ,
} ;
use anyhow :: Context ;
# [ 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 )
} spring-web :基于axum spring-sqlx :与sqlx集成spring-postgres : rust-postgres集成spring-sea-orm与sea-orm集成spring-redis :与redis集成spring-mail :与lettre集成spring-job :与tokio-cron-scheduler集成spring-stream :整合sea-streamer以实施消息处理,例如redis-tream和kafka spring-opentelemetry :与opentelemetry集成以实现日志记录,指标,跟踪的完整可观察性spring-tarpc :集成tarpc以实现RPC调用spring-sqlx-migration-pluginspring-opendal更多>>
我们还欢迎社区专家贡献自己的插件。贡献→
单击此处查看使用spring-rs帮助时遇到的常见问题→