標籤:htm 統計 mys enc sql ntp org blank 時間
1.配置Druid的內建監控
首先在Maven項目的pom.xml中引入包
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid-version}</version></dependency>
2.引入包後配置spring的設定檔
<!--讀取設定檔--><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property></bean><!-- Druid JNDI DataSource for J2EE environments --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="20" /> <property name="initialSize" value="1" /> <property name="maxWait" value="60000" /> <property name="minIdle" value="1" /> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT ‘x‘ FROM DUAL" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- mysql 不支援 poolPreparedStatements--> <!--<property name="poolPreparedStatements" value="true" />--> <!--<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />--> <!-- 開啟Druid的監控統計功能 --> <property name="filters" value="stat" /></bean>
然後在web.xml中開啟監控服務
<!-- 添加druid監控--> <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><!-- 添加Web應用等監控--> <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> <init-param> <param-name>profileEnable</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>principalCookieName</param-name> <param-value>USER_COOKIE</param-value> </init-param> <init-param> <param-name>principalSessionName</param-name> <param-value>USER_SESSION</param-value> </init-param></filter><filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
好了,經過上邊三個步驟,就可以進行監控了。訪問頁面http://127.0.0.1:8080/{projectName}/druid/index.html就可以訪問監控頁面了,druid已經將資料友好的顯示到頁面上了,包括每條sql執行的次數,執行的時間,最慢時間等等很多詳細資料,供我們參考,找到哪些執行效率低的耗時間長度的語句進行最佳化。同時提供資料來源、sql防火牆,web應用,URI監控、Session監控,spring監控等,裡面有詳細的各種資料資訊,大家可以參考查閱。projectName是項目名稱
下邊是監控結果頁面
Druid 配置及內建監控,Web頁面查看監控內容