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.
๐ Table of Contents
- Features
- How It Works
- Architecture
- Quick Start
- Usage Examples
- Running Examples
- Configuration
- Modules
- Tech Stack
- Contributing
- License
- Support
โจ 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
๐ก How It Works
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.
Core Workflow
- Service Registration: A service sends a registration request to the NetSel server
- IP Allocation: NetSel assigns a virtual IP address and port to the service
- Heartbeat Mechanism: The service sends periodic heartbeat messages (every 10 seconds by default)
- Service Discovery: Other services can resolve the registered service's name to its virtual IP via DNS
- Traffic Routing: The built-in TCP/HTTP proxies route traffic between services
- Health Monitoring: NetSel automatically removes services that stop sending heartbeats
๐๏ธ Architecture
The NetSel system consists of several components working together:
1. Registry Server
- Manages service registration and heartbeat messages
- Maintains a list of active services
- Handles service deregistration and cleanup
2. TCP Proxy
- Routes TCP traffic between registered services
- Uses the registry to resolve service names to virtual IPs
- Provides load balancing capabilities
3. HTTP Proxy
- Routes HTTP requests between registered services
- Supports HTTP/1.1 and HTTP/2
- Maintains persistent connections
4. DNS Server
- Resolves service names to IP addresses
- Listens on a configurable port (default: 5353)
- Uses the registry as its data source
5. Virtual Network
- Manages IP address allocation for services
- Provides a simplified IP addressing scheme
- Handles IP conflicts automatically
6. Health Checker
- Monitors service health based on heartbeat messages
- Removes inactive services after a configurable timeout (default: 60 seconds)
- Runs periodic cleanup tasks
7. Service Client
- Library for services to register and send heartbeats
- Provides a simple API for service integration
- Built-in retry mechanisms for robustness
๐ Quick Start
Prerequisites
- Rust 1.75+ (with Cargo)
- Tokio runtime
Installation
Add NetSel to your Cargo.toml:
=
= { = "1.37", = ["full"] }
= { = "." }
Basic Usage
Starting the NetSel Server
use signal;
use NetSelServer;
async
Registering a Service
use ;
use FromStr;
use Duration;
use ServiceClient;
async
๐ Usage Examples
Example 1: Basic Server Setup
use NetSelServer;
async
Example 2: Custom Configuration
use ;
use ;
async
๐งช Running Examples
The project includes three example programs that demonstrate the full NetSel workflow:
1. Start the NetSel Server
2. Register a Service
3. Test the System
Example Output
# 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.
โ๏ธ Configuration
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) |
๐ฆ Modules
client
- Service client implementation for registering services and sending heartbeats
- Provides the
ServiceClientstruct for service integration
dns
- DNS server implementation for service discovery
- Resolves service names to IP addresses
- Built on trust-dns-server
network
- Virtual network implementation for IP allocation
- Manages IP address assignments for registered services
proxy
- TCP and HTTP proxy implementations for traffic routing
- Routes traffic between registered services
registry
- Service registry implementation for managing service information
- Handles registration, heartbeat, and cleanup operations
๐ ๏ธ Tech Stack
| 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 |
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
- Clone the repository
- Install dependencies:
cargo build - Run tests:
cargo test - Run examples:
cargo run --example <example_name>
Code Style
- Follow Rust's standard code style (enforced by rustfmt)
- Write comprehensive tests for new features
- Document all public APIs
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Support
For questions or issues, please open an issue on the GitHub repository.
NetSel - Making service registration and discovery simple for distributed systems.