spring rs
0.3.0
英語| |
Spring-RSは、JavaのSpringbootに触発された、構成に関する慣習を強調するアプリケーションフレームワークです。 Spring-RSは、Axum、SQLX、Sea-Ormなど、Rustコミュニティに優れたプロジェクトを統合するための簡単に拡張可能なプラグインシステムを提供します。
JavaのSpringbootと比較して、Spring-RSはパフォーマンスが高く、メモリの使用量が少なくなるため、肥大化したJVMとトラベルライトを完全に取り除くことができます。
ウェブ
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 :redis-streamやkafkaなどのメッセージ処理を実装するためにsea-streamerを統合しますspring-opentelemetry : opentelemetryと統合して、ロギング、メトリック、トレースの完全な観測可能性を実装するspring-tarpc : tarpcを統合してRPC呼び出しを実装しますspring-sqlx-migration-pluginspring-opendalもっと>>
また、コミュニティの専門家が独自のプラグインを提供することを歓迎します。寄与→
ここをクリックして、 spring-rsヘルプを使用するときに遭遇した一般的な問題を表示→