聊聊PowerJob的DatabaseMonitorAspect

本文主要研究一下PowerJob的DatabaseMonitorAspect

DatabaseMonitorAspect

tech/powerjob/server/persistence/monitor/DatabaseMonitorAspect.java

@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class DatabaseMonitorAspect {

    private final MonitorService monitorService;

    @Around("execution(* tech.powerjob.server.persistence.remote.repository..*.*(..))")
    public Object monitorCoreDB(ProceedingJoinPoint joinPoint) throws Throwable {
        return wrapperMonitor(joinPoint, DatabaseType.CORE);
    }

    @Around("execution(* tech.powerjob.server.persistence.local..*.*(..))")
    public Object monitorLocalDB(ProceedingJoinPoint joinPoint) throws Throwable {
        return wrapperMonitor(joinPoint, DatabaseType.LOCAL);
    }

    //......
}    

DatabaseMonitorAspect拦截了remote和local的repository,然后执行wrapperMonitor

wrapperMonitor

    private Object wrapperMonitor(ProceedingJoinPoint point, DatabaseType type) throws Throwable {
        String classNameMini = AOPUtils.parseRealClassName(point);
        final String methodName = point.getSignature().getName();

        DatabaseEvent event = new DatabaseEvent().setType(type)
                .setServiceName(classNameMini)
                .setMethodName(methodName)
                .setStatus(DatabaseEvent.Status.SUCCESS);

        long startTs = System.currentTimeMillis();
        try {
            final Object ret = point.proceed();
            event.setRows(parseEffectRows(ret));
            return ret;
        } catch (Throwable t) {
            event.setErrorMsg(t.getClass().getSimpleName()).setStatus(DatabaseEvent.Status.FAILED);
            throw t;
        } finally {
            long cost = System.currentTimeMillis() - startTs;
            monitorService.monitor(event.setCost(cost));
        }
    }

wrapperMonitor主要是构建DatabaseEvent,成功时获取effectRows,异常时获取erroMsg,最后计算耗时,执行monitorService.monitor

MonitorService

tech/powerjob/server/monitor/MonitorService.java

public interface MonitorService {
    void monitor(Event event);
}

MonitorService定义了monitor方法

PowerJobMonitorService

tech/powerjob/server/monitor/PowerJobMonitorService.java

@Slf4j
@Component
public class PowerJobMonitorService implements MonitorService {

    private final List<Monitor> monitors = Lists.newLinkedList();

    public PowerJobMonitorService(List<Monitor> monitors) {
        monitors.forEach(m -> {
            log.info("[MonitorService] register monitor: {}", m.getClass().getName());
            this.monitors.add(m);
        });
    }

    @Override
    public void monitor(Event event) {
        monitors.forEach(m -> m.record(event));
    }
}

PowerJobMonitorService是实现了MonitorService接口,其monitor方法遍历monitors,挨个执行record方法

Monitor

tech/powerjob/server/monitor/Monitor.java

public interface Monitor {

    /**
     * 全局上下文绑定 & 初始化
     */
    void init();
    /**
     * 记录监控事件
     * 请注意该方法务必异步不阻塞!!!
     * @param event 事件
     */
    void record(Event event);
}

Monitor接口定义了record方法

LogMonitor

tech/powerjob/server/monitor/monitors/LogMonitor.java

@Component
public class LogMonitor implements Monitor, ServerInfoAware {

    /**
     * server 启动依赖 DB,DB会被 monitor,因此最初的几条 log serverInfo 一定为空,在此处简单防空
     */
    private ServerInfo serverInfo = new ServerInfo();

    private static final String MDC_KEY_SERVER_ID = "serverId";


    @Override
    public void init() {
    }

    @Override
    public void record(Event event) {
        MDC.put(MDC_KEY_SERVER_ID, String.valueOf(serverInfo.getId()));
        LoggerFactory.getLogger(event.type()).info(event.message());
    }

    @Override
    public void setServerInfo(ServerInfo serverInfo) {
        this.serverInfo = serverInfo;
    }
}

LogMonitor实现了Monitor, ServerInfoAware接口,其record方法通过logger打印event的message

小结

DatabaseMonitorAspect拦截了remote和local的repository,然后执行wrapperMonitor;wrapperMonitor主要是构建DatabaseEvent,成功时获取effectRows,异常时获取erroMsg,最后计算耗时,执行monitorService.monitor;LogMonitor的record方法通过logger打印event的message。

相关推荐
2401_854391084 分钟前
城镇住房保障:SpringBoot系统功能概览
java·spring boot·后端
hummhumm5 分钟前
Oracle 第29章:Oracle数据库未来展望
java·开发语言·数据库·python·sql·oracle·database
wainyz14 分钟前
Java NIO操作
java·开发语言·nio
工业3D_大熊20 分钟前
【虚拟仿真】CEETRON SDK在船舶流体与结构仿真中的应用解读
java·python·科技·信息可视化·c#·制造·虚拟现实
lzb_kkk29 分钟前
【JavaEE】JUC的常见类
java·开发语言·java-ee
爬山算法1 小时前
Maven(28)如何使用Maven进行依赖解析?
java·maven
2401_857439691 小时前
SpringBoot框架在资产管理中的应用
java·spring boot·后端
怀旧6661 小时前
spring boot 项目配置https服务
java·spring boot·后端·学习·个人开发·1024程序员节
李老头探索1 小时前
Java面试之Java中实现多线程有几种方法
java·开发语言·面试
芒果披萨1 小时前
Filter和Listener
java·filter