dubbo下的异常统一处理

dubbo下的异常统一处理

ps:lz最近写了一个统一异常处理模块,按等级高低发送给对应的负责人(该异常发生的类对应的包对应的模块),跟大家分享一下.


异常统一处理需要类去实现javax.ws.rs的ExceptionMapper接口
public interface ExceptionMapper<E extends Throwable> {

    /**
     * Map an exception to a {@link javax.ws.rs.core.Response}. Returning
     * {@code null} results in a {@link javax.ws.rs.core.Response.Status#NO_CONTENT}
     * response. Throwing a runtime exception results in a
     * {@link javax.ws.rs.core.Response.Status#INTERNAL_SERVER_ERROR} response.
     *
     * @param exception the exception to map to a response.
     * @return a response mapped from the supplied exception.
     */
    Response toResponse(E exception);
}
需要了解dubbo的ExceptionFilter的实现:
/* 
 * Copyright 1999-2011 Alibaba Group. 
 *   
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *   
 *      http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */  
package com.alibaba.dubbo.rpc.filter;  

import java.lang.reflect.Method;  

import com.alibaba.dubbo.common.Constants;  
import com.alibaba.dubbo.common.extension.Activate;  
import com.alibaba.dubbo.common.logger.Logger;  
import com.alibaba.dubbo.common.logger.LoggerFactory;  
import com.alibaba.dubbo.common.utils.ReflectUtils;  
import com.alibaba.dubbo.common.utils.StringUtils;  
import com.alibaba.dubbo.rpc.Filter;  
import com.alibaba.dubbo.rpc.Invocation;  
import com.alibaba.dubbo.rpc.Invoker;  
import com.alibaba.dubbo.rpc.Result;  
import com.alibaba.dubbo.rpc.RpcContext;  
import com.alibaba.dubbo.rpc.RpcException;  
import com.alibaba.dubbo.rpc.RpcResult;  
import com.alibaba.dubbo.rpc.service.GenericService;  

/** 
 * ExceptionInvokerFilter 
 * 

* 功能: *

    *
  1. 不期望的异常打ERROR日志(Provider端)
    * 不期望的日志即是,没有的接口上声明的Unchecked异常。 *
  2. 异常不在API包中,则Wrap一层RuntimeException。
    * RPC对于第一层异常会直接序列化传输(Cause异常会String化),避免异常在Client出不能反序列化问题。 *
* * @author william.liangf * @author ding.lid */
@Activate(group = Constants.PROVIDER) public class ExceptionFilter implements Filter { private final Logger logger; public ExceptionFilter() { this(LoggerFactory.getLogger(ExceptionFilter.class)); } public ExceptionFilter(Logger logger) { this.logger = logger; } 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(); // 如果是checked异常,直接抛出 if (! (exception instanceof RuntimeException) && (exception instanceof Exception)) { return result; } // 在方法签名上有声明,直接抛出 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; } // 未在方法签名上定义的异常,在服务器端打印ERROR日志 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 result; } // 是JDK自带的异常,直接抛出 String className = exception.getClass().getName(); if (className.startsWith("java.") || className.startsWith("javax.")) { return result; } // 是Dubbo本身的异常,直接抛出 if (exception instanceof RpcException) { return result; } // 否则,包装成RuntimeException抛给客户端 return new RpcResult(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); return result; } } return result; } catch (RuntimeException e) { 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); throw e; } } }
在此基础上,去实现ExceptionMapper抓取特定的异常,比如:
public class RpcExceptionMapperSupportWarning implements ExceptionMapper<RpcException>{
@Override
    public Response toResponse(RpcException e) {
    FailedResponse faild = null;

        if (e.getCause() instanceof ConstraintViolationException) {
            EExceptionLevel level = EExceptionLevel.LEVEL_LOW;
            ExceptionWarningFilter.FILTER.warning(e, level);
            return this.rpcMapperSupper.toResponse(e);
        }//else if(){}...
        else {//RPC Call Failure!!!
            EExceptionLevel level = EExceptionLevel.LEVEL_HIGH;
            ExceptionWarningFilter.FILTER.warning(e, level);
            faild = FailedResponse.ILLEGAL_RPC_CALL;
        }
        return Response.status(Response.Status.OK).entity(faild).type("application/json; charset=UTF-8").build();
    }
}
上述代码中的ExceptionWarningFilter类,便是处理异常的类:
public enum ExceptionWarningFilter {

    FILTER;
    private static Logger logger = LoggerFactory.getLogger(ExceptionWarningFilter.class);
    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void warning(Throwable throwable,EExceptionLevel level){
        Throwable exception = throwable.getCause();
        if (exception == null) {
            exception = throwable;
        }
        WarningMessage warningMessage = null;

        Timestamp happenTimestamp =new Timestamp(new Date().getTime());

        String exceptionStack = getExceptionStack(exception);//异常栈

        String ip = RpcContext.getContext().getLocalHost();

        if (exception instanceof RuntimeException) {
            String message = exception.getLocalizedMessage();
            if (message!=null) {
                warningMessage = getWarningInfo(message,warningMessage);
            }else{
                for(StackTraceElement el : exception.getStackTrace()){
                    warningMessage = getWarningInfo(el,exception,warningMessage);
                    break;
                }
            }
        }else {
            for (StackTraceElement el : exception.getStackTrace()) {
                if (el.getClassName().startsWith("com.java")) {
                    warningMessage = getWarningInfo(el,exception,warningMessage);
                    break;
                }       
            }
            if (warningMessage==null) {
                for(StackTraceElement el : exception.getStackTrace()){
                    warningMessage = getWarningInfo(el,exception,warningMessage);
                    break;
                }
            }
        }
        callingTheExceptionNotificationService(level,happenTimestamp,warningMessage,exceptionStack,ip);

    }
    private String getExceptionStack(Throwable exception) {
        String exceptionStack = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        exception.printStackTrace(new PrintStream(baos));
        if (baos!=null) {
            exceptionStack =  baos.toString();
        }
        return exceptionStack;

    }
    private void callingTheExceptionNotificationService(EExceptionLevel level, Timestamp happenTimestamp,
            WarningMessage warningMessage, String exceptionStack, String ip) {
        String happenTime = df.format(happenTimestamp==null?new Date().getTime():happenTimestamp);
        FileSystemXmlApplicationContext fsxac = null;
        try {
            fsxac = new FileSystemXmlApplicationContext("/server-spring.xml");
            IExceptionNotifyService exceptionNotifyService = (IExceptionNotifyService) fsxac.getBean("exceptionNotifyService");
            exceptionNotifyService.exceptionNotify(level,
                    getWarningInfo(happenTimestamp,warningMessage,exceptionStack,ip));
            logger.debug("发送异常报警信息(时间:{},info:{}",happenTime,warningMessage.toString());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.toString());
        }finally {
            if (fsxac!=null) {
                fsxac.close();                          
            }
        }
    }
    lz只贴了主要的方法,其他的就是对bean的数据的封装,就不贴了
    这里lz使用了一个枚举类来作为这个异常处理类,主要是想运用一个单例模式,这个用法可在efftive java中找到,这种写法在功能上与共有域方法相近,但是它更简洁,无偿地提供了序列化机制,绝对防止对此实例化,即使是在面对复杂的序列化或者反射攻击的时候。虽然这中方法还没有广泛采用,但是单元素的枚举类型已经成为实现Singleton的最佳方法。
    lz主要碰到的问题是,这个实现了的异常mapper并不支持spring的antowired注释,大概的解释应该是他并不会被声明为一个bean?lz的解决方法是自己去配置文件里get一个,如果有更好的解决方法欢迎评论@,这里还有一个要注意的是,dubbo的异常处理中,不符合条件的,会包装成一个runtimeException再抛出,所以lz在处理类中对 instanceof runtimeException的异常做了处理,lz的处理其实说起来很白痴,就是获取他携带的LocalizedMessage然后对其做处理,获取异常发生的类,方法,行以及对应的包名.
    之后就是根据业务需求将异常信息发送给对应负责人了,出去性能的考虑,lz和同事的方案为将该信息发送到消息队列上去,并设置该异常类型的redis缓存,用于判断相隔多少时间发送,而消费方则去收取消息队列上对应的topic数据去做处理.

你可能感兴趣的:(Java系统学习之路)