- 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

@@ -15,6 +15,7 @@ prost = "0.13.4"
warp = "0.3.7"
chrono = "0.4.39"
serde_json = "1.0.133"
tonic-health = "0.12.3"
[build-dependencies]
tonic-build = "0.12.3"
tonic-build = "0.12.3"

View File

@@ -34,7 +34,6 @@ 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("SESSION_SERVICE_PORT").unwrap_or_else(|_| "50055".to_string());
let health_port = env::var("HEALTH_CHECK_PORT").unwrap_or_else(|_| "8084".to_string());
let redis_url =
std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
@@ -43,12 +42,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let service_address =
env::var("SESSION_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);
// 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,
@@ -58,20 +56,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
service_port.parse().unwrap_or(50055),
tags,
meta,
&health_check_url,
None,
None,
)
.await?;
// Start health-check endpoint
consul_registration::start_health_check(addr.as_str()).await?;
.await?;
let full_addr = format!("{}:{}", &addr, port);
let address = full_addr.parse().expect("Invalid address");
let redis_cache = Arc::new(Mutex::new(RedisCache::new(&redis_url)));
let session_service = SessionServiceImpl { redis: redis_cache };
let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter.set_serving::<SessionServiceServer<SessionServiceImpl>>().await;
tokio::spawn(
Server::builder()
.add_service(health_service)
.add_service(SessionServiceServer::new(session_service))
.serve(address),
);