dubboにおける異常統一処理
21878 ワード
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
*
* :
*
* - ERROR (Provider )
* , Unchecked 。
* - 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 .