Custom Extensions
Dubbo’s Extensibility Design Philosophy
- Equality towards third-party implementations: In Dubbo, all internal and external implementations are treated equally. Users can replace Dubbo’s native implementations based on their business requirements.
- Each extension point encapsulates only one variable factor for maximum reuse: The implementer of each extension point is often only concerned with one thing. If users need to extend functionality, they only need to extend the relevant extension points, significantly reducing the workload.
Java SPI-based Extensibility Design
Dubbo’s extensibility comes from an enhanced version of the JDK standard SPI (Service Provider Interface). It improves upon the following issues with the JDK’s standard SPI:
- The standard SPI instantiates all extension points at once, wasting resources if some are not used.
- If extension point loading fails, even the name of the extension point cannot be retrieved.
Dubbo’s extensibility features:
- On-demand loading: Dubbo doesn’t instantiate all implementations at once, reducing resource waste.
- IOC (Inversion of Control) capabilities: Dubbo not only discovers extension service implementation classes but also automatically injects dependent objects into the extension class.
- AOP (Aspect-Oriented Programming) capabilities: Dubbo automatically discovers wrapper classes for extensions, constructing them to enhance extension functionality.
- Dynamic extension selection: Dubbo dynamically chooses the corresponding extension class based on parameters at runtime.
- Extension implementation sorting: Users can specify the execution order of extension implementations.
- Adaptive extension points: Extensions can be made effective on either the consumer or provider side.
Extension Loading Process
Dubbo’s extension loading process is as follows:
The main steps are:
- Read and parse the configuration file
- Cache all extension implementations
- Instantiate the corresponding extension implementation based on the user-executed extension name
- Perform IOC injection for extension instance attributes and instantiate extension wrapper classes for AOP features
Task Items
Next, let’s introduce Dubbo’s extensibility features through the following task items.
Custom Filters
Dynamically load custom filters through the SPI mechanism to uniformly handle, validate returned results, and minimize developer interruptions.
Custom Routing
Customize routing strategies during service invocation based on actual use-cases to effectively improve service throughput and latency.
Custom Protocols
Use custom transport protocols for different heterogeneous systems to eliminate the differences between protocols and facilitate system integration.
Custom Registry Centers
Include services from different registry centers into the Dubbo ecosystem. Customizing registry centers is a key to bridging different service ecosystems.
Dubbo’s extensibility design aims to provide users with a highly flexible extension mechanism, offering features like dynamic extension selection, IOC, and AOP. This makes it easier to adapt Dubbo to various scenarios and integrate it into different technology stacks.