pub struct ServiceDiscoveryClient { /* private fields */ }Expand description
The main client for interacting with ScoutQuest Service Discovery.
This client provides methods for service registration, discovery, and making HTTP calls to discovered services. It handles automatic heartbeats for registered services and includes retry logic for failed requests.
§Examples
use scoutquest_rust::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ServiceDiscoveryClient::new("http://localhost:8080")?;
// Register a service
client.register_service("my-service", "localhost", 3000, None).await?;
// Discover services
let instance = client.discover_service("other-service", None).await?;
Ok(())
}Implementations§
Source§impl ServiceDiscoveryClient
impl ServiceDiscoveryClient
Sourcepub fn new(discovery_url: &str) -> Result<Self>
pub fn new(discovery_url: &str) -> Result<Self>
Creates a new ServiceDiscoveryClient with default configuration.
§Arguments
discovery_url- The base URL of the ScoutQuest discovery server
§Returns
Returns a Result containing the client or an error if the URL is invalid.
§Examples
use scoutquest_rust::ServiceDiscoveryClient;
let client = ServiceDiscoveryClient::new("http://localhost:8080")?;Sourcepub fn with_config(
discovery_url: &str,
timeout: Duration,
retry_attempts: usize,
retry_delay: Duration,
) -> Result<Self>
pub fn with_config( discovery_url: &str, timeout: Duration, retry_attempts: usize, retry_delay: Duration, ) -> Result<Self>
Creates a new ServiceDiscoveryClient with custom configuration.
§Arguments
discovery_url- The base URL of the ScoutQuest discovery servertimeout- HTTP request timeoutretry_attempts- Number of retry attempts for failed requestsretry_delay- Base delay between retry attempts
§Returns
Returns a Result containing the client or an error if the URL is invalid.
Sourcepub async fn register_service(
&self,
service_name: &str,
host: &str,
port: u16,
options: Option<ServiceRegistrationOptions>,
) -> Result<ServiceInstance>
pub async fn register_service( &self, service_name: &str, host: &str, port: u16, options: Option<ServiceRegistrationOptions>, ) -> Result<ServiceInstance>
Registers a service with the ScoutQuest discovery server.
This method registers a service instance and starts automatic heartbeat to maintain the registration. Only one service can be registered per client.
§Arguments
service_name- The name of the service to registerhost- The hostname or IP address where the service is runningport- The port number where the service is listeningoptions- Optional registration options (metadata, tags, health check, etc.)
§Returns
Returns the registered ServiceInstance or an error if registration fails.
§Examples
use scoutquest_rust::*;
let client = ServiceDiscoveryClient::new("http://localhost:8080")?;
let options = ServiceRegistrationOptions::new()
.with_tags(vec!["api".to_string(), "v1".to_string()]);
let instance = client.register_service("user-service", "localhost", 3000, Some(options)).await?;
println!("Registered with ID: {}", instance.id);Sourcepub async fn discover_service(
&self,
service_name: &str,
options: Option<ServiceDiscoveryOptions>,
) -> Result<ServiceInstance>
pub async fn discover_service( &self, service_name: &str, options: Option<ServiceDiscoveryOptions>, ) -> Result<ServiceInstance>
Discovers a service instance from the ScoutQuest discovery server.
§Arguments
service_name- The name of the service to discoveroptions- Discovery options (healthy only, tags, etc.)
§Returns
Returns a ServiceInstance or an error if no instances are available.
§Examples
use scoutquest_rust::*;
let client = ServiceDiscoveryClient::new("http://localhost:8080")?;
let instance = client.discover_service("user-service", None).await?;
println!("Found instance: {}:{}", instance.host, instance.port);Sourcepub async fn call_service<T>(
&self,
service_name: &str,
path: &str,
method: Method,
body: Option<Value>,
) -> Result<T>where
T: DeserializeOwned,
pub async fn call_service<T>(
&self,
service_name: &str,
path: &str,
method: Method,
body: Option<Value>,
) -> Result<T>where
T: DeserializeOwned,
Sourcepub async fn get<T>(&self, service_name: &str, path: &str) -> Result<T>where
T: DeserializeOwned,
pub async fn get<T>(&self, service_name: &str, path: &str) -> Result<T>where
T: DeserializeOwned,
Makes an HTTP GET request to a discovered service.
§Arguments
service_name- The name of the service to callpath- The API path to call
§Returns
Returns the deserialized response of type T.
§Examples
use scoutquest_rust::*;
use serde_json::Value;
let client = ServiceDiscoveryClient::new("http://localhost:8080")?;
let response: Value = client.get("user-service", "/api/users").await?;Sourcepub async fn post<T>(
&self,
service_name: &str,
path: &str,
body: Value,
) -> Result<T>where
T: DeserializeOwned,
pub async fn post<T>(
&self,
service_name: &str,
path: &str,
body: Value,
) -> Result<T>where
T: DeserializeOwned,
Sourcepub async fn put<T>(
&self,
service_name: &str,
path: &str,
body: Value,
) -> Result<T>where
T: DeserializeOwned,
pub async fn put<T>(
&self,
service_name: &str,
path: &str,
body: Value,
) -> Result<T>where
T: DeserializeOwned,
Sourcepub async fn deregister(&self) -> Result<()>
pub async fn deregister(&self) -> Result<()>
Deregisters the currently registered service from the discovery server.
This stops the automatic heartbeat and removes the service registration. It’s important to call this method before dropping the client to ensure clean shutdown.
§Returns
Returns an empty result on success.
§Examples
use scoutquest_rust::*;
let client = ServiceDiscoveryClient::new("http://localhost:8080")?;
client.register_service("my-service", "localhost", 3000, None).await?;
// ... do work ...
client.deregister().await?;Sourcepub async fn get_registered_instance(&self) -> Option<ServiceInstance>
pub async fn get_registered_instance(&self) -> Option<ServiceInstance>
Retrieves the currently registered service instance.
This method returns a clone of the registered service instance, if it exists.
Sourcepub fn get_discovery_url(&self) -> &str
pub fn get_discovery_url(&self) -> &str
Retrieves the discovery URL for the service.
This method returns the discovery URL for the service.
Trait Implementations§
Source§impl Clone for ServiceDiscoveryClient
impl Clone for ServiceDiscoveryClient
Source§fn clone(&self) -> ServiceDiscoveryClient
fn clone(&self) -> ServiceDiscoveryClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more