- update: docker compose to add custom networks

- add: get service endpoints by using consul dns
This commit is contained in:
2025-03-10 06:09:26 -04:00
parent ae04d2bf5b
commit 81068759e5
8 changed files with 105 additions and 13 deletions

View File

@@ -9,9 +9,9 @@ use std::env;
use std::str::FromStr;
use std::sync::Arc;
use tonic::transport::Server;
use tracing::{info, Level};
use tracing::{debug, info, Level};
use utils::consul_registration;
use utils::service_discovery::get_service_address;
use utils::service_discovery::{get_service_address, get_service_endpoints_by_dns};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -29,12 +29,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string());
let port = env::var("AUTH_SERVICE_PORT").unwrap_or_else(|_| "50051".to_string());
let consul_url = env::var("CONSUL_URL").unwrap_or_else(|_| "http://127.0.0.1:8500".to_string());
let consul_address = env::var("CONSUL_ADDRESS").unwrap_or_else(|_| "127.0.0.1".to_string());
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 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);
// Register service with Consul
let service_id = consul_registration::get_or_generate_service_id(env!("CARGO_PKG_NAME"));

View File

@@ -5,6 +5,8 @@
dockerfile: Dockerfile
restart: on-failure
container_name: frontend
networks:
- frontend
ports:
- "3000:80"
env_file:
@@ -18,6 +20,8 @@
context: ./
dockerfile: ./auth-service/Dockerfile
restart: on-failure
networks:
- backend
ports:
- "50051:50051"
env_file:
@@ -37,6 +41,9 @@
context: ./
dockerfile: ./api-service/Dockerfile
restart: on-failure
networks:
- frontend
- backend
ports:
- "8080:8080"
- "8081:8081"
@@ -54,6 +61,8 @@
context: ./
dockerfile: ./database-service/Dockerfile
restart: on-failure
networks:
- backend
ports:
- "50052:50052"
env_file:
@@ -70,6 +79,8 @@
context: ./
dockerfile: ./character-service/Dockerfile
restart: on-failure
networks:
- backend
ports:
- "50053:50053"
env_file:
@@ -86,6 +97,8 @@
context: ./
dockerfile: ./world-service/Dockerfile
restart: on-failure
networks:
- backend
ports:
- "50054:50054"
env_file:
@@ -102,9 +115,12 @@
context: ./
dockerfile: ./packet-service/Dockerfile
restart: on-failure
networks:
- backend
ports:
- "29000:29000"
- "4001:4001"
- "8082:8082"
env_file:
- ./packet-service/.env
- .env
@@ -119,6 +135,8 @@
context: ./
dockerfile: ./session-service/Dockerfile
restart: on-failure
networks:
- backend
ports:
- "50055:50055"
env_file:
@@ -134,6 +152,9 @@
env_file:
- .env
restart: on-failure
networks:
backend:
ipv4_address: 172.16.238.4
ports:
- "6379:6379"
volumes:
@@ -144,6 +165,9 @@
env_file:
- .env
restart: on-failure
networks:
backend:
ipv4_address: 172.16.238.3
ports:
- "5432:5432"
volumes:
@@ -152,10 +176,21 @@
consul:
image: consul:1.15.4
command: agent -dev -client=0.0.0.0
command: [
"agent",
"-dev",
"-bootstrap-expect=1",
"-client=0.0.0.0",
"-bind=0.0.0.0",
]
restart: on-failure
networks:
backend:
ipv4_address: 172.16.238.2
ports:
- "8500:8500"
- "8600:8600/udp"
- "8600:8600/tcp"
volumes:
- ./scripts/consul.json:/consul/config/cors.json
@@ -163,3 +198,13 @@ volumes:
db_data:
cache_data:
service_ids:
networks:
backend:
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"
- subnet: "2001:3984:3989::/64"
frontend:
driver: bridge

View File

@@ -83,7 +83,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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, "tcp".to_string()];
let mut meta = HashMap::new();
consul_registration::register_service(
&consul_url,

View File

@@ -15,3 +15,4 @@ redis = "0.29.1"
deadpool-redis = "0.20.0"
async-trait = "0.1.87"
serde_json = "1.0.140"
hickory-resolver = "0.24.4"

View File

@@ -64,6 +64,12 @@ pub async fn register_service(
"port": service_port,
"tags": tags,
"meta": meta,
"tagged_addresses": {
"lan": {
"address": service_address.to_string(),
"port": service_port,
}
},
"check": {
check_protocol: check_address,
"interval": "10s",

View File

@@ -1,5 +1,32 @@
use serde::Deserialize;
use hickory_resolver::config::*;
use hickory_resolver::{Resolver, TokioAsyncResolver};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::SocketAddr;
use tokio::runtime::Runtime;
use tracing::log::debug;
pub async fn get_service_endpoints_by_dns(consul_url: &str, service_protocol: &str, service_name: &str) -> Result<Vec<SocketAddr>, Box<dyn std::error::Error>> {
let mut rc = ResolverConfig::new();
let url = consul_url.parse()?;
rc.add_name_server(NameServerConfig::new(url, Protocol::Tcp));
let resolver = TokioAsyncResolver::tokio(rc, ResolverOpts::default());
let srv_name = format!("_{}._{}.service.consul", service_name, service_protocol);
let srv_record = resolver.srv_lookup(&srv_name).await?;
let mut endpoints = Vec::new();
debug!("service records: {:?}", srv_record);
for record in srv_record {
let hostname = record.target();
debug!("hostname: {:?}", hostname);
// endpoints.push(SocketAddr::new(, record.port()));
}
Ok(endpoints)
}
#[derive(Debug, Deserialize)]
pub struct ServiceNode {
@@ -24,7 +51,7 @@ pub async fn get_service_address(
service_name,
response.status()
)
.into());
.into());
}
// Deserialize the response into a Vec<ServiceNode>
@@ -52,7 +79,7 @@ async fn get_services_with_tag(
service_name,
response.status()
)
.into());
.into());
}
// Deserialize the response into a Vec<ServiceNode>

View File

@@ -13,6 +13,7 @@ tracing-subscriber = "0.3.18"
tonic = "0.12.3"
prost = "0.13.4"
warp = "0.3.7"
tonic-health = "0.12.3"
[build-dependencies]
tonic-build = "0.12.3"

View File

@@ -2,9 +2,9 @@ use dotenv::dotenv;
use std::collections::HashMap;
use std::env;
use std::str::FromStr;
use tracing::Level;
use tracing::{debug, Level};
use utils::consul_registration;
use utils::service_discovery::get_service_address;
use utils::service_discovery::{get_service_address, get_service_endpoints_by_dns};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -21,15 +21,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let port = env::var("WORLD_SERVICE_PORT").unwrap_or_else(|_| "50054".to_string());
let health_port = env::var("HEALTH_CHECK_PORT").unwrap_or_else(|_| "8084".to_string());
let consul_url = env::var("CONSUL_URL").unwrap_or_else(|_| "http://127.0.0.1:8500".to_string());
// let consul_url = env::var("CONSUL_URL").unwrap_or_else(|_| "http://127.0.0.1:8500".to_string());
let consul_address = env::var("CONSUL_ADDRESS").unwrap_or_else(|_| "127.0.0.1".to_string());
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 service_name = env::var("SERVICE_NAME").unwrap_or_else(|_| "world-service".to_string());
let service_address =
env::var("WORLD_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);
let health_check_endpoint_addr = format!("{}:{}", service_address, health_port);
let db_nodes = get_service_address(&consul_url, "database-service").await?;
let temp_db_nodes = get_service_endpoints_by_dns(format!("{}:{}", consul_address, consul_dns_port).as_str(), "grpc", "database-service").await?;
debug!("{:?}", temp_db_nodes);
// 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();
@@ -47,7 +53,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Some("http"),
Some(&health_check_url),
)
.await?;
.await?;
// Start health-check endpoint
consul_registration::start_health_check(addr.as_str()).await?;