以下二种方式都需要先配置:
@Bean
@ConditionalOnExpression("${spring.datasource.jmxEnabled:true}")
public ConnectionPool jdbcPool(org.apache.tomcat.jdbc.pool.DataSource dataSource) throws SQLException {
return dataSource.createPool().getJmxPool();
}
一、系统中监控
import javax.management.MBeanAttributeInfo; import javax.management.MBeanInfo; import javax.management.MBeanServer; import javax.management.ObjectName; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.lang.management.ManagementFactory; import java.util.Set; public class MonitorServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); writer.println("<!DOCTYPE html>"); writer.println("<html>"); writer.println("<body>"); writer.println("<p><h1>Tomcat Pool</h1></p><p>"); try { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); Set<ObjectName> objectNames = server.queryNames(null, null); for (ObjectName name : objectNames) { MBeanInfo info = server.getMBeanInfo(name); if (info.getClassName().equals( "org.apache.tomcat.jdbc.pool.jmx.ConnectionPool")) { for (MBeanAttributeInfo mf : info.getAttributes()) { Object attributeValue = server.getAttribute(name, mf.getName()); if (attributeValue != null) { writer.println("" + mf.getName() + " : " + attributeValue.toString() + "<br/>"); } } break; } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } writer.println("</p></body>"); writer.println("</html>"); } }
二、jconsole监控
远程监控需要配置-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8011 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
1、cmd 然后输入jconsole,选择应用
2、