Springboot集成Druid方案:一个是在POM中直接配置druid-spring-boot-starter,不用写任何代码;一个是配置druid,写几行代码,可以加入;在方案一基础上加入stat、wall后进行验证登录。
测试环境是:SpringBoot2.1.4.RELEASE。
一、直接使用druid-spring-boot-starter
1、POM配置
2、application.properties配置
### db config ###
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/XXX?useUnicode=true&characterEncoding=utf8¤tSchema=publicspring.datasource.username=postgres
spring.datasource.password=XXX
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=truespring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update#config druid
#连接池的设置
#初始化时建立物理连接的个数
spring.datasource.druid.initial-size=5#最小连接池数量
spring.datasource.druid.min-idle=5#最大连接池数量 maxIdle已经不再使用
spring.datasource.druid.max-active=20#获取连接时最大等待时间,单位毫秒
spring.datasource.druid.max-wait=60000#申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
spring.datasource.druid.test-while-idle=true#既作为检测的间隔时间又作为testWhileIdel执行的依据
spring.datasource.druid.time-between-eviction-runs-millis=60000#销毁线程时检测当前连接的最后活动时间和当前时间差大于该值时,关闭当前连接
spring.datasource.druid.min-evictable-idle-time-millis=30000#用来检测连接是否有效的sql 必须是一个查询语句
#mysql中为 select'x'#oracle中为 select1from dual
spring.datasource.druid.validation-query=select'x'#申请连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
spring.datasource.druid.test-on-borrow=false#归还连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
spring.datasource.druid.test-on-return=false#当数据库抛出不可恢复的异常时,抛弃该连接
spring.datasource.druid.exception-sorter=true#是否缓存preparedStatement,mysql5.5+建议开启
#spring.datasource.druid.pool-prepared-statements=true#当值大于0时poolPreparedStatements会自动修改为true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20#配置扩展插件
spring.datasource.druid.filters=stat,wall
#通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500#合并多个DruidDataSource的监控数据
spring.datasource.druid.use-global-data-source-stat=true#设置访问druid监控页的账号和密码,默认没有
#spring.datasource.druid.stat-view-servlet.login-username=admin
#spring.datasource.druid.stat-view-servlet.login-password=admin
### mybatis config ###
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.jrsc.supervisor.model
### log config ###
logging.config=classpath:logback-spring.xml
3、输入http://localhost:9091/druid/webapp.html后,即可看到Druid监控面板
二、使用代码,可验证登录的方案
1、POM配置,删除:druid-spring-boot-starter
2、application.properties配置不变
3、新建一个类:DruidConfig,注意包的引入,特别是:
package com.jrsc.supervisor.druid;import com.alibaba.druid.pool.DruidDataSource;import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;
3、DruidConfig具体实现如下:
@Configurationpublicclass DruidConfig {
@Bean
@ConfigurationProperties(prefix ="spring.datasource")
public DataSource getDataSource() {
returnnew DruidDataSource();
}
@Bean
public ServletRegistrationBean druidStatViewServlet() {
//ServletRegistrationBean提供类的进行注册ServletRegistrationBean servletRegistrationBean =newServletRegistrationBean(newStatViewServlet(),"/druid/*");
//添加初始化参数:initParams
//白名单:servletRegistrationBean.addInitParameter("allow","127.0.0.1");
//IP黑名单(同时存在时,deny优先于allow)
//如果满足deny,就提示:sorry,you are not permitted to view this pageservletRegistrationBean.addInitParameter("deny","192.168.1.73");
//登录查看信息的账号和密码servletRegistrationBean.addInitParameter("loginUsername","admin");
servletRegistrationBean.addInitParameter("loginPassword","123456");
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean filterRegistrationBean =newFilterRegistrationBean(new WebStatFilter());
//添加过滤规则filterRegistrationBean.addUrlPatterns("/*");
//添加需要忽略的格式信息filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,"+"*.jpg,*.png, *.css,*.ico,/druid/*");
return filterRegistrationBean;
}
4、对于SpringBoot的启动类,加入注解:@ServletComponentScan
5、输入:http://localhost:9091/druid/sql.html,观察Druid监控面板
三、通过在方案一基础上加入如下代码,实现验证登录
#设置访问druid监控页的账号和密码,默认没有
## Druid WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
spring.datasource.druid.web-stat-filter.enabled=truespring.datasource.druid.web-stat-filter.url-pattern=/*spring.datasource.druid.web-stat-filter.exclusions=*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico,/druid/*
# Druid StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123456
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=
四、测试
博客地址: http://www.cnblogs.com/defineconst/
博客版权: 本文以学习、研究和分享为主,欢迎转载和各类爬虫,但必须在文章页面明显位置给出原文链接。 如果文中有不妥或者错误的地方还望高手的您指出,以免误人子弟。如果您有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。