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 new(service_name: impl Into<String>) -> ServiceRuntime
pub fn new(service_name: impl Into<String>) -> ServiceRuntime
Sourcepub fn simple() -> ServiceRuntime
pub fn simple() -> ServiceRuntime
创建简单的任务运行器(无需服务名和地址)
用于运行 MQ 消费者、自定义任务等简单场景
§示例
use flare_core_runtime::ServiceRuntime;
// 仅运行 MQ 消费者
let runtime = ServiceRuntime::simple()
.add_spawn("kafka-consumer", async { Ok(()) });Sourcepub fn mq_consumer() -> ServiceRuntime
pub fn mq_consumer() -> ServiceRuntime
创建 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() -> ServiceRuntime
pub fn tasks() -> ServiceRuntime
创建自定义任务运行器
专门用于运行自定义任务的便捷方法
§示例
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) -> ServiceRuntime
pub fn with_address(self, address: SocketAddr) -> ServiceRuntime
Sourcepub fn with_config(self, config: RuntimeConfig) -> ServiceRuntime
pub fn with_config(self, config: RuntimeConfig) -> ServiceRuntime
设置运行时配置
Sourcepub fn with_registry(self, registry: Box<dyn ServiceRegistry>) -> ServiceRuntime
pub fn with_registry(self, registry: Box<dyn ServiceRegistry>) -> ServiceRuntime
设置服务注册器
Sourcepub fn with_health_checker(self, checker: HealthChecker) -> ServiceRuntime
pub fn with_health_checker(self, checker: HealthChecker) -> ServiceRuntime
设置健康检查器
Sourcepub fn add_health_check(self, check: Arc<dyn HealthCheck>) -> ServiceRuntime
pub fn add_health_check(self, check: Arc<dyn HealthCheck>) -> ServiceRuntime
添加健康检查项
Sourcepub fn with_health_failure_action(
self,
action: HealthFailureAction,
) -> ServiceRuntime
pub fn with_health_failure_action( self, action: HealthFailureAction, ) -> ServiceRuntime
设置健康检查失败时的行为
Sourcepub fn add_task(self, task: Box<dyn Task>) -> ServiceRuntime
pub fn add_task(self, task: Box<dyn Task>) -> ServiceRuntime
Sourcepub fn add_spawn<Fut>(
self,
name: impl Into<String>,
future: Fut,
) -> ServiceRuntime
pub fn add_spawn<Fut>( self, name: impl Into<String>, future: Fut, ) -> ServiceRuntime
Sourcepub fn add_spawn_with_deps<Fut>(
self,
name: impl Into<String>,
future: Fut,
dependencies: Vec<String>,
) -> ServiceRuntime
pub fn add_spawn_with_deps<Fut>( self, name: impl Into<String>, future: Fut, dependencies: Vec<String>, ) -> ServiceRuntime
添加 spawn 任务(带依赖)
Sourcepub fn add_spawn_with_shutdown<F, Fut>(
self,
name: impl Into<String>,
future_fn: F,
) -> ServiceRuntime
pub fn add_spawn_with_shutdown<F, Fut>( self, name: impl Into<String>, future_fn: F, ) -> ServiceRuntime
添加 spawn 任务(需要 shutdown)
Sourcepub fn state_tracker(&self) -> Arc<StateTracker> ⓘ
pub fn state_tracker(&self) -> Arc<StateTracker> ⓘ
获取状态追踪器
Sourcepub async fn run(self) -> Result<(), Error>
pub async fn run(self) -> Result<(), Error>
运行服务(简单模式,不注册服务)
执行以下步骤:
- 启动所有任务
- 等待所有任务就绪
- 等待关闭信号(Ctrl+C)
- 优雅关闭所有任务
Sourcepub async fn run_with_signals(
self,
signals: Vec<Box<dyn ShutdownSignal>>,
) -> Result<(), Error>
pub async fn run_with_signals( self, signals: Vec<Box<dyn ShutdownSignal>>, ) -> Result<(), Error>
Sourcepub async fn run_with_registration<F, Fut>(
self,
register_fn: F,
) -> Result<(), Error>
pub async fn run_with_registration<F, Fut>( self, register_fn: F, ) -> Result<(), Error>
运行服务(带服务注册)
执行以下步骤:
- 启动所有任务
- 等待所有任务就绪
- 注册服务
- 等待关闭信号
- 注销服务
- 优雅关闭所有任务
Sourcepub async fn run_with_registration_and_signals<F, Fut>(
self,
register_fn: F,
signals: Vec<Box<dyn ShutdownSignal>>,
) -> Result<(), Error>
pub async fn run_with_registration_and_signals<F, Fut>( self, register_fn: F, signals: Vec<Box<dyn ShutdownSignal>>, ) -> Result<(), Error>
运行服务(带服务注册 + 自定义停机信号)
聚合部署进程可注入 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
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request