对于Java Web应用,spring的拦截器可以拦截Web接口的调用,而对于dubbo接口,Spring的拦截器就不管用了。要实现此功能,需要dubbo提供Filter
dubbo中的Filter过滤器的使用场景:
1 、IP白名单
示例:给dubbo接口添加白名单——dubbo Filter的使用
http://blog.csdn.net/mj158518/article/details/47379799
2 、soa调用异常处理
import com.alibaba.dubbo.rpc.*;
import com.#.service.channel.dto.response.ResponseResult;
import com.##.service.channel.dto.response.ResponseResult.ResponseCode;
import com.##.service.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EPayFilter implements Filter {
private static final Logger LOGGER = LoggerFactory.getLogger(EPayFilter.class);
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
Result result = invoker.invoke(invocation);
if (result.hasException()) {
Throwable exception = result.getException();
if (exception instanceof BusinessException) {
LOGGER.error("调用方法{}出现异常,请求参数为:{},异常信息为:{}", invocation.getMethodName(), invocation.getArguments(), exception.getMessage());
BusinessException e = (BusinessException) exception;
ResponseResult responseResult = new ResponseResult(e.getResponseCode(), e.getMessage());
return new RpcResult(responseResult);
}
LOGGER.error("调用方法{}出现异常,请求参数为:{}", invocation.getMethodName(), invocation.getArguments(), exception);
ResponseResult responseResult = new ResponseResult(ResponseCode.EXCEPTION, exception.getMessage());
return new RpcResult(responseResult);
}
return result;
}
}
配置参考白名单示例