Routing Rules
Routing rules in Dubbo are an important mechanism for implementing traffic control and distribution in microservices architecture. They determine which target service provider instances client requests will be routed to.
Core Concepts of Routing Rules
Routing rules are essentially a set of conditional judgment logic, containing the following key elements:
- Match Condition: Defines which service calls need to apply the routing rule
- Filter Condition: Specifies conditions that target service providers need to satisfy
- Priority: Determines the execution order of rules
- Routing Strategy: Determines how qualified service providers are selected
Common Routing Rule Types
1. Tag Routing
Routes distribution based on service provider tags, commonly used for grayscale releases.
tagRouter=consumerTag=test => providerTag=test
2. Condition Routing
Routes based on conditions like method name and parameters:
conditionRouter=method=findUser => host=192.168.1.*
3. Script Routing
Uses scripting languages to define complex routing logic:
scriptRouter=JavaScript (host.contains("192.168.1")) => host.contains("192.168.1")
Routing Rule Execution Flow
- Rule Parsing: Load and parse routing rule configuration when Dubbo starts
- Rule Matching: Match applicable routing rules based on call information
- Provider Filtering: Filter available service provider list according to rule conditions
- Load Balancing: Apply load balancing strategy within the filtered provider list
- Request Sending: Send request to the selected service provider instance
Typical Application Scenarios
- Grayscale Release: Guide part of user traffic to new version service through tag routing
- Machine Room Routing: Route requests to service providers in the same machine room based on caller location
- Traffic Isolation: Isolate test traffic and production traffic
- Fault Isolation: Automatically remove faulty instances from routing list
- Multi-version Management: Simultaneously maintain multiple service versions and route requests on demand
Quick Start
// Define your own rules
String rule = "consumer.host != 127.0.0.1";
String encodedRule = URLEncoder.encode(rule, "UTF-8");
String routerUrl = String.format(
"condition://localhost/%s?category=routers&router=condition&rule=%s",
serviceInterface, encodedRule
);
URL conditionUrl = URL.valueOf(routerUrl);
registry.register(conditionUrl);
System Integration and Grayscale Release Mechanism Explained
Role and Necessity of Release System
When company business develops to medium scale or above (usually exceeding 50 servers or daily active users exceeding one million), establishing a professional release system becomes particularly important.
Graceful Shutdown Solution
public void shutdown() {
// 1. Remove this machine from routing rules
registry.unregister(serviceName, currentIP);
// 2. Wait for existing requests to complete (default 30 seconds)
Thread.sleep(30000);
// 3. Force close remaining long connections
forceShutdown();
}
Best Practices During Release Process
- Cold Start Protection: Delay registration after service startup
# Wait 30 seconds before registering service
dubbo.service.delay=30000
-
Progressive Traffic Access: During initial phase, allocate 5% traffic through tag routing
-
Warm-up Period Configuration:
<dubbo:reference>
<dubbo:parameter key="warmup" value="60000"/>
<dubbo:parameter key="weight" value="50"/>
</dubbo:reference>
Indicates that the new node gradually increases from 50% weight to 100% within 60 seconds.