spring配置Druid實現sql監控介面
阿新 • • 發佈:2018-12-21
1.新增資料庫連結依賴
<!-- 新增資料庫連線池依賴 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
2.spring-mybatis.xml配置
<!-- 配置資料來源 使用的是Druid資料來源 --> <bean name="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="initialSize" value="0" /> <!-- 連線池最大使用連線數量 --> <property name="maxActive" value="20" /> <!-- 連線池最小空閒 --> <property name="minIdle" value="0" /> <!-- 獲取連線最大等待時間 --> <property name="maxWait" value="60000" /> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> <!-- 用來檢測有效sql --> <property name="validationQuery" value="SELECT 1 FROM DUAL" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個連線在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 開啟removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分鐘 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 關閉abanded連線時輸出錯誤日誌 --> <property name="logAbandoned" value="true" /> <!-- 監控資料庫 --> <property name="filters" value="stat,wall,log4j" /> </bean>
3.web.xml配置新增如下程式碼 設定使用者名稱user 密碼123456
<!-- druid監控 --> <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>192.168.3.120,127.0.0.1</param-value> </init-param> <init-param> <param-name>deny</param-name> <param-value>192.168.3.120</param-value> </init-param> <init-param> <param-name>resetEnable</param-name> <param-value>true</param-value> </init-param> <init-param> <!-- 使用者名稱 --> <param-name>loginUsername</param-name> <param-value>user</param-value> </init-param> <init-param> <!-- 密碼 --> <param-name>loginPassword</param-name> <param-value>123456</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping>
5.下面是配置spring監控
spring.xml新增
<!-- druid spring monitor setting --> <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.test.service.*</value> </list> </property> </bean> <aop:config proxy-target-class="true"> <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut"/> </aop:config>
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>
<init-param>
<param-name>profileEnable</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>DruidWebStatFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
成功