码迷,mamicode.com
首页 > 其他好文 > 详细

druid数据源-为监控而生

时间:2015-09-23 12:07:47      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

我用的druid版本0.2.9:

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

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

package com.xxx.web.servlet;

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

import javax.servlet.http.HttpServletRequest;

/**
 * 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);
    }
}
package com.xxx.web.utils;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class ClientIpUtils {

   /**
    * 获取调用端的IP地址
    * @param request
    * @return
    */
   public static String getClientIP(HttpServletRequest request) {   
      String ip = __getClientIp(request);
      if(StringUtils.isEmpty(ip))
         return ip;
      return ip.replaceAll("::1", "").replaceAll(",", "").trim();
   } 
   
   private static String __getClientIp(HttpServletRequest request){
      String ip = request.getHeader("x-forwarded-for");   
       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
           ip = request.getHeader("Proxy-Client-IP");   
       }   
       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
           ip = request.getHeader("WL-Proxy-Client-IP");   
     
       }   
       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
           ip = request.getRemoteAddr();   
       }   
       return ip; 
   }
}

数据源配置:

<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 name="driverClassName" value="com.mysql.jdbc.Driver" />
   <property name="filters" value="log4j" />
   <property name="proxyFilters">
      <list>
         <ref bean="stat-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.xxx.web.servlet.StatViewServletWrapper</servlet-class>
   <init-param>
      <param-name>allow</param-name>
      <param-value>127.0.0.1,112.112.112,223.223.223</param-value>
   </init-param>
   <init-param>
      <param-name>deny</param-name>
      <param-value></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>

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

druid数据源-为监控而生

标签:

原文地址:http://my.oschina.net/wnjustdoit/blog/509919

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!