Adaptive Function Details
Function Overview
The Adaptive function in Dubbo is a dynamic adaptation mechanism that can automatically select and load the most suitable extension point implementation based on runtime conditions. The core of this function is solving the problem of how to dynamically switch concrete implementation classes based on different runtime environments and configurations in distributed systems.
Core Mechanism
-
Dynamic Selection Mechanism:
- Uniformly encapsulates all extension points corresponding to the specified interface through
getAdaptiveExtension()method - Uses decorator pattern to wrap extension points
- Dynamically determines which concrete implementation to use based on URL parameters at runtime
- Uniformly encapsulates all extension points corresponding to the specified interface through
-
URL-driven Mechanism:
- All registration information in Dubbo is processed in URL format
- URL format example:
dubbo://192.168.1.1:20880/com.service.DemoService?timeout=3000&loadbalance=random
Typical Application Scenarios
- Protocol Selection: Automatically select protocol implementation based on protocol parameter in URL
- Cluster Fault Tolerance: Select fault tolerance strategy based on cluster parameter
- Load Balancing: Select algorithm based on loadbalance parameter
Code Example
// Provide unified support for all extension points through getAdaptiveExtension
final WzkHelloService adaptiveExtension =
ExtensionLoader.getExtensionLoader(WzkHelloService.class).getAdaptiveExtension();
System.out.println(adaptiveExtension.sayHello(url));
Interception Operations
Dubbo Filter Mechanism Explained
Basic Working Principle
Dubbo’s Filter mechanism is an interceptor chain specifically designed for the invocation process of service providers (Provider) and consumers (Consumer). During each remote method call, registered Filters are executed in a specific order, forming a complete invocation chain.
Typical Application Scenarios
- Access Control: Implement IP whitelist/blacklist filtering
- Monitoring Statistics: Record call duration, success rate and other metrics
- Logging: Record request/response data for debugging
- Parameter Validation: Validate input parameter legitimacy
- Rate Limiting and Circuit Breaking: Implement service traffic control and circuit breaker protection
Implementation Example
public class WhitelistFilter implements Filter {
private List<String> allowedIps = Arrays.asList("192.168.1.1", "10.0.0.1");
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
String clientIp = RpcContext.getContext().getRemoteHost();
if (!allowedIps.contains(clientIp)) {
throw new RpcException("IP " + clientIp + " is not allowed");
}
return invoker.invoke(invocation);
}
}
Execution Flow
Dubbo Filter follows this execution order:
- Consumer Filter chain (from back to front)
- Network transmission
- Provider Filter chain (from front to back)
- Actual service method call
- Response returns along the original path
Custom Interception
@Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER}, order = -1000)
public class WzkLogFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
System.out.println("[Before custom WzkLogFilter]" + invocation.getMethodName());
Result result = invoker.invoke(invocation);
System.out.println("[After custom WzkLogFilter]" + invocation.getMethodName());
return result;
}
}
Summary
The combination of Adaptive and Filter is a key manifestation of Dubbo’s highly pluggable architecture design. Developers only need to implement the Filter interface and register through SPI and @Activate annotation to achieve functional enhancement. This design gives Dubbo extreme flexibility and scalability.