由于项目中有好多sql写的不够规范,导致项目在运行时效率得到阻碍,组长下令说,利用德鲁伊 来监控一下看看那些sql需要优化,才得此机会了解阿里这个开源的监控组件.
首先了解一下,什么是Druid.
Druid是Java语言中最好的数据库连接池,它能够提供强大的监控和扩展功能。
Druid是一个JDBC组件,它包括三部分:
1)DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系。
2)DruidDataSource 高效可管理的数据库连接池。
3)SQLParser
Druid可以做什么呢,官网做了一下解释:
1) 可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助。
2) 替换DBCP和C3P0。Druid提供了一个高效、功能强大、可扩展性好的数据库连接池。
3) 数据库密码加密。直接把数据库密码写在配置文件中,这是不好的行为,容易导致安全问题。DruidDruiver和DruidDataSource都支持PasswordCallback。
4) SQL执行日志,Druid提供了不同的LogFilter,能够支持Common-Logging、Log4j和JdkLog,你可以按需要选择相应的LogFilter,监控你应用的数据库访问情况。
接下来就开始我们的正题,Druid在SpringMVC中的配置与使用.
============================分割线================================
1. 首先从 http://repo1.maven.org/maven2/com/alibaba/druid/ 下载最新的jar包,然后导到项目之中.
2.配置ApplicationContext.xml
< bean name = "transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = "dataSource" ref = "dataSource" > property >
bean >
< bean id = "propertyConfigurer" class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name = "locations" >
< list >
< value > /WEB-INF/classes/dbconfig.properties value >
list >
property >
bean >
< bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource"destroy-method = "close" >
< property name = "url" value = "${url}" />
< property name = "username" value = "${username}" />
< property name = "password" value = "${password}" />
< property name = "driverClassName" value = "${driverClassName}" />
< property name = "filters" value = "${filters}" />
< property name = "maxActive" value = "${maxActive}" />
< property name = "initialSize" value = "${initialSize}" />
< property name = "maxWait" value = "${maxWait}" />
< property name = "minIdle" value = "${minIdle}" />
< property name = "timeBetweenEvictionRunsMillis" value ="${timeBetweenEvictionRunsMillis}" />
< property name = "minEvictableIdleTimeMillis" value ="${minEvictableIdleTimeMillis}" />
< property name = "validationQuery" value = "${validationQuery}" />
< property name = "testWhileIdle" value = "${testWhileIdle}" />
< property name = "testOnBorrow" value = "${testOnBorrow}" />
< property name = "testOnReturn" value = "${testOnReturn}" />
< property name = "maxOpenPreparedStatements" value ="${maxOpenPreparedStatements}" />
< property name = "removeAbandoned" value = "${removeAbandoned}" />
< property name = "removeAbandonedTimeout" value ="${removeAbandonedTimeout}" />
< property name = "logAbandoned" value = "${logAbandoned}" />
bean >
3.添加dbconfig.properties
url:jdbc:mysql://60.205.106.190:3306/oasis_mom(你的项目的地址)
driverClassName: com.MySQL.jdbc.Driver
username: root(项目数据库名称)
password: root(项目数据库密码)
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 10
maxIdle: 15
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
maxOpenPreparedStatements: 20
removeAbandoned: true
removeAbandonedTimeout: 1800
logAbandoned: true
4.配置web.xml
< filter >
< filter-name > DruidWebStatFilter filter-name >
< filter-class > com.alibaba.druid.support.http.WebStatFilter filter-class >
< init-param >
< param-name > exclusions param-name >
< param-value > *. js ,*. gif ,*. jpg ,*. png ,*. css ,*. ico ,/ druid /* param-value >
init-param >
filter >
< filter-mapping >
< filter-name > DruidWebStatFilter filter-name >
< url-pattern > /* url-pattern >
filter-mapping >
< servlet >
< servlet-name > DruidStatView servlet-name >
< servlet-class > com.alibaba.druid.support.http.StatViewServlet servlet-class >
servlet >
< servlet-mapping >
< servlet-name > DruidStatView servlet-name >
< url-pattern > / druid /* url-pattern >
servlet-mapping >
5.配置完毕,访问监控页面: http://ip:port/projectName/druid/index.html(http://test.cn/honghclient/druid/weburi.html)进行访问