- update: service discovery now supports retrieving multiple service nodes
This commit is contained in:
@@ -9,6 +9,7 @@ use std::str::FromStr;
|
||||
use tokio::{select, signal};
|
||||
use tonic::transport::Server;
|
||||
use tracing::{info, Level};
|
||||
use tracing::log::debug;
|
||||
use warp::Filter;
|
||||
use utils::consul_registration;
|
||||
use utils::service_discovery::get_service_address;
|
||||
@@ -33,7 +34,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let service_port = port.clone();
|
||||
let health_check_url = format!("http://{}:{}/health", service_address, health_port);
|
||||
let health_check_endpoint_addr = format!("{}:{}", service_address, health_port);
|
||||
let db_address = get_service_address(&consul_url, "database-service").await?;
|
||||
let db_nodes = get_service_address(&consul_url, "database-service").await?;
|
||||
|
||||
// Register service with Consul
|
||||
let service_id = consul_registration::generate_service_id();
|
||||
@@ -53,7 +54,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
tokio::spawn(warp::serve(health_route).run(health_check_endpoint_addr.to_socket_addrs()?.next().unwrap()));
|
||||
|
||||
let db_url = format!("http://{}:{}", db_address.Address, db_address.Port);
|
||||
let db_address = db_nodes.get(0).unwrap();
|
||||
let db_url = format!("http://{}:{}", db_address.0, db_address.1);
|
||||
let database_client = DatabaseClient::connect(&db_url).await?;
|
||||
|
||||
let full_addr = format!("{}:{}", &addr, port);
|
||||
|
||||
@@ -81,7 +81,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let service_port = port.clone();
|
||||
let health_check_url = format!("http://{}:{}/health", service_address, health_port);
|
||||
let health_check_endpoint_addr = format!("{}:{}", service_address, health_port);
|
||||
let auth_address = get_service_address(&consul_url, "auth-service").await?;
|
||||
let auth_node = get_service_address(&consul_url, "auth-service").await?;
|
||||
|
||||
// Register service with Consul
|
||||
let service_id = consul_registration::generate_service_id();
|
||||
@@ -101,14 +101,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
tokio::spawn(warp::serve(health_route).run(health_check_endpoint_addr.to_socket_addrs()?.next().unwrap()));
|
||||
|
||||
let auth_url = format!("http://{}:{}", auth_address.Address, auth_address.Port);
|
||||
let auth_address = auth_node.get(0).unwrap();
|
||||
let auth_url = format!("http://{}:{}", auth_address.0, auth_address.1);
|
||||
let auth_client = Arc::new(Mutex::new(AuthClient::connect(&auth_url).await?));
|
||||
|
||||
let full_addr = format!("{}:{}", &addr, port);
|
||||
// let address = full_addr.parse().expect("Invalid address");
|
||||
|
||||
|
||||
|
||||
tokio::spawn(async move {
|
||||
let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_CONNECTIONS));
|
||||
let listener = TcpListener::bind(full_addr.clone()).await.unwrap();
|
||||
|
||||
@@ -1,48 +1,40 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Address {
|
||||
Address: String,
|
||||
Port: u16,
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ServiceNode {
|
||||
ServiceAddress: String,
|
||||
ServicePort: 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 ID: String,
|
||||
pub Service: String,
|
||||
pub Tags: Vec<String>,
|
||||
pub Port: u16,
|
||||
pub Address: String,
|
||||
pub TaggedAddresses: TaggedAddresses,
|
||||
pub Weights: Weights,
|
||||
pub EnableTagOverride: bool,
|
||||
pub ContentHash: String,
|
||||
pub Datacenter: String,
|
||||
}
|
||||
|
||||
pub async fn get_service_address(consul_url: &str, service_name: &str) -> Result<(Service), Box<dyn std::error::Error>> {
|
||||
pub async fn get_service_address(consul_url: &str, service_name: &str) -> Result<Vec<(String, u16)>, Box<dyn std::error::Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let consul_service_url = format!("{}/v1/agent/service/{}", consul_url, service_name);
|
||||
let consul_service_url = format!("{}/v1/catalog/service/{}", consul_url, service_name);
|
||||
|
||||
let response = client
|
||||
.get(&consul_service_url)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json::<Service>()
|
||||
.await?; // Ensure response is successful
|
||||
.await?;
|
||||
|
||||
Ok(response)
|
||||
if !response.status().is_success() {
|
||||
return Err(format!(
|
||||
"Failed to fetch service nodes for '{}': {}",
|
||||
service_name, response.status()
|
||||
)
|
||||
.into());
|
||||
}
|
||||
|
||||
// Deserialize the response into a Vec<ServiceNode>
|
||||
let nodes: Vec<ServiceNode> = response.json().await?;
|
||||
|
||||
// Map the nodes to (address, port) tuples
|
||||
let addresses: Vec<(String, u16)> = nodes
|
||||
.into_iter()
|
||||
.map(|node| (node.ServiceAddress, node.ServicePort))
|
||||
.collect();
|
||||
|
||||
if addresses.is_empty() {
|
||||
Err(format!("No nodes found for service '{}'", service_name).into())
|
||||
} else {
|
||||
Ok(addresses)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user