- add: service discovery protocol using consul
This commit is contained in:
53
auth-service/src/service_discovery.rs
Normal file
53
auth-service/src/service_discovery.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use std::collections::HashMap;
|
||||
use std::iter::Map;
|
||||
use consul::Client;
|
||||
use consul::health::Health;
|
||||
use reqwest::Response;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Address {
|
||||
Address: String,
|
||||
Port: u16,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct TaggedAddresses {
|
||||
lan_ipv4: Address,
|
||||
wan_ipv4: Address,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Weights {
|
||||
Passing: u8,
|
||||
Warning: u8,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Service {
|
||||
pub(crate) ID: String,
|
||||
pub(crate) Service: String,
|
||||
pub(crate) Tags: Vec<String>,
|
||||
pub(crate) Port: u16,
|
||||
pub(crate) Address: String,
|
||||
pub(crate) TaggedAddresses: TaggedAddresses,
|
||||
pub(crate) Weights: Weights,
|
||||
pub(crate) EnableTagOverride: bool,
|
||||
pub(crate) ContentHash: String,
|
||||
pub(crate) Datacenter: String,
|
||||
}
|
||||
|
||||
pub async fn get_service_address(consul_url: &str, service_name: &str) -> Result<(Service), Box<dyn std::error::Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let consul_service_url = format!("{}/v1/agent/service/{}", consul_url, service_name);
|
||||
|
||||
let response = client
|
||||
.get(&consul_service_url)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json::<Service>()
|
||||
.await?; // Ensure response is successful
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
Reference in New Issue
Block a user