Skip to main content

flare_core_runtime/registry/
trait.rs

1//! ServiceRegistry trait 定义
2//!
3//! 服务注册抽象,支持多种服务发现系统:Consul, Etcd, Nacos 等
4
5use crate::error::RegistryError;
6use std::collections::HashMap;
7use std::future::Future;
8use std::net::SocketAddr;
9use std::pin::Pin;
10use std::time::Duration;
11
12/// 服务信息
13#[derive(Debug, Clone)]
14pub struct ServiceInfo {
15    /// 服务名称
16    pub name: String,
17    /// 服务 ID
18    pub id: String,
19    /// 服务地址
20    pub address: SocketAddr,
21    /// 服务元数据
22    pub metadata: HashMap<String, String>,
23    /// 心跳 TTL
24    pub ttl: Duration,
25}
26
27impl ServiceInfo {
28    /// 创建新的服务信息
29    pub fn new(name: impl Into<String>, id: impl Into<String>, address: SocketAddr) -> Self {
30        Self {
31            name: name.into(),
32            id: id.into(),
33            address,
34            metadata: HashMap::new(),
35            ttl: Duration::from_secs(10),
36        }
37    }
38
39    /// 添加元数据
40    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
41        self.metadata.insert(key.into(), value.into());
42        self
43    }
44
45    /// 设置心跳 TTL
46    pub fn with_ttl(mut self, ttl: Duration) -> Self {
47        self.ttl = ttl;
48        self
49    }
50}
51
52/// 服务注册抽象
53///
54/// 支持多种服务发现系统:Consul, Etcd, Nacos 等
55///
56/// # 实现说明
57///
58/// - 使用 Boxed Future 模式支持 trait object
59/// - 所有服务注册器必须实现此 trait
60///
61/// # 示例
62///
63/// ```rust
64/// use flare_core_runtime::registry::{ServiceRegistry, ServiceInfo};
65/// use flare_core_runtime::error::RegistryError;
66/// use std::pin::Pin;
67/// use std::future::Future;
68///
69/// struct ConsulRegistry {
70///     // Consul 客户端
71/// }
72///
73/// impl ServiceRegistry for ConsulRegistry {
74///     fn register<'a>(&'a mut self, service: &'a ServiceInfo) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + 'a>> {
75///         Box::pin(async move {
76///             // 注册服务到 Consul
77///             Ok(())
78///         })
79///     }
80///
81///     fn deregister<'a>(&'a mut self, service: &'a ServiceInfo) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + 'a>> {
82///         Box::pin(async move {
83///             // 从 Consul 注销服务
84///             Ok(())
85///         })
86///     }
87///
88///     fn send_heartbeat<'a>(&'a mut self, service_id: &'a str) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + 'a>> {
89///         Box::pin(async move {
90///             // 发送心跳
91///             Ok(())
92///         })
93///     }
94///
95///     fn shutdown(&mut self) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + '_>> {
96///         Box::pin(async move {
97///             // 关闭注册器
98///             Ok(())
99///         })
100///     }
101/// }
102/// ```
103pub trait ServiceRegistry: Send {
104    /// 注册服务
105    ///
106    /// # 参数
107    ///
108    /// * `service` - 服务信息
109    ///
110    /// # 返回
111    ///
112    /// 成功返回 `Ok(())`,失败返回 `RegistryError`
113    fn register<'a>(
114        &'a mut self,
115        service: &'a ServiceInfo,
116    ) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + 'a>>;
117
118    /// 注销服务
119    ///
120    /// # 参数
121    ///
122    /// * `service` - 服务信息
123    ///
124    /// # 返回
125    ///
126    /// 成功返回 `Ok(())`,失败返回 `RegistryError`
127    fn deregister<'a>(
128        &'a mut self,
129        service: &'a ServiceInfo,
130    ) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + 'a>>;
131
132    /// 发送心跳
133    ///
134    /// # 参数
135    ///
136    /// * `service_id` - 服务 ID
137    ///
138    /// # 返回
139    ///
140    /// 成功返回 `Ok(())`,失败返回 `RegistryError`
141    fn send_heartbeat<'a>(
142        &'a mut self,
143        service_id: &'a str,
144    ) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + 'a>>;
145
146    /// 关闭注册器
147    ///
148    /// # 返回
149    ///
150    /// 成功返回 `Ok(())`,失败返回 `RegistryError`
151    fn shutdown(&mut self) -> Pin<Box<dyn Future<Output = Result<(), RegistryError>> + Send + '_>>;
152}