- add: uuid to generate service id for consul

- update: each service now generates a service id for registering with consul
This commit is contained in:
2024-12-10 13:31:35 -05:00
parent f847ec0896
commit 13d4b45859
5 changed files with 22 additions and 5 deletions

View File

@@ -36,8 +36,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let db_address = get_service_address(&consul_url, "database-service").await?; let db_address = get_service_address(&consul_url, "database-service").await?;
// Register service with Consul // Register service with Consul
let service_id = consul_registration::generate_service_id();
consul_registration::register_service( consul_registration::register_service(
&consul_url, &consul_url,
service_id.as_str(),
service_name.as_str(), service_name.as_str(),
service_address, service_address,
service_port.parse().unwrap_or(50052), service_port.parse().unwrap_or(50052),
@@ -71,7 +73,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
_ = signal::ctrl_c() => {}, _ = 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); info!("service {} deregistered", service_name);
Ok(()) Ok(())
} }

View File

@@ -37,8 +37,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let health_check_endpoint_addr = format!("{}:{}", service_address, health_port); let health_check_endpoint_addr = format!("{}:{}", service_address, health_port);
// Register service with Consul // Register service with Consul
let service_id = consul_registration::generate_service_id();
consul_registration::register_service( consul_registration::register_service(
&consul_url, &consul_url,
service_id.as_str(),
service_name.as_str(), service_name.as_str(),
service_address, service_address,
service_port.parse().unwrap_or(50052), service_port.parse().unwrap_or(50052),
@@ -78,7 +80,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
_ = signal::ctrl_c() => {}, _ = 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); info!("service {} deregistered", service_name);
Ok(()) Ok(())
} }

View File

@@ -84,8 +84,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let auth_address = get_service_address(&consul_url, "auth-service").await?; let auth_address = get_service_address(&consul_url, "auth-service").await?;
// Register service with Consul // Register service with Consul
let service_id = consul_registration::generate_service_id();
consul_registration::register_service( consul_registration::register_service(
&consul_url, &consul_url,
service_id.as_str(),
service_name.as_str(), service_name.as_str(),
service_address, service_address,
service_port.parse().unwrap_or(50052), service_port.parse().unwrap_or(50052),
@@ -136,7 +138,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
_ = signal::ctrl_c() => {}, _ = 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); info!("service {} deregistered", service_name);
Ok(()) Ok(())
} }

View File

@@ -7,3 +7,5 @@ edition = "2021"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.12.9", features = ["json"] } reqwest = { version = "0.12.9", features = ["json"] }
tracing = "0.1" tracing = "0.1"
rand = "0.9.0-beta.1"
uuid = { version = "1.11.0", features = ["v4"] }

View File

@@ -1,8 +1,10 @@
use reqwest::Client; use reqwest::Client;
use serde::Serialize; use serde::Serialize;
use uuid::Uuid;
#[derive(Serialize)] #[derive(Serialize)]
struct ConsulRegistration { struct ConsulRegistration {
id: String,
name: String, name: String,
address: String, address: String,
port: u16, port: u16,
@@ -15,14 +17,21 @@ struct ConsulCheck {
interval: String, interval: String,
} }
pub fn generate_service_id() -> String {
Uuid::new_v4().to_string()
}
pub async fn register_service( pub async fn register_service(
consul_url: &str, consul_url: &str,
service_id: &str,
service_name: &str, service_name: &str,
service_address: &str, service_address: &str,
service_port: u16, service_port: u16,
health_check_url: &str, health_check_url: &str,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
let registration = ConsulRegistration { let registration = ConsulRegistration {
id: service_id.to_string(),
name: service_name.to_string(), name: service_name.to_string(),
address: service_address.to_string(), address: service_address.to_string(),
port: service_port, port: service_port,
@@ -45,9 +54,9 @@ pub async fn register_service(
Ok(()) Ok(())
} }
pub async fn deregister_service(consul_url: &str, service_name: &str) -> Result<(), Box<dyn std::error::Error>> { pub async fn deregister_service(consul_url: &str, service_id: &str) -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(); 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 client
.put(&consul_deregister_url) .put(&consul_deregister_url)