- add: service discovery protocol using consul
This commit is contained in:
59
auth-service/src/consul_registration.rs
Normal file
59
auth-service/src/consul_registration.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use reqwest::Client;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ConsulRegistration {
|
||||
name: String,
|
||||
address: String,
|
||||
port: u16,
|
||||
check: ConsulCheck,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ConsulCheck {
|
||||
http: String,
|
||||
interval: String,
|
||||
}
|
||||
|
||||
pub async fn register_service(
|
||||
consul_url: &str,
|
||||
service_name: &str,
|
||||
service_address: &str,
|
||||
service_port: u16,
|
||||
health_check_url: &str,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let registration = ConsulRegistration {
|
||||
name: service_name.to_string(),
|
||||
address: service_address.to_string(),
|
||||
port: service_port,
|
||||
check: ConsulCheck {
|
||||
http: health_check_url.to_string(),
|
||||
interval: "10s".to_string(), // Health check interval
|
||||
},
|
||||
};
|
||||
|
||||
let client = Client::new();
|
||||
let consul_register_url = format!("{}/v1/agent/service/register", consul_url);
|
||||
|
||||
client
|
||||
.put(&consul_register_url)
|
||||
.json(®istration)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?; // Ensure response is successful
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn deregister_service(consul_url: &str, service_name: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let client = Client::new();
|
||||
let consul_deregister_url = format!("{}/v1/agent/service/deregister/{}", consul_url, service_name);
|
||||
|
||||
client
|
||||
.put(&consul_deregister_url)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?; // Ensure response is successful
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,17 +1,16 @@
|
||||
use warp::Filter;
|
||||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
use std::net::ToSocketAddrs;
|
||||
use tonic::transport::Server;
|
||||
use auth_service::grpc::MyAuthService;
|
||||
use auth_service::database_client::DatabaseClient;
|
||||
use auth_service::database_client::DatabaseClientTrait;
|
||||
use auth_service::auth::auth_service_server::AuthServiceServer;
|
||||
use crate::service_discovery::get_service_address;
|
||||
|
||||
pub mod auth {
|
||||
tonic::include_proto!("auth"); // Path matches the package name in auth.proto
|
||||
}
|
||||
pub mod database {
|
||||
tonic::include_proto!("database"); // Matches package name in database.proto
|
||||
}
|
||||
mod consul_registration;
|
||||
mod service_discovery;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
@@ -25,11 +24,45 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.init();
|
||||
|
||||
// Set the gRPC server address
|
||||
let addr = env::var("AUTH_SERVICE_ADDR").unwrap_or_else(|_| "127.0.0.1:50051".to_string());
|
||||
let addr = env::var("AUTH_SERVICE_ADDR").unwrap_or_else(|_| "127.0.0.1".to_string());
|
||||
let port = env::var("AUTH_SERVICE_PORT").unwrap_or_else(|_| "50051".to_string());
|
||||
let db_addr = env::var("DATABASE_SERVICE_ADDR").unwrap_or_else(|_| "http://127.0.0.1:50052".to_string());
|
||||
let database_client = DatabaseClient::connect(&db_addr).await?;
|
||||
|
||||
let addr = addr.parse().expect("Invalid address");
|
||||
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 = addr.as_str();
|
||||
let service_port = port.clone();
|
||||
let health_check_url = format!("http://{}:{}/health", service_address, service_port);
|
||||
let health_check_endpoint_addr = format!("{}:8081", service_address);
|
||||
|
||||
// Register service with Consul
|
||||
consul_registration::register_service(
|
||||
&consul_url,
|
||||
service_name.as_str(),
|
||||
service_address,
|
||||
service_port.parse().unwrap_or(50052),
|
||||
&health_check_url,
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Start health-check endpoint
|
||||
let health_route = warp::path!("health")
|
||||
.map(|| warp::reply::with_status("OK", warp::http::StatusCode::OK));
|
||||
|
||||
tokio::spawn(warp::serve(health_route).run(health_check_endpoint_addr.to_socket_addrs()?.next().unwrap()));
|
||||
|
||||
let db_address = get_service_address(&consul_url, "database-service").await?;
|
||||
|
||||
tokio::spawn(async move {
|
||||
tokio::signal::ctrl_c().await.unwrap();
|
||||
consul_registration::deregister_service(&consul_url, service_name.as_str()).await.expect("");
|
||||
});
|
||||
|
||||
let db_url = format!("http://{}:{}", db_address.Address, db_address.Port);
|
||||
let database_client = DatabaseClient::connect(&db_url).await?;
|
||||
|
||||
let full_addr = format!("{}:{}", &addr, port);
|
||||
let address = full_addr.parse().expect("Invalid address");
|
||||
let auth_service = MyAuthService {
|
||||
db_client: database_client,
|
||||
};
|
||||
@@ -39,7 +72,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Start the gRPC server
|
||||
Server::builder()
|
||||
.add_service(AuthServiceServer::new(auth_service))
|
||||
.serve(addr)
|
||||
.serve(address)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
|
||||
53
auth-service/src/service_discovery.rs
Normal file
53
auth-service/src/service_discovery.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use std::collections::HashMap;
|
||||
use std::iter::Map;
|
||||
use consul::Client;
|
||||
use consul::health::Health;
|
||||
use reqwest::Response;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Address {
|
||||
Address: String,
|
||||
Port: u16,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct TaggedAddresses {
|
||||
lan_ipv4: Address,
|
||||
wan_ipv4: Address,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Weights {
|
||||
Passing: u8,
|
||||
Warning: u8,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Service {
|
||||
pub(crate) ID: String,
|
||||
pub(crate) Service: String,
|
||||
pub(crate) Tags: Vec<String>,
|
||||
pub(crate) Port: u16,
|
||||
pub(crate) Address: String,
|
||||
pub(crate) TaggedAddresses: TaggedAddresses,
|
||||
pub(crate) Weights: Weights,
|
||||
pub(crate) EnableTagOverride: bool,
|
||||
pub(crate) ContentHash: String,
|
||||
pub(crate) Datacenter: String,
|
||||
}
|
||||
|
||||
pub async fn get_service_address(consul_url: &str, service_name: &str) -> Result<(Service), Box<dyn std::error::Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let consul_service_url = format!("{}/v1/agent/service/{}", consul_url, service_name);
|
||||
|
||||
let response = client
|
||||
.get(&consul_service_url)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json::<Service>()
|
||||
.await?; // Ensure response is successful
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
Reference in New Issue
Block a user