ServiceDiscoveryClient

Struct ServiceDiscoveryClient 

Source
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

Source

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")?;
Source

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 server
  • timeout - HTTP request timeout
  • retry_attempts - Number of retry attempts for failed requests
  • retry_delay - Base delay between retry attempts
§Returns

Returns a Result containing the client or an error if the URL is invalid.

Source

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 register
  • host - The hostname or IP address where the service is running
  • port - The port number where the service is listening
  • options - 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);
Source

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 discover
  • options - 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);
Source

pub async fn get_services_by_tag(&self, tag: &str) -> Result<Vec<Service>>

Finds all services that have the specified tag.

§Arguments
  • tag - The tag to search for
§Returns

Returns a vector of Service objects that have the specified tag.

Source

pub async fn call_service<T>( &self, service_name: &str, path: &str, method: Method, body: Option<Value>, ) -> Result<T>

Calls a REST API endpoint on a discovered service with retry logic.

§Arguments
  • service_name - The name of the service to call
  • path - The API path to call
  • method - The HTTP method to use
  • body - Optional request body
§Returns

Returns the deserialized response of type T.

Source

pub async fn get<T>(&self, service_name: &str, path: &str) -> Result<T>

Makes an HTTP GET request to a discovered service.

§Arguments
  • service_name - The name of the service to call
  • path - 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?;
Source

pub async fn post<T>( &self, service_name: &str, path: &str, body: Value, ) -> Result<T>

Makes an HTTP POST request to a discovered service.

§Arguments
  • service_name - The name of the service to call
  • path - The API path to call
  • body - The JSON body to send
§Returns

Returns the deserialized response of type T.

Source

pub async fn put<T>( &self, service_name: &str, path: &str, body: Value, ) -> Result<T>

Makes an HTTP PUT request to a discovered service.

§Arguments
  • service_name - The name of the service to call
  • path - The API path to call
  • body - The JSON body to send
§Returns

Returns the deserialized response of type T.

Source

pub async fn delete(&self, service_name: &str, path: &str) -> Result<()>

Makes an HTTP DELETE request to a discovered service.

§Arguments
  • service_name - The name of the service to call
  • path - The API path to call
§Returns

Returns an empty result on success.

Source

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?;
Source

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.

Source

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

Source§

fn clone(&self) -> ServiceDiscoveryClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for ServiceDiscoveryClient

Service discovery client for interacting with the ScoutQuest server.

Source§

fn drop(&mut self)

This method is called when the ServiceDiscoveryClient is dropped.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,