Spring中Druid连接池的配置

本文记录了使用Druid的方法, 包括Spring和Spring boot中使用Druid的配置方法. css

Spring中配置Druid链接池

以连接mysql为例java

1 添加druid依赖

<dependency>
    <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> 

2 在Spring配置中添加Druid相关配置

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_user}" /> <property name="password" value="${jdbc_password}" /> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat" /> <!-- 配置初始化大小、最小、最大链接池数量 --> <property name="maxActive" value="20" /> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <!-- 配置获取链接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测须要关闭的空闲链接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个链接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打开PSCache,而且指定每一个链接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxOpenPreparedStatements" value="20" /> </bean> 

3 在web.xml中添加Druid相关配置

  <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> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

而后运行程序访问http://localhost:8080/druid便可mysql

Spring boot中配置Druid链接池

首先添加pom依赖, 而后application.properties配置信息以下:git

# 驱动配置信息  
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource  
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/mealsystem?useUnicode=true&characterEncoding=utf-8  
spring.datasource.username = root  
spring.datasource.password = 123456  
spring.datasource.driverClassName = com.mysql.jdbc.Driver  

#链接池的配置信息  
spring.datasource.initialSize=5  
spring.datasource.minIdle=5  
spring.datasource.maxActive=20  
spring.datasource.maxWait=60000  
spring.datasource.timeBetweenEvictionRunsMillis=60000  
spring.datasource.minEvictableIdleTimeMillis=300000  
spring.datasource.validationQuery=SELECT 1 FROM DUAL  
spring.datasource.testWhileIdle=true  
spring.datasource.testOnBorrow=false  
spring.datasource.testOnReturn=false  
spring.datasource.poolPreparedStatements=true  
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20  
spring.datasource.filters=stat,wall,log4j  
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

Spring boot的Druid监控设置

首先druid配置文件中须要有以下相关filter配置, 内置的Filter名字可参考这里github

spring.datasource.filters=stat,wall,log4j 

而后再注册Servlet和Filter. 有两种方法web

方法一 使用代码注册Servlet和Filter

新建一个DruidConfiguration文件spring

@Configuration
public class DruidConfiguration { @Bean public ServletRegistrationBean druidServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); servletRegistrationBean.setServlet(new StatViewServlet()); servletRegistrationBean.addUrlMappings("/druid/*"); Map<String, String> initParameters = new HashMap<String, String>(); initParameters.put("loginUsername", "admin"); initParameters.put("loginPassword", "admin"); initParameters.put("resetEnable", "false"); servletRegistrationBean.setInitParameters(initParameters); return servletRegistrationBean; } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } } 

而后运行程序访问http://localhost:8080/druid便可sql

方法二 原生的servlet和filter方式

配置Servlet代码app

@WebServlet( urlPatterns = "/druid/*", initParams = { @WebInitParam(name = "loginUsername", value = "admin"), @WebInitParam(name = "loginPassword", value = "admin"), @WebInitParam(name = "resetEnable", value = "admin"), } ) public class DruidStatViewServlet extends StatViewServlet { } 

配置filter测试

@WebFilter( filterName = "druidWebStatFilter", urlPatterns = "/*", initParams = { @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*") } ) public class DruidWebStatFilter extends WebStatFilter { } 

而后在spring boot启动类Application.java上加注解@ServletComponentScan 设置Servlet自动扫描.访问http://localhost:8080/druid测试.

相关文章
相关标签/搜索