继上篇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"> <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" /> <property name="validationQuery" value="SELECT 1 FROM DUAL" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <property name="filters" value="stat,wall,log4j" /> <property name="proxyFilters"> <list> <ref bean="stat-filter" /> <ref bean="log-filter" /> list> property> bean> <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"> bean> <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>DruidWebStatFilterfilter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class> <init-param> <param-name>exclusionsparam-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*param-value> init-param> <init-param> <param-name>sessionStatMaxCountparam-name> <param-value>1000param-value> init-param> <init-param> <param-name>profileEnableparam-name> <param-value>trueparam-value> init-param> <init-param> <param-name>principalSessionNameparam-name> <param-value>users.usernameparam-value> init-param> filter> <servlet> <servlet-name>DruidStatViewservlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class> <init-param> <param-name>allowparam-name> <param-value>128.242.127.1/24,127.0.0.1param-value> init-param> <init-param> <param-name>denyparam-name> <param-value>192.168.1.118param-value> init-param> <span style="white-space:pre"> span><init-param> <span style="white-space:pre"> span><param-name>resetEnableparam-name> <span style="white-space:pre"> span><param-value>falseparam-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>loginUsernameparam-name> <param-value>druidparam-value> init-param> <init-param> <param-name>loginPasswordparam-name> <param-value>druidparam-value> init-param> servlet> <servlet-mapping> <servlet-name>DruidStatViewservlet-name> <url-pattern>/druid/*url-pattern> servlet-mapping>
3 . 配置好web.xml之后,启动自己的JavaWeb服务,访问一下地址就可以看到Druid的监控界面。比如我在这里的访问地址是:http://web4.bj1:8119/druid/index.htm
4 . 输入用户名密码(druid/druid)就可以进入监控页面。
二 . 监控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即可一目了然的看到使用方法 , 具体就不描述了