| Crates.io | netsel |
| lib.rs | netsel |
| version | 0.1.0 |
| created_at | 2026-01-16 18:08:40.16005+00 |
| updated_at | 2026-01-16 18:08:40.16005+00 |
| description | A lightweight, Rust-based service registration and discovery system |
| homepage | |
| repository | https://github.com/jrtxreal/netsel |
| max_upload_size | |
| id | 2049065 |
| size | 83,357 |
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.
NetSel operates on a simple yet robust principle: services register themselves with a central registry, receive a virtual IP address, and then periodically send heartbeat messages to maintain their registration. Other services can discover registered services through DNS resolution or by querying the registry directly.
The NetSel system consists of several components working together:
Add NetSel to your Cargo.toml:
dependencies =
tokio = { version = "1.37", features = ["full"] }
netsel = { path = "." }
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(())
}
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(())
}
use netsel::NetSelServer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let server = NetSelServer::new();
server.start().await?;
Ok(())
}
use std::net::{IpAddr, SocketAddr};
use netsel::{NetSelConfig, NetSelServer};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = NetSelConfig {
registry_addr: SocketAddr::new(IpAddr::from([0, 0, 0, 0]), 9000),
tcp_proxy_addr: SocketAddr::new(IpAddr::from([0, 0, 0, 0]), 8080),
http_proxy_addr: SocketAddr::new(IpAddr::from([0, 0, 0, 0]), 8081),
dns_addr: SocketAddr::new(IpAddr::from([127, 0, 0, 1]), 5353),
health_check_interval: 30,
max_heartbeat_age: 60,
};
let server = NetSelServer::with_config(config);
server.start().await?;
Ok(())
}
The project includes three example programs that demonstrate the full NetSel workflow:
cargo run --example netsel_server
cargo run --example service_b
cargo run --example test_client
# netsel_server output
Starting NetSel Service...
NetSel Service started successfully!
- Registration server: 0.0.0.0:9000
- TCP proxy: 0.0.0.0:8080
- HTTP proxy: 0.0.0.0:8081
- DNS server: 127.0.0.1:5353
# service_b output
Registering service 'test-service-3' with Service A at 127.0.0.1:9000
Registration response: SUCCESS|10.0.0.100|9000|86400
Successfully registered! Assigned address: 10.0.0.100:9000
Starting echo server on 127.0.0.1:11000 (local testing)
# test_client output
Testing NetSel system...
Test 1: Testing Service B's echo server directly...
Sending test data: Hello, NetSel Service B!
Received response: Hello, NetSel Service B!
โ Echo server test passed!
Test 2: Checking Service A logs...
โ Service A is receiving heartbeats from test-service-3
โ Heartbeat mechanism test passed!
Test 3: Checking service registration...
โ Service 'test-service-3' is registered successfully
โ Service registration test passed!
๐ All tests passed! NetSel system is working correctly.
NetSel can be configured using the NetSelConfig struct. Here are the available configuration options:
| Option | Default Value | Description |
|---|---|---|
registry_addr |
0.0.0.0:9000 |
Address for the registration server |
tcp_proxy_addr |
0.0.0.0:8080 |
Address for the TCP proxy |
http_proxy_addr |
0.0.0.0:8081 |
Address for the HTTP proxy |
dns_addr |
127.0.0.1:5353 |
Address for the DNS server |
health_check_interval |
30 |
Health check interval in seconds |
max_heartbeat_age |
60 |
Maximum allowed time since last heartbeat before removing a service (seconds) |
clientServiceClient struct for service integrationdnsnetworkproxyregistry| Technology | Purpose |
|---|---|
| Rust | Core programming language |
| Tokio | Async runtime for concurrency |
| Hyper | HTTP server and client library |
| trust-dns | DNS server implementation |
| socket2 | Low-level socket operations |
Contributions are welcome! Please feel free to submit a Pull Request.
cargo buildcargo testcargo run --example <example_name>This project is licensed under the MIT License - see the LICENSE file for details.
For questions or issues, please open an issue on the GitHub repository.
NetSel - Making service registration and discovery simple for distributed systems.