Skip to main content

Crate netsel

Crate netsel 

Source
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:

  1. Registry Server: Manages service registration and heartbeat messages
  2. TCP Proxy: Routes TCP traffic between registered services
  3. HTTP Proxy: Routes HTTP requests between registered services
  4. DNS Server: Resolves service names to IP addresses
  5. Virtual Network: Manages IP address allocation for services
  6. Health Checker: Monitors service health based on heartbeat messages
  7. Service Client: Library for services to register and send heartbeats

§Modules

  • client: Service client implementation for registering services and sending heartbeats
  • dns: DNS server implementation for service discovery
  • network: Virtual network implementation for IP allocation
  • proxy: TCP and HTTP proxy implementations for traffic routing
  • registry: Service registry implementation for managing service information

Modules§

client
Service client implementation for NetSel
dns
network
proxy
registry

Structs§

NetSelConfig
Main NetSel server configuration
NetSelServer
NetSel server instance