- add: gRPC health check for gRPC services

This commit is contained in:
2025-03-10 03:56:47 -04:00
parent d583ca1228
commit ae04d2bf5b
12 changed files with 70 additions and 69 deletions

View File

@@ -26,6 +26,7 @@ warp = "0.3.7"
reqwest = { version = "0.12.9", features = ["json"] }
utils = { path = "../utils" }
uuid = "1.11.0"
tonic-health = "0.12.3"
[build-dependencies]
tonic-build = "0.12.3"

View File

@@ -28,21 +28,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set the gRPC server address
let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string());
let port = env::var("AUTH_SERVICE_PORT").unwrap_or_else(|_| "50051".to_string());
let health_port = env::var("HEALTH_CHECK_PORT").unwrap_or_else(|_| "8081".to_string());
let consul_url = env::var("CONSUL_URL").unwrap_or_else(|_| "http://127.0.0.1:8500".to_string());
let service_name = env::var("SERVICE_NAME").unwrap_or_else(|_| "auth-service".to_string());
let service_address = env::var("AUTH_SERVICE_ADDR").unwrap_or_else(|_| "127.0.0.1".to_string());
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_nodes = get_service_address(&consul_url, "database-service").await?;
let session_nodes = get_service_address(&consul_url, "session-service").await?;
// Register service with Consul
let service_id = consul_registration::get_or_generate_service_id(env!("CARGO_PKG_NAME"));
let version = env!("CARGO_PKG_VERSION").to_string();
let tags = vec![version];
let tags = vec![version, "grpc".to_string()];
let meta = HashMap::new();
consul_registration::register_service(
&consul_url,
@@ -52,12 +49,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
service_port.parse().unwrap_or(50052),
tags,
meta,
&health_check_url,
None,
None,
)
.await?;
// Start health-check endpoint
consul_registration::start_health_check(addr.as_str()).await?;
.await?;
let db_address = db_nodes.get(0).unwrap();
let db_url = format!(
@@ -80,11 +75,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
session_client,
};
let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter.set_serving::<AuthServiceServer<MyAuthService>>().await;
println!("Authentication Service running on {}", addr);
// Start the gRPC server
tokio::spawn(
Server::builder()
.add_service(health_service)
.add_service(AuthServiceServer::new(auth_service))
.serve(address),
);