Server and Business Monitoring Practices Using Netdata, Spring AOP, and Javamelody
This article explains how to monitor both Linux servers and Java business applications by selecting lightweight tools like Netdata, implementing request‑time logging with Spring AOP, and integrating Javamelody, while providing configuration snippets and code examples for a comprehensive monitoring solution.
The article begins by distinguishing two monitoring scopes: server‑level monitoring (CPU, memory, disk I/O on Linux) and business‑level monitoring (SQL execution, request latency, error rates, session counts, etc.).
For server monitoring, the author notes that although Alibaba Cloud provides built‑in alerts, a custom solution is preferred; after evaluating Zabbix, Nagios, and others, the lightweight Netdata tool was chosen for the CentOS environment.
Business monitoring is described as essential for any system, requiring error logs, SMS alerts, and metrics such as Java memory, CPU usage, session counts, JDBC connections, and request statistics. The author highlights the use of Spring AOP to intercept method executions and log any request that exceeds a 1500 ms threshold.
import org.apache.commons.lang.time.StopWatch;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
@Pointcut("execution(* com.netcai.admin.controller.*.*.*(..))")
public void pointcutExpression() { logger.debug("配置切入点"); }
@Before("pointcutExpression()")
public void beforeMethod(JoinPoint joinPoint) { logger.debug("前置通知执行了"); }
@After("pointcutExpression()")
public void afterMethod(JoinPoint joinPoint) { logger.debug("后置通知执行了,有异常也会执行"); }
@AfterReturning(value = "pointcutExpression()", returning = "returnValue")
public void afterRunningMethod(JoinPoint joinPoint, Object returnValue) { logger.debug("返回通知执行,执行结果:" + returnValue); }
@AfterThrowing(value = "pointcutExpression()", throwing = "e")
public void afterThrowingMethod(JoinPoint joinPoint, Exception e) { logger.debug("异常通知, 出现异常 " + e); }
@Around("pointcutExpression()")
public Object aroundMethod(ProceedingJoinPoint pjd) {
StopWatch clock = new StopWatch();
Object result = null;
String className = pjd.getTarget().getClass().getName();
String methodName = pjd.getSignature().getName();
try {
clock.start();
result = pjd.proceed();
clock.stop();
} catch (Throwable e) {
e.printStackTrace();
}
if (!methodName.equalsIgnoreCase("initBinder")) {
long constTime = clock.getTime();
logger.info("["+className+"]-["+methodName+"] 花费时间:" + constTime + "ms");
if (constTime > 500) {
logger.error("["+className+"]-["+methodName+"] 花费时间过长,请检查: " + constTime + "ms");
}
}
return result;
}
}The author adds that this aspect records execution time for each method and logs an error when the duration exceeds a configured threshold, prompting daily review and optimization.
Another monitoring solution, Javamelody , is introduced for overall business line visibility. The required Maven dependency and web.xml configuration are provided.
<!-- 系统监控 -->
<dependency>
<groupId>net.bull.javamelody</groupId>
<artifactId>javamelody-core</artifactId>
<version>1.68.1</version>
</dependency>
<dependency>
<groupId>org.jrobin</groupId>
<artifactId>jrobin</artifactId>
<version>1.5.9</version>
</dependency> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:config/applicationContext.xml
classpath*:net/bull/javamelody/monitoring-spring.xml
classpath*:net/bull/javamelody/monitoring-spring-datasource.xml
classpath*:net/bull/javamelody/monitoring-spring-aspectj.xml
</param-value>
</context-param>
<filter>
<filter-name>monitoring</filter-name>
<filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>logEnabled</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>monitoring</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>net.bull.javamelody.SessionListener</listener-class>
</listener>Operational results are illustrated with several screenshots showing dashboards and alerts generated by the monitoring stack.
In conclusion, the author emphasizes that a self‑built monitoring system can be assembled from lightweight components like Netdata, Spring AOP, and Javamelody, while also mentioning alternatives such as Druid for connection pools, Zabbix for system metrics, and CAT for business tracing, all of which can be extended or customized.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.