diff --git a/auth-service/src/main.rs b/auth-service/src/main.rs index c4ccdcf..c4425c5 100644 --- a/auth-service/src/main.rs +++ b/auth-service/src/main.rs @@ -36,8 +36,10 @@ async fn main() -> Result<(), Box> { let db_address = get_service_address(&consul_url, "database-service").await?; // Register service with Consul + let service_id = consul_registration::generate_service_id(); consul_registration::register_service( &consul_url, + service_id.as_str(), service_name.as_str(), service_address, service_port.parse().unwrap_or(50052), @@ -71,7 +73,7 @@ async fn main() -> Result<(), Box> { _ = signal::ctrl_c() => {}, } - consul_registration::deregister_service(&consul_url, service_name.as_str()).await.expect(""); + consul_registration::deregister_service(&consul_url, service_id.as_str()).await.expect(""); info!("service {} deregistered", service_name); Ok(()) } diff --git a/database-service/src/main.rs b/database-service/src/main.rs index eceff8d..0d55e32 100644 --- a/database-service/src/main.rs +++ b/database-service/src/main.rs @@ -37,8 +37,10 @@ async fn main() -> Result<(), Box> { let health_check_endpoint_addr = format!("{}:{}", service_address, health_port); // Register service with Consul + let service_id = consul_registration::generate_service_id(); consul_registration::register_service( &consul_url, + service_id.as_str(), service_name.as_str(), service_address, service_port.parse().unwrap_or(50052), @@ -78,7 +80,7 @@ async fn main() -> Result<(), Box> { _ = signal::ctrl_c() => {}, } - consul_registration::deregister_service(&consul_url, service_name.as_str()).await.expect(""); + consul_registration::deregister_service(&consul_url, service_id.as_str()).await.expect(""); info!("service {} deregistered", service_name); Ok(()) } diff --git a/packet-service/src/main.rs b/packet-service/src/main.rs index d2f1f99..86bffc6 100644 --- a/packet-service/src/main.rs +++ b/packet-service/src/main.rs @@ -84,8 +84,10 @@ async fn main() -> Result<(), Box> { let auth_address = get_service_address(&consul_url, "auth-service").await?; // Register service with Consul + let service_id = consul_registration::generate_service_id(); consul_registration::register_service( &consul_url, + service_id.as_str(), service_name.as_str(), service_address, service_port.parse().unwrap_or(50052), @@ -136,7 +138,7 @@ async fn main() -> Result<(), Box> { _ = signal::ctrl_c() => {}, } - consul_registration::deregister_service(&consul_url, service_name.as_str()).await.expect(""); + consul_registration::deregister_service(&consul_url, service_id.as_str()).await.expect(""); info!("service {} deregistered", service_name); Ok(()) } diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 551d88b..8daf111 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" serde = { version = "1.0", features = ["derive"] } reqwest = { version = "0.12.9", features = ["json"] } tracing = "0.1" +rand = "0.9.0-beta.1" +uuid = { version = "1.11.0", features = ["v4"] } diff --git a/utils/src/consul_registration.rs b/utils/src/consul_registration.rs index be77976..a768540 100644 --- a/utils/src/consul_registration.rs +++ b/utils/src/consul_registration.rs @@ -1,8 +1,10 @@ use reqwest::Client; use serde::Serialize; +use uuid::Uuid; #[derive(Serialize)] struct ConsulRegistration { + id: String, name: String, address: String, port: u16, @@ -15,14 +17,21 @@ struct ConsulCheck { interval: String, } +pub fn generate_service_id() -> String { + Uuid::new_v4().to_string() +} + + pub async fn register_service( consul_url: &str, + service_id: &str, service_name: &str, service_address: &str, service_port: u16, health_check_url: &str, ) -> Result<(), Box> { let registration = ConsulRegistration { + id: service_id.to_string(), name: service_name.to_string(), address: service_address.to_string(), port: service_port, @@ -45,9 +54,9 @@ pub async fn register_service( Ok(()) } -pub async fn deregister_service(consul_url: &str, service_name: &str) -> Result<(), Box> { +pub async fn deregister_service(consul_url: &str, service_id: &str) -> Result<(), Box> { let client = Client::new(); - let consul_deregister_url = format!("{}/v1/agent/service/deregister/{}", consul_url, service_name); + let consul_deregister_url = format!("{}/v1/agent/service/deregister/{}", consul_url, service_id); client .put(&consul_deregister_url)