- add: gRPC health check for gRPC services
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use reqwest::Client;
|
||||
use serde::Serialize;
|
||||
use serde_json::json;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
@@ -10,23 +10,6 @@ use warp::Filter;
|
||||
|
||||
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ConsulRegistration {
|
||||
id: String,
|
||||
name: String,
|
||||
address: String,
|
||||
port: u16,
|
||||
tags: Vec<String>,
|
||||
meta: HashMap<String, String>,
|
||||
check: ConsulCheck,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ConsulCheck {
|
||||
http: String,
|
||||
interval: String,
|
||||
}
|
||||
|
||||
pub fn generate_service_id() -> String {
|
||||
Uuid::new_v4().to_string()
|
||||
}
|
||||
@@ -60,21 +43,33 @@ pub async fn register_service(
|
||||
service_port: u16,
|
||||
tags: Vec<String>,
|
||||
mut meta: HashMap<String, String>,
|
||||
health_check_url: &str,
|
||||
health_check_protocol: Option<&str>,
|
||||
health_check_address: Option<&str>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
meta.insert("version".to_string(), VERSION.to_string());
|
||||
let registration = ConsulRegistration {
|
||||
id: service_id.to_string(),
|
||||
name: service_name.to_string(),
|
||||
address: service_address.to_string(),
|
||||
port: service_port,
|
||||
tags,
|
||||
meta,
|
||||
check: ConsulCheck {
|
||||
http: health_check_url.to_string(),
|
||||
interval: "10s".to_string(), // Health check interval
|
||||
},
|
||||
};
|
||||
|
||||
let mut check_protocol = "grpc".to_string();
|
||||
let mut check_address = format!("{}:{}", service_name, service_port);
|
||||
if health_check_protocol.is_some() {
|
||||
check_protocol = health_check_protocol.unwrap().to_string();
|
||||
}
|
||||
if health_check_address.is_some() {
|
||||
check_address = health_check_address.unwrap().to_string();
|
||||
}
|
||||
|
||||
let registration = json!({
|
||||
"id": format!("{}-{}", service_name, service_id),
|
||||
"name": service_name.to_string(),
|
||||
"address": service_address.to_string(),
|
||||
"port": service_port,
|
||||
"tags": tags,
|
||||
"meta": meta,
|
||||
"check": {
|
||||
check_protocol: check_address,
|
||||
"interval": "10s",
|
||||
"timeout": "2s"
|
||||
}
|
||||
});
|
||||
|
||||
let client = Client::new();
|
||||
let consul_register_url = format!("{}/v1/agent/service/register", consul_url);
|
||||
|
||||
Reference in New Issue
Block a user