Module apollo_client::conf[][src]

This is supported on crate feature conf only.
Expand description

Apollo configuration apis.

Refs: https://www.apolloconfig.com/#/zh/usage/other-language-client-user-guide.

Example

Simple fetch configuration:

use apollo_client::{
    conf::{meta::IpValue, requests::CachedFetchRequest, ApolloConfClientBuilder},
    errors::ApolloClientResult,
};
use ini::Properties;
use std::error::Error;
use url::Url;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    env_logger::init();

    // Create configuration client.
    let client =
        ApolloConfClientBuilder::new_via_config_service(Url::parse("http://localhost:8080")?)?
            .build()?;

    // Request apollo cached configuration api.
    let configuration: Properties = client
        .execute(
            CachedFetchRequest::builder()
                .app_id("SampleApp")
                .namespace_name("application.json")
                .ip(IpValue::HostName)
                .build(),
        )
        .await?;

    // Get the content of configuration.
    let content = configuration.get("content");
    dbg!(content);

    Ok(())
}

Watch configuration and fetch when changed:

use apollo_client::conf::{meta::IpValue, requests::WatchRequest, ApolloConfClientBuilder};
use cidr_utils::cidr::IpCidr;
use futures_util::{pin_mut, stream::StreamExt};
use std::error::Error;
use url::Url;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    env_logger::init();

    // Create configuration client.
    let client =
        ApolloConfClientBuilder::new_via_config_service(Url::parse("http://localhost:8080")?)?
            .build()?;

    // Request apollo notification api, and fetch configuration when notified.
    let stream = client.watch(
        WatchRequest::builder()
            .app_id("SampleApp")
            .namespace_names([
                "application.properties".into(),
                "application.json".into(),
                "application.yml".into(),
            ])
            .ip(IpValue::HostCidr(IpCidr::from_str("172.16.0.0/16")?))
            .build(),
    );

    pin_mut!(stream);

    // There is a dead loop, `next()` is returned when configuration has changed.
    while let Some(response) = stream.next().await {
        let responses = response?;
        for response in responses {
            let _ = dbg!(response);
        }
    }

    Ok(())
}

Modules

Configuration api metadata.

Configuration api requests.

Configuration api response.

Structs

Apollo configuration apis client.

Builder for ApolloConfClient.