Expand description
NetSel - Service Registration and Discovery System
A lightweight, Rust-based service registration and discovery system designed for distributed applications.
NetSel provides a simple yet powerful way to register services, discover them through a DNS server, and route traffic between them using built-in proxies. It’s built with Tokio for high performance and reliability.
§Features
- Service Registration: Simple API for services to register themselves
- Heartbeat Mechanism: Automatic service health monitoring
- DNS Resolution: Service name to IP address resolution
- TCP and HTTP Proxies: Built-in traffic routing
- Virtual Network: Simplified IP address management
- Concurrent Design: Built with Tokio for high performance
- Fault Tolerance: Graceful handling of service failures
§Getting Started
ⓘ
use tokio::signal;
use netsel::NetSelServer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create and start NetSel server with default configuration
let server = NetSelServer::new();
server.start().await?;
// Wait for shutdown signal
signal::ctrl_c().await?;
println!("Shutting down NetSel Service...");
Ok(())
}§Service Client Example
ⓘ
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;
use tokio::time::Duration;
use netsel::client::ServiceClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let service_a_addr = SocketAddr::from_str("127.0.0.1:9000")?;
let hostname = "my-service";
// Create service client
let mut client = ServiceClient::new(service_a_addr, hostname.to_string());
// Register with Service A
let (assigned_ip, assigned_port) = client.register().await?;
let assigned_addr = SocketAddr::new(assigned_ip, assigned_port);
println!("Successfully registered! Assigned address: {}", assigned_addr);
// Start heartbeats
let heartbeat_client = client.clone();
tokio::spawn(async move {
loop {
if let Err(e) = heartbeat_client.send_heartbeat().await {
eprintln!("Heartbeat failed: {}", e);
}
tokio::time::sleep(Duration::from_secs(10)).await;
}
});
// ... start your service logic
Ok(())
}§Architecture
The NetSel system consists of several components working together:
- Registry Server: Manages service registration and heartbeat messages
- TCP Proxy: Routes TCP traffic between registered services
- HTTP Proxy: Routes HTTP requests between registered services
- DNS Server: Resolves service names to IP addresses
- Virtual Network: Manages IP address allocation for services
- Health Checker: Monitors service health based on heartbeat messages
- Service Client: Library for services to register and send heartbeats
§Modules
client: Service client implementation for registering services and sending heartbeatsdns: DNS server implementation for service discoverynetwork: Virtual network implementation for IP allocationproxy: TCP and HTTP proxy implementations for traffic routingregistry: Service registry implementation for managing service information
Modules§
Structs§
- NetSel
Config - Main NetSel server configuration
- NetSel
Server - NetSel server instance