A high-performance HTTP router built with Netty and Spring Framework, designed for intelligent content-based request routing and load balancing.
NettySmartRouter is a lightweight, high-performance HTTP router that provides intelligent request routing capabilities based on content matching and advanced load balancing. It uses Netty for high-performance I/O operations and Spring Framework for configuration management and dependency injection.
- High Performance: Built on Netty 4.1.0 for excellent I/O performance
- Intelligent Routing: Content-based request routing with keyword matching
- Load Balancing: Multiple load balancing strategies (Round Robin, Random, Weighted Round Robin)
- Spring Integration: Full Spring Framework integration for configuration management
- Configurable: Flexible configuration through properties files
- Asynchronous Processing: Non-blocking request processing for better throughput
- Health Check Support: Optional health checking for backend servers
The router consists of several key components:
- GatewayServer: Main server class that initializes the Netty server
- GatewayServerHandler: Core request handler that processes HTTP requests with load balancing
- LoadBalancer: Load balancing engine with multiple strategies
- RoutingLoader: Spring-based configuration loader for routing rules
- HttpClientUtils: HTTP client utility for forwarding requests to backend services
- Java 6+
- Netty 4.1.0.Final
- Spring Framework 4.1.1.RELEASE
- Maven 3.3+
mvn clean packagejava -jar target/NettyGateway-1.0-SNAPSHOT.jarThe router will start on port 8999 by default.
Configure server settings and load balancing:
<bean id="options" class="com.newlandframework.gateway.commons.GatewayOptions">
<property name="gatewayPort" value="8999"/>
<!-- Load balance configuration -->
<property name="loadBalanceEnabled" value="true"/>
<property name="loadBalanceStrategy" value="roundRobin"/>
<property name="healthCheckEnabled" value="false"/>
<property name="healthCheckInterval" value="30000"/>
</bean>Configure default routing rules with load balancing support:
# Router configuration format:
# netty-gateway.configX.serverPath ==> URL path keyword
# netty-gateway.configX.defaultAddr ==> Default forwarding URL (supports multiple servers comma-separated)
# Example with load balancing (multiple servers)
netty-gateway.config1.serverPath=fcgi-bin/UIG_SFC_186
netty-gateway.config1.defaultAddr=http://10.46.158.10:8088/fcgi-bin/UIG_SFC_186,http://10.46.158.11:8088/fcgi-bin/UIG_SFC_186,http://10.46.158.12:8088/fcgi-bin/UIG_SFC_186
# Single server (no load balancing)
netty-gateway.config2.serverPath=api/health
netty-gateway.config2.defaultAddr=http://10.46.158.10:8090/api/healthConfigure specific routing rules with keyword matching and load balancing:
# Route configuration format:
# netty-gateway.configX.serverPath ==> URL path keyword
# netty-gateway.configX.keyWord ==> Content matching keywords (supports 1~N keywords, separated by commas, AND logic)
# netty-gateway.configX.matchAddr ==> Target URL when keywords match successfully (supports multiple servers comma-separated)
# Example with load balancing
netty-gateway.config1.serverPath=fcgi-bin/UIG_SFC_186
netty-gateway.config1.keyWord=1,2,3
netty-gateway.config1.matchAddr=http://10.46.158.20:8088/fcgi-bin/UIG_SFC_186,http://10.46.158.21:8088/fcgi-bin/UIG_SFC_186Configure load balancing behavior:
# Load balance configuration
gateway.loadBalance.enabled=true
gateway.loadBalance.strategy=roundRobin
# Health check configuration
gateway.loadBalance.healthCheck.enabled=false
gateway.loadBalance.healthCheck.interval=30000- Round Robin: Distributes requests evenly across all available servers
- Random: Randomly selects a server from the available server list
- Weighted Round Robin: Distributes requests based on server weights (planned)
Load balancing is automatically applied when multiple servers are specified in the configuration (comma-separated addresses). Single server configurations bypass load balancing.
# Multiple servers - load balancing applied
netty-gateway.config1.defaultAddr=http://server1:8080/api,http://server2:8080/api,http://server3:8080/api
# Single server - no load balancing
netty-gateway.config2.defaultAddr=http://server1:8080/api- Request Reception: The router receives HTTP requests on the configured port
- Path Matching: The router matches the request URI against configured server paths
- Content Analysis: For matched paths, the router analyzes request content for keyword matches
- Load Balancing: If multiple servers are configured, applies load balancing strategy
- Route Selection:
- If keywords match, forward to the specific target address (with load balancing)
- If no keywords match, forward to the default address (with load balancing)
- If no path matches, return localhost response
- Request Forwarding: The router forwards the request to the selected backend service
- Response Handling: The response from the backend is returned to the client
gatewayPort: Port number for the router server (default: 8999)loadBalanceEnabled: Enable/disable load balancing (default: true)loadBalanceStrategy: Load balancing strategy (default: roundRobin)healthCheckEnabled: Enable health checking (default: false)healthCheckInterval: Health check interval in milliseconds (default: 30000)
roundRobin: Round robin distributionrandom: Random server selectionweightedRoundRobin: Weighted round robin (planned)
The router supports two types of routing rules:
-
Default Routes: Defined in
netty-gateway.properties- Matches requests by URI path
- Supports multiple servers for load balancing
- Provides fallback destination when no specific route matches
-
Content-based Routes: Defined in
netty-route.properties- Matches requests by URI path AND content keywords
- Supports multiple keywords with AND logic
- Supports multiple servers for load balancing
- Takes precedence over default routes
- Client sends POST request to
http://localhost:8999/fcgi-bin/UIG_SFC_186 - Router matches the path
fcgi-bin/UIG_SFC_186 - Router checks request content for keywords (e.g., "1", "2", "3")
- If keywords match, applies load balancing to
http://10.46.158.20:8088/fcgi-bin/UIG_SFC_186,http://10.46.158.21:8088/fcgi-bin/UIG_SFC_186 - If no keywords match, applies load balancing to default servers
- Returns backend response to client
# Configuration with 3 servers
netty-gateway.config1.defaultAddr=http://server1:8080/api,http://server2:8080/api,http://server3:8080/apiWith Round Robin strategy:
- Request 1 → server1:8080
- Request 2 → server2:8080
- Request 3 → server3:8080
- Request 4 → server1:8080 (cycles back)
- High Throughput: Asynchronous, non-blocking I/O operations
- Low Latency: Direct memory access with Netty's zero-copy capabilities
- Scalability: Configurable thread pool based on available CPU cores
- Memory Efficiency: Efficient buffer management and object pooling
- Load Distribution: Even distribution across multiple backend servers
The router provides built-in logging for:
- Request details and content
- URL matching results
- Load balancing decisions
- Error conditions and exceptions
- System information on startup
Run the test suite to verify load balancing functionality:
mvn testThis project is open source. Please refer to the original repository for license information.