监控JAVASE程序
现在有一个JAVA程序A,使用druid连接数据库,想要通过web页面查看数据库的连接情况。
程序A
程序A在启动时要添加jmx参数。
-Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
就是启动程序的时候在java后面添加上面的参数
java -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false xxx.Main
监控页面
点击Create New Project,然后出现下面的页面
选中图中红框里部分。
选择JDK版本。
点击Next。
点击Next。
选择maven配置文件。
点击Finish完成创建。
创建完成后的目录结构如下:
打开pom.xml文件
在dependencies标签下增加对druid的依赖,版本号根据情况改。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.bulaoerhuoblog</groupId>
<artifactId>DruidMonitor</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>DruidMonitor Maven Webapp</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- add 需要增加的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- end -->
</dependencies>
<build>
<finalName>DruidMonitor</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
打开web.xml,增加配置
jmxUrl格式,host1的可以空着,hostname2和port2根据程序A的配置填。
hostname2就是程序A的IP地址。
port2是程序A启动的时候-Dcom.sun.management.jmxremote.port=1099属性指定的端口。
service:jmx:rmi://hostname1:port1/jndi/rmi://hostname2:port2/jmxrmi
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- add 增加的配置 -->
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
<init-param>
<!-- 允许清空统计数据 -->
<param-name>resetEnable</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<!-- 用户名 -->
<param-name>loginUsername</param-name>
<param-value>druid</param-value>
</init-param>
<init-param>
<!-- 目标程序地址 -->
<param-name>jmxUrl</param-name>
<param-value>service:jmx:rmi:///jndi/rmi://:1099/jmxrmi</param-value>
</init-param>
<init-param>
<!-- 密码 -->
<param-name>loginPassword</param-name>
<param-value>druid</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>
<!-- end -->
</web-app>
然后打war包,直接放到tomcat的webapp目录下就可以了。
访问地址:http://IP:PROT/DruidMonitor/druid/sql.html
先启动程序A,然后启动tomcat。如果程序A重启,tomcat也要重启。