ELK+MDC追踪日志 ---- Dubbo的Filter实现
程序员文章站
2022-03-05 08:07:53
...
由于在项目中,会通过dubbo远程调用其他服务,如果没有唯一标识,要追终被调用的服务执行日志很困难,所以这里实现下dubbo的日志追终。除了Filter实现不一样,其他都同https://editor.csdn.net/md/?articleId=108078745
package com.sakura.framework.filter;
import com.alibaba.dubbo.common.extension.Activate;
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.RpcException;
import com.alibaba.fastjson.JSON;
import com.fuiou.crm.framework.utils.LogWriter;
import java.util.UUID;
import org.slf4j.MDC;
@Activate
public class MonitorArgFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
try {
MDC.put("uuid", UUID.randomUUID().toString().replaceAll("-", ""));
LogWriter.info("path:" + invocation.getAttachments().get("path"));
LogWriter.info("method:" + invocation.getMethodName());
LogWriter.info("arg:" + JSON.toJSONString(invocation.getArguments()));
long stTime = System.currentTimeMillis();
Result result = invoker.invoke(invocation);
LogWriter.info("result:" + JSON.toJSONString(result.getValue()));
LogWriter.info("consume timeMillis:" + (System.currentTimeMillis() - stTime));
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}