目前公司的方案是用mdc来实现一个请求的业务数据埋点记录,但是mdc是map方式,需要手动设置key,而且每次都要手动clear,一是不方便管理,再者如果忘记clear会造成业务埋点数据混乱。所以有了想要把埋点数据字段统一封装的想法,这样方便维护,后面如果修改,也很容易找到所有修改点。于是我就想把封装好的bean放到mdc里,然后再用切面统一处理,但是,研究发现,mdc的value不支持object只能用String,很失望。我顺手翻了一下mdc的源码,如下
MDC的put方法源码
private InheritableThreadLocal<Map<String, String>> inheritableThreadLocal = new InheritableThreadLocal<Map<String, String>>() {
protected Map<String, String> childValue(Map<String, String> parentValue) {
return parentValue == null?null:new HashMap(parentValue);
}
};
public BasicMDCAdapter() {
}
public void put(String key, String val) {
if(key == null) {
throw new IllegalArgumentException("key cannot be null");
} else {
Map<String, String> map = (Map)this.inheritableThreadLocal.get();
if(map == null) {
map = new HashMap();
this.inheritableThreadLocal.set(map);
}
((Map)map).put(key, val);
}
}
发现是用ThreadLocal实现的,它直接把里面的对象定义成map,我想,那我是不是可以定义成我自己的日志实体,开始动手,写了一个简单的类,如下
public class BusinessLogUtil {
private final static ThreadLocal threadLocal = new InheritableThreadLocal<>();
public static BusinessLog getThreadLocalLog() {
if (threadLocal.get() == null) {
return new BusinessLog();
}
return threadLocal.get();
}
public static void setThreadLocalLog(final BusinessLog businessLog) {
threadLocal.set(businessLog);
}
public static void remove() {
threadLocal.remove();
}
}
好了,操作类实现完了,来实现一下统一的前置和后置操作,刚开始用aspectj实现的,但是发现,不是在同一线程里操作的,具体原理不细说。于是换成HandlerInterceptor,具体代码如下
@Slf4j
public class BusinessLogInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(final HttpServletRequest httpServletRequest,
final HttpServletResponse httpServletResponse, final Object o) throws Exception {
final BusinessLog businessLog = new BusinessLog();
businessLog.setUserId(String.valueOf(Math.random()));
BusinessLogUtil.setThreadLocalLog(businessLog);
log.info("开始访问:" + BusinessLogUtil.getThreadLocalLog().toString());
return true;// 只有返回true才会继续向下执行,返回false取消当前请求
}
@Override
public void postHandle(final HttpServletRequest httpServletRequest,
final HttpServletResponse httpServletResponse, final Object o,
final ModelAndView modelAndView)
throws Exception {
}
@Override
public void afterCompletion(final HttpServletRequest httpServletRequest,
final HttpServletResponse httpServletResponse, final Object o, final Exception e)
throws Exception {
log.info("访问结束:" + BusinessLogUtil.getThreadLocalLog().toString());
BusinessLogUtil.remove();
log.info("清理结束:" + BusinessLogUtil.getThreadLocalLog().toString());
}
}
测试结果如下
[2017-12-01T11:25:40,015] | INFO | com.aaa.interceptor.BusinessLogInterceptor.preHandle(BusinessLogInterceptor.java:23) | http-nio-8080-exec-1 开始访问:BusinessLog(userId=0.1528058009781007, type=null, message=null, phone=null)
[2017-12-01T11:25:40,033] | INFO | com.aaa.api.rest.IntegrateController.getIntegrateList(IntegrateController.java:70) | http-nio-8080-exec-1访问controller:BusinessLog(userId=0.1528058009781007, type=null, message=null, phone=null)
[2017-12-01T11:25:40,033] | INFO | com.aaa.api.rest.IntegrateController.getIntegrateList(IntegrateController.java:74) | http-nio-8080-exec-1 设置controller变量:BusinessLog(userId=0.1528058009781007, type=integrate, message=null, phone=null)
[2017-12-01T11:25:40,033] | INFO | com.aaa.service.impl.BaseServiceImpl.getBatchList(BaseServiceImpl.java:39) | http-nio-8080-exec-1 访问service:BusinessLog(userId=0.1528058009781007, type=integrate, message=null, phone=null)
[2017-12-01T11:25:40,034] | INFO | com.aaa.service.impl.BaseServiceImpl.getBatchList(BaseServiceImpl.java:43) | http-nio-8080-exec-1 设置service变量:BusinessLog(userId=0.1528058009781007, type=integrate, message=null, phone=18600732726)
[2017-12-01T11:25:41,024] | INFO | com.aaa.oplog.OpLogAspect.logEvent(OpLogAspect.java:75) | http-nio-8080-exec-1 EventParameter{source='com.aaa.api.rest.IntegrateController', level='INFO', name='获取整合请求列表', desc='', arguments='ListBatchRequest(batchId=null, batchNo=null, state=null, createPerson=null, updateTime=null, createTime=null, type=null, pageNum=1, pageSize=10)', message='', method='getIntegrateList', url='http://localhost:8080/api/online-tickets/v1/integrate'}
[2017-12-01T11:25:41,066] | INFO | com.aaa.interceptor.BusinessLogInterceptor.afterCompletion(BusinessLogInterceptor.java:39) | http-nio-8080-exec-1 访问结束:BusinessLog(userId=0.1528058009781007, type=integrate, message=null, phone=18600732726)
[2017-12-01T11:25:41,067] | INFO | com.aaa.interceptor.BusinessLogInterceptor.afterCompletion(BusinessLogInterceptor.java:41) | http-nio-8080-exec-1 清理结束:BusinessLog(userId=null, type=null, message=null, phone=null)
[2017-12-01T11:33:18,880] | INFO | com.aaa.interceptor.BusinessLogInterceptor.preHandle(BusinessLogInterceptor.java:23) | http-nio-8080-exec-3 开始访问:BusinessLog(userId=0.203277605749153, type=null, message=null, phone=null)
[2017-12-01T11:33:18,881] | INFO | com.aaa.api.rest.IntegrateController.getIntegrateList(IntegrateController.java:70) | http-nio-8080-exec-3 访问controller:BusinessLog(userId=0.203277605749153, type=null, message=null, phone=null)
[2017-12-01T11:33:18,882] | INFO | com.aaa.api.rest.IntegrateController.getIntegrateList(IntegrateController.java:74) | http-nio-8080-exec-3 设置controller变量:BusinessLog(userId=0.203277605749153, type=integrate, message=null, phone=null)
[2017-12-01T11:33:18,882] | INFO | com.aaa.service.impl.BaseServiceImpl.getBatchList(BaseServiceImpl.java:39) | http-nio-8080-exec-3 访问service:BusinessLog(userId=0.203277605749153, type=integrate, message=null, phone=null)
[2017-12-01T11:33:18,882] | INFO | com.aaa.service.impl.BaseServiceImpl.getBatchList(BaseServiceImpl.java:43) | http-nio-8080-exec-3 设置service变量:BusinessLog(userId=0.203277605749153, type=integrate, message=null, phone=18600732726)
[2017-12-01T11:33:18,925] | INFO | com.aaa.oplog.OpLogAspect.logEvent(OpLogAspect.java:75) | http-nio-8080-exec-3 EventParameter{source='com.aaa.api.rest.IntegrateController', level='INFO', name='获取整合请求列表', desc='', arguments='ListBatchRequest(batchId=null, batchNo=null, state=null, createPerson=null, updateTime=null, createTime=null, type=null, pageNum=1, pageSize=10)', message='', method='getIntegrateList', url='http://localhost:8080/api/online-tickets/v1/integrate'}
[2017-12-01T11:33:18,931] | INFO | com.aaa.interceptor.BusinessLogInterceptor.afterCompletion(BusinessLogInterceptor.java:39) | http-nio-8080-exec-3 访问结束:BusinessLog(userId=0.203277605749153, type=integrate, message=null, phone=18600732726)
[2017-12-01T11:33:18,931] | INFO | com.aaa.interceptor.BusinessLogInterceptor.afterCompletion(BusinessLogInterceptor.java:41) | http-nio-8080-exec-3 清理结束:BusinessLog(userId=null, type=null, message=null, phone=null)
发现没有问题,每次访问的数据可以在整个请求区间内保存,在访问结束后,被清理掉,不会影响其它请求。