druid数据源-为监控而生

官方参考:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatViewServlet%E9%85%8D%E7%BD%AE

maven坐标:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.9</version>
</dependency>

由于这个版本的ip获取的有问题,所以覆盖了druid的servlet,并重写获取ip的方法:

package com.caiya.software.web.servlet;

import com.alibaba.druid.support.http.StatViewServlet;
import com.caiya.software.web.utils.ClientIpUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;

/**
 * 覆盖父类获取IP的方式
 * Created by caiya on 15/9/18.
 */
public class StatViewServletWrapper extends StatViewServlet {

    private static final Logger logger = Logger.getLogger(StatViewServletWrapper.class);

    @Override
    public boolean isPermittedRequest(HttpServletRequest request) {
        String remoteAddress = ClientIpUtils.getClientIP(request);
        return super.isPermittedRequest(remoteAddress);
    }
}

数据源配置:

    <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
        <property name="slowSqlMillis" value="5000"/>
        <property name="logSlowSql" value="true"/>
        <property name="mergeSql" value="true"/>
    </bean>
    <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
        <property name="dbType" value="mysql"/>
        <property name="logViolation" value="true"/>
        <property name="throwException" value="false"/>
    </bean>
    <bean id="log4j-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
    </bean>

    <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.user}"/>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <!--<property name="filters" value="log4j" />-->
        <property name="proxyFilters">
            <list>
                <ref bean="stat-filter"/>
                <ref bean="log4j-filter"/>
            </list>
        </property>
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="initialSize" value="${jdbc.initialSize}"/>
        <property name="maxWait" value="${jdbc.maxWait}"/>
        <property name="minIdle" value="${jdbc.minIdle}"/>
        <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
        <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>

        <property name="validationQuery" value="SELECT 'x'"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        <property name="poolPreparedStatements" value="false"/>
    </bean>

web.xml:

<servlet>
    <servlet-name>DruidStatView</servlet-name>
    <servlet-class>com.caiya.software.web.servlet.StatViewServletWrapper</servlet-class>
    <init-param>
        <param-name>allow</param-name>
        <param-value>127.0.0.1,1.2.3.4</param-value>
    </init-param>
    <init-param>
        <param-name>deny</param-name>
        <param-value>1.1.1.1</param-value>
    </init-param>
    <init-param>
        <param-name>resetEnable</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>DruidStatView</servlet-name>
    <url-pattern>/druid/*</url-pattern>
</servlet-mapping>

<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>principalCookieName</param-name>
        <param-value>cookiename</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>

spring配置:

<bean id="druid-stat-interceptor"
      class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>

<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
      scope="prototype">
    <property name="patterns">
        <list>
            <value>com.caiya.software.web.controllers.*</value>
        </list>
    </property>
</bean>

<aop:config>
    <aop:advisor advice-ref="druid-stat-interceptor"
                 pointcut-ref="druid-stat-pointcut" />
</aop:config>

注意:

1、前端访问路径:http://ip:port/druid/index.html(注意nginx的静态资源配置);

2、监控配置会有一定的性能损耗,建议优化完毕去掉

你可能感兴趣的:(druid数据源-为监控而生)