The Filter dynamically intercepts requests or responses to transform or utilize the information contained within them. Filters themselves do not typically create responses but provide generic functions that can be “attached” to any RPC request. Dubbo Filters are pluggable, allowing us to insert any type and number of Filters into an RPC request.
The operation of a Filter is illustrated as follows:
Some typical capabilities achievable through Filters include:
As shown in the figure above, the Dubbo proxy automatically loads Filter implementations and assembles them into the call chain. Filter is a standard SPI definition, and the framework automatically loads Filter implementations based on certain activation rules.
The default activation status of a Filter can be set in the definition using the @Activate
annotation, as shown in the following definition, which indicates that this Filter is automatically enabled when executing RPC requests on the provider side (not enabled on the consumer side). The @Activate
annotation supports various conditional controls, including enabling it when a specific class is present in the classpath, or when certain parameter values are present in the URL, more details can be referenced in SPI Extension Activate Introduction.
To disable loading for a specific Filter without modifying its definition, you can use the following configurations.
Globally disable filters, so all RPC calls do not enable filters:
Skip filter execution for a specific service call:
To enable loading for a specific Filter without modifying its definition, you can use the following configurations.
Globally enable filters, so all RPC calls will enable filters:
Execute the specified filter for a specific service call:
The following are some built-in Filter implementations in the Dubbo framework, serving as underlying implementation principles for certain features, which most users typically do not need to be concerned about. They are listed here for reference, to help users understand how to enable certain specific functionalities and the working principles behind them:
The following are details regarding the specific definitions and implementations of Filters, which can serve as references for users extending Filters.
The definition of Dubbo Filter is as follows:
Based on the above BaseFilter definition, Dubbo defines two SPI interfaces: ClusterFilter and Filter. The effects achievable by these two SPI implementations are essentially the same, but the reason for defining two is mainly for performance optimization. It is recommended that users focus on the Filter SPI and only consider ClusterFilter under strict performance requirements (such as a large number of cluster provider instances).
Refer to Usage Tutorial - Custom Extensions for specific examples.