我用的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的静态资源配置)
时间: 2024-11-06 15:02:46