1. 程式人生 > >Druid 配置及內置監控,Web頁面查看監控內容

Druid 配置及內置監控,Web頁面查看監控內容

ans view ppi for ids group cto -c url

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頁面查看監控內容