1. 程式人生 > >Druid+SSM+視覺化監控介面

Druid+SSM+視覺化監控介面

先看效果:

關鍵步驟說明:

前提條件,自己先搭建一個ssm框架,然後再繼續下面的步驟

1.匯入jar包

<druid.version>0.2.23</druid.version>

<dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>druid</artifactId>

    <version>${druid.version}</version>

</dependency>

2.配置資料來源

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/srnswx?characterEncoding=utf-8

jdbc.username=root

jdbc.password=123456

druid.initialSize=10

druid.minIdle=10

druid.maxActive=50

druid.maxWait=60000

druid.timeBetweenEvictionRunsMillis=60000

druid.minEvictableIdleTimeMillis=300000

druid.validationQuery=SELECT 'x'

druid.testWhileIdle=true

druid.testOnReturn=false

druid.poolPreparedStatements=true

 

</bean>

<property name="filters" value="${druid.filters}" />

<!-- 配置監控統計攔截的filters -->

value="${druid.maxPoolPreparedStatementPerConnectionSize}" />

<property name="maxPoolPreparedStatementPerConnectionSize"

<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" />

<!-- 開啟PSCache,並且指定每個連線上PSCache的大小 如果用Oracle,則把poolPreparedStatements配置為true,mysql可以配置為false。 -->

<property name="testOnReturn" value="${druid.testOnReturn}" />

<property name="testOnBorrow" value="${druid.testOnBorrow}" />

<property name="testWhileIdle" value="${druid.testWhileIdle}" />

<property name="validationQuery" value="${druid.validationQuery}" />

<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />

<!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->

<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />

<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->

<property name="maxWait" value="${druid.maxWait}" />

<!-- 配置獲取連線等待超時的時間 -->

<property name="maxActive" value="${druid.maxActive}" />

<property name="minIdle" value="${druid.minIdle}" />

<property name="initialSize" value="${druid.initialSize}" />

<!-- 配置初始化大小、最小、最大 -->

<property name="password" value="${jdbc.password}" />

<property name="username" value="${jdbc.username}" />

<property name="url" value="${jdbc.url}" />

<property name="driverClassName" value="${jdbc.driver}" />

destroy-method="close">

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

druid.filters=stat

druid.maxPoolPreparedStatementPerConnectionSize=20

druid.poolPreparedStatements=true

 

3.配置web.xml

<!--druid 監控 -->

<filter>

<filter-name>DruidWebStatFilter</filter-name>

<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>

<async-supported>true</async-supported>

<init-param>

<param-name>exclusions</param-name>

<param-value>/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>

</init-param>

<init-param>

<param-name>sessionStatMaxCount</param-name>

<param-value>1000</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>DruidWebStatFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- druid監控 -->

<servlet>

<servlet-name>DruidStatView</servlet-name>

<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>

<async-supported>true</async-supported>

</servlet>

<servlet-mapping>

<servlet-name>DruidStatView</servlet-name>

<url-pattern>/druid/*</url-pattern>

</servlet-mapping>

 

4.編寫controller

package com.demo.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

@RequestMapping("/druid")

public class DruidController {

@RequestMapping("/index")

public String index(Model model, HttpServletRequest request) {

return "durid/index";

}

}

到這裡就完成了,http://localhost:8080/.../druid/index  就ok了。