- update: docker compose to add custom networks
- add: get service endpoints by using consul dns
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user