Druid Monitor小记

时间:2024-12-15 11:05:44

继上篇DruidDataSource源码分析之后 , 公司又要求做一个Druid的数据库监控 , 以及spring监控 , 研究一小时 , 总结出了一点经验 , 特此贴出来分享一下

一 . 利用Druid Monitor监控JavaWeb项目

1. 首先配置数据源

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close"> <!-- 基本属性 url、user、password -->
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.102.93:1521:mydb1" />
<property name="username" value="my_v31" />
<property name="password" value="my_v31" /> <!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" /> <!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" /> <!--
用来检测连接是否有效的sql,要求是一个查询语句。
如果validationQuery为null,testOnBorrow、testOnReturn、
testWhileIdle都不会其作用
-->
<property name="validationQuery" value="SELECT 1 FROM DUAL" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" /> <!--
打开PSCache,并且指定每个连接上PSCache的大小
如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false
-->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
<property name="filters" value="stat,wall,log4j" /> <!--
如果配置了proxyFilters,此配置可以不配置
druid.stat.mergeSql=true 合并执行的相同sql,避免因为参数不同而统计多条sql语句
druid.stat.slowSqlMillis=10000 用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢 <property name="connectionProperties" value="druid.stat.mergeSql=true;druid.stat.slowSqlMillis=10000" />
--> <!-- 监控统计拦截的filters -->
<!-- 并在filters属性中配置了log4j -->
<property name="proxyFilters">
<list>
<ref bean="stat-filter" />
<ref bean="log-filter" />
</list>
</property>
</bean> <!-- 慢SQL记录-->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<property name="mergeSql" value="true" />
<property name="slowSqlMillis" value="10000" />
<property name="logSlowSql" value="true" />
</bean> <bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
<!-- <property name="resultSetLogEnabled" value="false" /> -->
<!-- <property name="statementExecutableSqlLogEnable" value="true" /> -->
</bean> <!-- 配置druid监控spring jdbc -->
<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" /> <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
<property name="patterns">
<list>
<value>com.XXX.stat.service.*</value>
<value>com.XXX.stat.dao.*</value>
</list>
</property>
</bean> <aop:config proxy-target-class="true">
<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
</aop:config>
</beans>

2 . 配置web.xml

<filter>
<filter-name>DruidWebStatFilter</filter-name>
<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
<init-param>
<!-- 经常需要排除一些不必要的url,比如.js,/jslib/等等。配置在init-param中 -->
<param-name>exclusions</param-name>
<param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
</init-param>
<!-- 缺省sessionStatMaxCount是1000个。你可以按需要进行配置 -->
<init-param>
<param-name>sessionStatMaxCount</param-name>
<param-value>1000</param-value>
</init-param>
<!-- druid 0.2.7版本开始支持profile,配置profileEnable能够监控单个url调用的sql列表 -->
<init-param>
<param-name>profileEnable</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>principalSessionName</param-name>
<param-value>users.username</param-value>
</init-param>
<!-- 你可以关闭session统计功能
<init-param>
<param-name>sessionStatEnable</param-name>
<param-value>true</param-value>
</init-param> -->
</filter> <servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
<!--
deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝。
如果allow没有配置或者为空,则允许所有访问
-->
<init-param>
<param-name>allow</param-name>
<param-value>128.242.127.1/24,127.0.0.1</param-value>
</init-param>
<init-param>
<param-name>deny</param-name>
<param-value>192.168.1.118</param-value>
</init-param>
<!-- 在StatViewSerlvet输出的html页面中,有一个功能是Reset All,执行这个操作之后,会导致所有计数器清零,重新计数 -->
<span style="white-space:pre"> </span><init-param>
<span style="white-space:pre"> </span><param-name>resetEnable</param-name>
<span style="white-space:pre"> </span><param-value>false</param-value>
<span style="white-space:pre"> </span></init-param>
<span style="white-space:pre"> </span><!-- 用户名和密码 -->
<span style="white-space:pre"> </span><init-param>
<param-name>loginUsername</param-name>
<param-value>druid</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>

3 . 配置好web.xml之后,启动自己的JavaWeb服务,访问一下地址就可以看到Druid的监控界面。比如我在这里的访问地址是:http://web4.bj1:8119/druid/index.htm

Druid Monitor小记

  4 . 输入用户名密码(druid/druid)就可以进入监控页面。

Druid Monitor小记

二 . 监控JavaSE项目 ( 使用druidStat.sh 这个命令脚本在Git源码的druid\src\main\scripts目录下)

首先贴出脚本内容

1 . druidStat.sh

    #!/bin/sh  

    if [ -z "$JAVA_HOME" ] ; then
echo "Error: JAVA_HOME is not defined."
exit 1
fi "$JAVA_HOME/bin/java" -Dfile.encoding="UTF-8" -cp "./druid-0.2.6.jar:$JAVA_HOME/lib/tools.jar" com.alibaba.druid.support.console.DruidStat $@

  2 . druidStat.bat

@echo off  

rem Copyright 1999-2011 Alibaba Group Holding Ltd.
rem
rem Licensed under the Apache License, Version 2.0 (the "License");
rem you may not use this file except in compliance with the License.
rem You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License. set _RUNJAVA="%JAVA_HOME%\bin\java.exe"
set _TOOLSJAR="%JAVA_HOME%\lib\tools.jar" %_RUNJAVA% -classpath "./druid-0.2.6.jar;%_TOOLSJAR%" com.alibaba.druid.support.console.DruidStat %*

3 . 有了这个脚本就可以在服务器上查看监控信息了 , 只修要修改druid的jar包的路径和版本号.下面举个例子 , 用 sh druidStat.sh -help即可一目了然的看到使用方法 , 具体就不描述了

Druid Monitor小记