pub struct ServiceRuntime { /* private fields */ }Expand description
服务运行时
统一管理服务的生命周期,包括:
- 任务启动和管理(HTTP, gRPC, MQ 消费者等)
- 服务注册和注销
- 优雅停机
- 状态监控
§示例
§简单模式(不注册服务)
use flare_core_runtime::ServiceRuntime;
use flare_core_runtime::task::SpawnTask;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let runtime = ServiceRuntime::new("my-service")
.add_spawn("my-task", async { Ok(()) });
runtime.run().await?;
Ok(())
}§完整模式(带服务注册)
use flare_core_runtime::ServiceRuntime;
use std::net::SocketAddr;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let runtime = ServiceRuntime::new("my-service")
.with_address("0.0.0.0:8080".parse().unwrap())
.add_spawn("grpc", async { Ok(()) });
runtime.run_with_registration(|addr| {
Box::pin(async move {
// 注册服务
Ok(None)
})
}).await?;
Ok(())
}Implementations§
Source§impl ServiceRuntime
impl ServiceRuntime
Sourcepub fn simple() -> Self
pub fn simple() -> Self
创建简单的任务运行器(无需服务名和地址)
用于运行 MQ 消费者、自定义任务等简单场景
§示例
use flare_core_runtime::ServiceRuntime;
// 仅运行 MQ 消费者
let runtime = ServiceRuntime::simple()
.add_spawn("kafka-consumer", async { Ok(()) });Sourcepub fn mq_consumer() -> Self
pub fn mq_consumer() -> Self
创建 MQ 消费者运行器
专门用于运行 MQ 消费者的便捷方法
§示例
use flare_core_runtime::ServiceRuntime;
let runtime = ServiceRuntime::mq_consumer()
.add_spawn("kafka-consumer", async { Ok(()) })
.add_spawn("nats-consumer", async { Ok(()) });Sourcepub fn tasks() -> Self
pub fn tasks() -> Self
创建自定义任务运行器
专门用于运行自定义任务的便捷方法
§示例
use flare_core_runtime::ServiceRuntime;
let runtime = ServiceRuntime::tasks()
.add_spawn("task-1", async { Ok(()) })
.add_spawn("task-2", async { Ok(()) });Sourcepub fn with_address(self, address: SocketAddr) -> Self
pub fn with_address(self, address: SocketAddr) -> Self
Sourcepub fn with_config(self, config: RuntimeConfig) -> Self
pub fn with_config(self, config: RuntimeConfig) -> Self
设置运行时配置
Sourcepub fn with_registry(self, registry: Box<dyn ServiceRegistry>) -> Self
pub fn with_registry(self, registry: Box<dyn ServiceRegistry>) -> Self
设置服务注册器
Sourcepub fn with_health_checker(self, checker: HealthChecker) -> Self
pub fn with_health_checker(self, checker: HealthChecker) -> Self
设置健康检查器
Sourcepub fn add_health_check(self, check: Arc<dyn HealthCheck>) -> Self
pub fn add_health_check(self, check: Arc<dyn HealthCheck>) -> Self
添加健康检查项
Sourcepub fn with_health_failure_action(self, action: HealthFailureAction) -> Self
pub fn with_health_failure_action(self, action: HealthFailureAction) -> Self
设置健康检查失败时的行为
Sourcepub fn add_spawn_with_deps<Fut>(
self,
name: impl Into<String>,
future: Fut,
dependencies: Vec<String>,
) -> Self
pub fn add_spawn_with_deps<Fut>( self, name: impl Into<String>, future: Fut, dependencies: Vec<String>, ) -> Self
添加 spawn 任务(带依赖)
Sourcepub fn add_spawn_with_shutdown<F, Fut>(
self,
name: impl Into<String>,
future_fn: F,
) -> Self
pub fn add_spawn_with_shutdown<F, Fut>( self, name: impl Into<String>, future_fn: F, ) -> Self
添加 spawn 任务(需要 shutdown)
Sourcepub fn state_tracker(&self) -> Arc<StateTracker> ⓘ
pub fn state_tracker(&self) -> Arc<StateTracker> ⓘ
获取状态追踪器
Sourcepub async fn run(self) -> Result<()>
pub async fn run(self) -> Result<()>
运行服务(简单模式,不注册服务)
执行以下步骤:
- 启动所有任务
- 等待所有任务就绪
- 等待关闭信号(Ctrl+C)
- 优雅关闭所有任务
Sourcepub async fn run_with_signals(
self,
signals: Vec<Box<dyn ShutdownSignal>>,
) -> Result<()>
pub async fn run_with_signals( self, signals: Vec<Box<dyn ShutdownSignal>>, ) -> Result<()>
Sourcepub async fn run_with_registration<F, Fut>(self, register_fn: F) -> Result<()>
pub async fn run_with_registration<F, Fut>(self, register_fn: F) -> Result<()>
运行服务(带服务注册)
执行以下步骤:
- 启动所有任务
- 等待所有任务就绪
- 注册服务
- 等待关闭信号
- 注销服务
- 优雅关闭所有任务
Sourcepub async fn run_with_registration_and_signals<F, Fut>(
self,
register_fn: F,
signals: Vec<Box<dyn ShutdownSignal>>,
) -> Result<()>
pub async fn run_with_registration_and_signals<F, Fut>( self, register_fn: F, signals: Vec<Box<dyn ShutdownSignal>>, ) -> Result<()>
运行服务(带服务注册 + 自定义停机信号)
聚合部署进程可注入 ChannelSignal,
让多个注册型服务共享同一个外部生命周期控制。
Auto Trait Implementations§
impl !RefUnwindSafe for ServiceRuntime
impl !Sync for ServiceRuntime
impl !UnwindSafe for ServiceRuntime
impl Freeze for ServiceRuntime
impl Send for ServiceRuntime
impl Unpin for ServiceRuntime
impl UnsafeUnpin for ServiceRuntime
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more