- add: service discovery protocol using consul
This commit is contained in:
@@ -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(())
|
||||
|
||||
Reference in New Issue
Block a user