Dubbo 肯定是家常便饭使用了,最近因为项目需要,对一个特定的异常,进行相关处理,并且这个异常不是checked异常,所以就没有在方法上加上异常签名,想通过消费方来直接进行异常捕获。
TODO 接下来是遇到的坑
- 泛化调用和非泛化调用的区别
- Dubbo调用Filter责任链
- 异常包装
泛化调用和非泛化调用在provider上的区别提现,GenericFilter 实现?
问题:Java Dubbo 泛化调用不会返回自定义异常
解决:
自定义两个Filter:
DubboExceptionFilter:替换原生dubbo的Exception,抛出自定义异常,用于常规调用
@Activate(group={ Constants.PROVIDER})
public class DubboExceptionFilter implements Filter {
private static final Logger LOGGER = LoggerFactory.getLogger(DubboExceptionFilter.class);
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
try {
Result result = invoker.invoke(invocation);
if (result.hasException() && GenericService.class != invoker.getInterface()) {
try {
Throwable exception = result.getException();
// directly throw if it's checked exception
if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) {
return result;
}
// directly throw if the exception appears in the signature
try {
Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
Class<?>[] exceptionClassses = method.getExceptionTypes();
for (Class<?> exceptionClass : exceptionClassses) {
if (exception.getClass().equals(exceptionClass)) {
return result;
}
}
} catch (NoSuchMethodException e) {
return result;
}
// for the exception not found i