Filter

In this article, we will learn how to extend custom filter implementations: a unified Filter processor that can handle and validate returned results, reducing disruptions to developers.

In the section RPC Framework - Filter Request Interception, we learned about the working mechanism of Filters and some built-in Filter implementations provided by the Dubbo framework. In this article, we will learn how to extend custom filter implementations: a unified Filter processor that can handle and validate returned results, reducing disruptions to developers.

The complete source code for this example can be found at dubbo-samples-extensibility. In addition to this example, many Filter implementations in the Dubbo core repository apache/dubbo and the extension library apache/dubbo-spi-extensions can serve as reference implementations.

Task Details

Uniformly append 's customized AppendedFilter to the returned results of all requests calling Provider services.

Implementation Method

Customize a Filter in the Provider to modify the return results.

Code Structure

  1. src
  2. |-main
  3. |-java
  4. |-org
  5. |-apache
  6. |-dubbo
  7. |-samples
  8. |-extensibility
  9. |-filter
  10. |-provider
  11. |-AppendedFilter.java (Implements Filter interface)
  12. |-resources
  13. |-META-INF
  14. |-application.properties (Dubbo Provider configuration file)
  15. |-dubbo
  16. |-org.apache.dubbo.rpc.Filter (Plain text file)

Code Details

  1. package org.apache.dubbo.samples.extensibility.filter.provider;
  2. import org.apache.dubbo.rpc.Filter;
  3. import org.apache.dubbo.rpc.Result;
  4. import org.apache.dubbo.rpc.Invoker;
  5. import org.apache.dubbo.rpc.Invocation;
  6. import org.apache.dubbo.rpc.RpcException;
  7. import org.apache.dubbo.rpc.AsyncRpcResult;
  8. public class AppendedFilter implements Filter {
  9. @Override
  10. public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
  11. Result result= invoker.invoke(invocation);
  12. // Obtain the returned value
  13. Result appResponse = ((AsyncRpcResult) result).getAppResponse();
  14. // Appended value
  15. appResponse.setValue(appResponse.getValue()+"'s customized AppendedFilter");
  16. return result;
  17. }
  18. }

SPI Configuration

Add the following configuration to the resources/META-INF/dubbo/org.apache.dubbo.rpc.Filter file:

  1. appended=org.apache.dubbo.samples.extensibility.filter.provider.AppendedFilter

Configuration File

Add the following configuration to the resources/application.properties file to activate the custom Filter implementation:

  1. # Apply AppendedFilter
  2. dubbo.provider.filter=appended

Note

In addition to activating the Filter implementation via configuration, you can also add the @Activate annotation to the implementation class to automatically activate it under certain conditions, such as:

  1. @Activate(group="provider")
  2. public class AppendedFilter implements Filter {}

This Filter implementation will be automatically activated on the Provider side.

Running Results

Running the task using a local IDE, the result is as follows:

dubbo-samples-extensibility-filter-output.jpg

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)