- update: tell consul to use docker dns to resolve CNAME addresses
- add: load balancer for consul services - update: dns lookup to now return the service address - update: docker consul to the latest version
This commit is contained in:
@@ -11,6 +11,7 @@ use std::sync::Arc;
|
||||
use tonic::transport::Server;
|
||||
use tracing::{debug, info, Level};
|
||||
use utils::consul_registration;
|
||||
use utils::multi_service_load_balancer::{LoadBalancingStrategy, MultiServiceLoadBalancer};
|
||||
use utils::service_discovery::{get_service_address, get_service_endpoints_by_dns};
|
||||
|
||||
#[tokio::main]
|
||||
@@ -33,14 +34,35 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let consul_port = env::var("CONSUL_PORT").unwrap_or_else(|_| "8500".to_string());
|
||||
let consul_dns_port = env::var("CONSUL_DNS_PORT").unwrap_or_else(|_| "8600".to_string());
|
||||
let consul_url = format!("http://{}:{}", consul_address, consul_port);
|
||||
// let consul_url = env::var("CONSUL_URL").unwrap_or_else(|_| "http://127.0.0.1:8500".to_string());
|
||||
let consul_dns_url = format!("{}:{}", consul_address, consul_dns_port);
|
||||
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 db_nodes = get_service_address(&consul_url, "database-service").await?;
|
||||
let session_nodes = get_service_address(&consul_url, "session-service").await?;
|
||||
let temp_session_nodes = get_service_endpoints_by_dns(format!("{}:{}", consul_address, consul_dns_port).as_str(), "grpc", "session-service").await?;
|
||||
debug!("{:?}", temp_session_nodes);
|
||||
|
||||
let lb = MultiServiceLoadBalancer::new(&consul_dns_url, LoadBalancingStrategy::RoundRobin);
|
||||
|
||||
let mut db_url = "".to_string();
|
||||
match lb.get_endpoint("database-service", "grpc").await? {
|
||||
Some(endpoint) => {
|
||||
db_url = format!("http://{}", endpoint);
|
||||
},
|
||||
None => {
|
||||
println!("No endpoints available for database-service");
|
||||
}
|
||||
}
|
||||
|
||||
let mut session_service_address = "".to_string();
|
||||
match lb.get_endpoint("session-service", "grpc").await? {
|
||||
Some(endpoint) => {
|
||||
session_service_address = format!("http://{}", endpoint);
|
||||
},
|
||||
None => {
|
||||
println!("No endpoints available for session-service");
|
||||
}
|
||||
}
|
||||
|
||||
let db_client = Arc::new(DatabaseClient::connect(&db_url).await?);
|
||||
let session_client = Arc::new(SessionServiceClient::connect(session_service_address).await?);
|
||||
|
||||
// Register service with Consul
|
||||
let service_id = consul_registration::get_or_generate_service_id(env!("CARGO_PKG_NAME"));
|
||||
@@ -60,20 +82,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
)
|
||||
.await?;
|
||||
|
||||
let db_address = db_nodes.get(0).unwrap();
|
||||
let db_url = format!(
|
||||
"http://{}:{}",
|
||||
db_address.ServiceAddress, db_address.ServicePort
|
||||
);
|
||||
let db_client = Arc::new(DatabaseClient::connect(&db_url).await?);
|
||||
|
||||
let session_address = session_nodes.get(0).unwrap();
|
||||
let session_address = format!(
|
||||
"http://{}:{}",
|
||||
session_address.ServiceAddress, session_address.ServicePort
|
||||
);
|
||||
let session_client = Arc::new(SessionServiceClient::connect(session_address).await?);
|
||||
|
||||
let full_addr = format!("{}:{}", &addr, port);
|
||||
let address = full_addr.parse().expect("Invalid address");
|
||||
let auth_service = MyAuthService {
|
||||
|
||||
Reference in New Issue
Block a user