- 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

@@ -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"] }

View File

@@ -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<dyn std::error::Error>> {
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<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 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)