Filter

自定义过滤器

通过自定义过滤器,可以对返回的结果进行统一的处理、验证等,减少对开发人员的打扰。

开始之前

有两种部署运行方式,二选一

基于Kubernetes

  • 安装Kubernetes环境
  • 修改Provider中的配置文件,启用Kubernetes中部署的nacos的地址
    # Specify the application name of Dubbo
    dubbo.application.name=extensibility-filter-provider
    
    # Enable token verification for each invocation
    dubbo.provider.token=true
    
    # Specify the registry address
    # dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos
    # 启用Kubernetes中部署的nacos的地址
    dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
    
    # Specify the port of Dubbo protocol
    dubbo.protocol.port=20881
    
    # Apply AppendedFilter
    dubbo.provider.filter=appended
    
  • 修改Consumer中的配置文件,启用Kubernetes中部署的nacos的地址
    # Specify the application name of Dubbo
    dubbo.application.name=extensibility-filter-consumer
    
    # Enable token verification for each invocation
    dubbo.provider.token=true
    
    # Specify the registry address
    # dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos
    # 启用Kubernetes中部署的nacos的地址
    dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
    
  • 部署[Extensibility Filter Task](https://github.com/apache/dubbo-samples/blob/master/10-task/dubbo-samples-extensibility/deploy/All.yml)

使用本地IDE

  • 部署Nacos2.2.0版本
  • 修改Provider中的配置文件,启用本地nacos的地址
    # Specify the application name of Dubbo
    dubbo.application.name=extensibility-filter-provider
    
    # Enable token verification for each invocation
    dubbo.provider.token=true
    
    # Specify the registry address
    # 启用本地nacos的地址
    dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos
    # dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
    
    # Specify the port of Dubbo protocol
    dubbo.protocol.port=20881
    
    # Apply AppendedFilter
    dubbo.provider.filter=appended
    
  • 修改Consumer中的配置文件,启用本地nacos的地址
    # Specify the application name of Dubbo
    dubbo.application.name=extensibility-filter-consumer
    
    # Enable token verification for each invocation
    dubbo.provider.token=true
    
    # Specify the registry address
    # 启用本地nacos的地址
    dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos
    # dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
    

任务详情

对所有调用Provider服务的请求在返回的结果的后面统一添加's customized AppendedFilter

实现方式

在Provider中自定义一个Filter,在Filter中修改返回结果。

代码结构

src
 |-main
    |-java
        |-org
            |-apache
                |-dubbo
                    |-samples
                        |-extensibility
                            |-filter
                                |-provider
                                    |-AppendedFilter.java (实现Filter接口)
    |-resources
        |-META-INF
            |-application.properties (Dubbo Provider配置文件)
            |-dubbo
                |-org.apache.dubbo.rpc.Filter (纯文本文件)

代码详情

package org.apache.dubbo.samples.extensibility.filter.provider;

import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.AsyncRpcResult;

public class AppendedFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        Result result= invoker.invoke(invocation);
        // Obtain the returned value
        Result appResponse = ((AsyncRpcResult) result).getAppResponse();
        // Appended value
        appResponse.setValue(appResponse.getValue()+"'s customized AppendedFilter");
        return result;
    }
}

SPI配置

resources/META-INF/dubbo/org.apache.dubbo.rpc.Filter文件中添加如下配置:

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

配置文件

resources/application.properties文件中添加如下配置:

# Apply AppendedFilter
dubbo.provider.filter=appended

运行结果

使用本地IDE的方式来运行任务,结果如下:

dubbo-samples-extensibility-filter-output.jpg


最后修改 April 11, 2023: Update filter.md (#2481) (26e1680023)