用来在服务响应返回到客户端之前进行额外处理。
一、定义Filter
package cn.edu.tju.config;
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注入Filter
org.apache.dubbo.rpc.Filter的内容如下:
appended=cn.edu.tju.config.AppendedFilter
三、配置文件中指定Filter
server:
port: 9953
spring:
application:
name: dubbosky
dubbo:
application:
name: dubbosky
protocol:
name: dubbo
port: 28030
registry:
address: nacos://xx.xx.xx.xx:8848
provider:
filter: appended