zoukankan      html  css  js  c++  java
  • Dubbo中记录请求日志实现方式

      Dubbo中AccessLogFilter实现了记录请求日志的功能,在业务系统中,我们也可以借鉴Dubbo的实现原理,实现相应的业务功能。

      Dubbo中AccessLogFilter的实现原理:利用ConcurrentMap作为请求记录的本地存储结构,key为日志文件,value为请求记录;利用ScheduledExecutorService.scheduleWithFixedDelay间隔执行任务,将请求记录写入日志文件中;每次请求,都将请求记录添加到ConcurrentMap中。关键性代码如下:

      private final ConcurrentMap<String, Set<String>> logQueue = new ConcurrentHashMap<String, Set<String>>();
    
        private final ScheduledExecutorService logScheduled = Executors.newScheduledThreadPool(2, new NamedThreadFactory("Dubbo-Access-Log", true));
    
        private volatile ScheduledFuture<?> logFuture = null;
    
        private void init() {
            if (logFuture == null) {
                synchronized (logScheduled) {
                    if (logFuture == null) {
                        logFuture = logScheduled.scheduleWithFixedDelay(new LogTask(), LOG_OUTPUT_INTERVAL, LOG_OUTPUT_INTERVAL, TimeUnit.MILLISECONDS);
                    }
                }
            }
        }
    
        private void log(String accesslog, String logmessage) {
            init();
            Set<String> logSet = logQueue.get(accesslog);
            if (logSet == null) {
                logQueue.putIfAbsent(accesslog, new ConcurrentHashSet<String>());
                logSet = logQueue.get(accesslog);
            }
            if (logSet.size() < LOG_MAX_BUFFER) {
                logSet.add(logmessage);
            }
        }

      假如我们需要记录业务系统的操作日志,也可以借鉴Duboo的这种实现方式(数据同步记录到ConcurrentMap中,定时任务异步执行更新操作)。当然还可以使用一些消息中间件RocketMQ,或者Disruptor,异步处理。

      希望能给大家一些启发。

  • 相关阅读:
    【转】对象持久化与数据序列化的联系?
    【转】Linux安装方法一(U盘引导)
    bash中的"-n"、"-z" 以及“[]” 、“[[]]”判断
    mysql获取行号
    IP白名单
    复合赋值位运算符“&=、| =”
    Java匿名内部类访问外部
    mysql的orde by 按照指定状态顺序排序
    Spring声明式事务
    定时任务总结
  • 原文地址:https://www.cnblogs.com/dushenzi/p/12439663.html
Copyright © 2011-2022 走看看