dubbo抛出自定义异常

编程入门 行业动态 更新时间:2024-10-24 14:23:06

dubbo抛出<a href=https://www.elefans.com/category/jswz/34/1771438.html style=自定义异常"/>

dubbo抛出自定义异常

2021-08-25 更新:
更简单的方案是在接口上声明抛出自定义异常。因为自定义的异常为RuntimeException,所以调用方无需try catch。无需重写和配置ExceptionFilter,dubbo也会对这个异常继续向上抛出。


重写ExceptionFilter

package com.*.microservicemon.filter;import com.jumi.microservicemon.exception.BaseException;
import org.apache.dubbomon.constants.CommonConstants;
import org.apache.dubbomon.extension.Activate;
import org.apache.dubbomon.logger.Logger;
import org.apache.dubbomon.logger.LoggerFactory;
import org.apache.dubbomon.utils.ReflectUtils;
import org.apache.dubbomon.utils.StringUtils;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.service.GenericService;import java.lang.reflect.Method;/*** @author Dirk* @date 2020-11-07 11:39*/
@Activate(group = CommonConstants.PROVIDER)
public class ExceptionFilter implements Filter, Filter.Listener {private final Logger logger = LoggerFactory.getLogger(ExceptionFilter.class);@Overridepublic Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {return invoker.invoke(invocation);}@Overridepublic void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {// 如果有异常并且未实现GenericService接口,进入后续判断逻辑if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {try {Throwable exception = appResponse.getException();// 检查异常,直接抛出if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) {return;}// 方法签名上有说明抛出非检查异常,直接抛出try {Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());Class<?>[] exceptionClassses = method.getExceptionTypes();for (Class<?> exceptionClass : exceptionClassses) {if (exception.getClass().equals(exceptionClass)) {return;}}} catch (NoSuchMethodException e) {return;}// 自定义异常直接抛出if (exception instanceof BaseException) {return;}// 对于方法签名中未找到的异常,请在服务器日志中打印错误消息。logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + exception.getClass().getName() + ": " + exception.getMessage(), exception);// 异常类和接口类在同一jar包里,直接抛出.String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)) {return;}// JDK异常,直接抛出String className = exception.getClass().getName();if (className.startsWith("java.") || className.startsWith("javax.")) {return;}// dubbo异常,直接抛出if (exception instanceof RpcException) {return;}// 否则,包装成RuntimeException抛给客户端appResponse.setException(new RuntimeException(StringUtils.toString(exception)));} catch (Throwable e) {logger.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);}}}@Overridepublic void onError(Throwable e, Invoker<?> invoker, Invocation invocation) {logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);}
}

配置ExceptionFilter

  • 创建两级文件夹META-INF/dubbo/,添加文件org.apache.dubbo.rpc.Filter
  • 文件内容
exception=com.jumi.microservicemon.filter.ExceptionFilter

自定义异常类

public class BaseException extends RuntimeException {private static final long serialVersionUID = -2789990150758271257L;private int code;private String message;public BaseException() {}public BaseException(int code, String message) {this.code = code;this.message = message;}// BaseExceptionEnum是自定义异常枚举接口public BaseException(ExceptionEnum exceptionEnum) {this.code = exceptionEnum.getCode();this.message = exceptionEnum.getMessage();}// getter and setter
}

全局异常捕捉

/*** 全局异常处理类*/
@Component
@ControllerAdvice
@ConditionalOnWebApplication
public class GlobalExceptionHandler {private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);/*** 基础异常** @param e 异常* @return 异常结果*/@ExceptionHandler(value = BaseException.class)@ResponseBodypublic ResponseResult<Object> handleBaseException(BaseException e) {log.error("基础异常", e);return new ResponseResult<>(e.getCode(), e.getMessage());}// 其他异常捕获···
}

更多推荐

dubbo抛出自定义异常

本文发布于:2024-02-08 20:44:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1674843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   抛出   异常   dubbo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!