Spring Boot使用和配置Druid
程序员文章站
2024-02-26 19:02:16
1、引入依赖包
com.alibaba
1、引入依赖包
<!--druid--> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version>1.0.27</version> </dependency>
2、配置application.properties
spring.datasource.type=com.alibaba.druid.pool.druiddatasource spring.datasource.url=jdbc:mysql://localhost:3306/xxxxx?characterencoding=utf8&usessl=true&verifyservercertificate=false spring.datasource.username=root spring.datasource.password=xxxxx spring.datasource.driver-class-name=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 # 打开pscache,并且指定每个连接上pscache的大小 spring.datasource.poolpreparedstatements=true spring.datasource.maxpoolpreparedstatementperconnectionsize=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.filters=stat,wall,log4j # 通过connectproperties属性来打开mergesql功能;慢sql记录 spring.datasource.connectionproperties=druid.stat.mergesql=true;druid.stat.slowsqlmillis=5000 # 合并多个druiddatasource的监控数据 #spring.datasource.useglobaldatasourcestat=true spring.jpa.database=mysql spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.mysql5dialect #懒加载配置 spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
3、目前spring boot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池。 由于druid暂时不在spring bootz中的直接支持,故需要进行配置信息的定制
import com.alibaba.druid.pool.druiddatasource; import com.alibaba.druid.support.http.statviewservlet; import com.alibaba.druid.support.http.webstatfilter; import org.springframework.beans.factory.annotation.value; 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 org.springframework.context.annotation.primary; import javax.sql.datasource;import java.sql.sqlexception; @configuration public class druidconfiguration { @value("${spring.datasource.url}") private string dburl; @value("${spring.datasource.username}") private string username; @value("${spring.datasource.password}") private string password; @value("${spring.datasource.driver-class-name}") private string driverclassname; @value("${spring.datasource.initialsize}") private int initialsize; @value("${spring.datasource.minidle}") private int minidle; @value("${spring.datasource.maxactive}") private int maxactive; @value("${spring.datasource.maxwait}") private int maxwait; @value("${spring.datasource.timebetweenevictionrunsmillis}") private int timebetweenevictionrunsmillis; @value("${spring.datasource.minevictableidletimemillis}") private int minevictableidletimemillis; @value("${spring.datasource.validationquery}") private string validationquery; @value("${spring.datasource.testwhileidle}") private boolean testwhileidle; @value("${spring.datasource.testonborrow}") private boolean testonborrow; @value("${spring.datasource.testonreturn}") private boolean testonreturn; @value("${spring.datasource.poolpreparedstatements}") private boolean poolpreparedstatements; @value("${spring.datasource.maxpoolpreparedstatementperconnectionsize}") private int maxpoolpreparedstatementperconnectionsize; @value("${spring.datasource.filters}") private string filters; @value("{spring.datasource.connectionproperties}") private string connectionproperties; @bean //声明其为bean实例 @primary //在同样的datasource中,首先使用被标注的datasource public datasource datasource(){ druiddatasource datasource = new druiddatasource(); datasource.seturl(this.dburl); datasource.setusername(username); datasource.setpassword(password); datasource.setdriverclassname(driverclassname); //configuration datasource.setinitialsize(initialsize); datasource.setminidle(minidle); datasource.setmaxactive(maxactive); datasource.setmaxwait(maxwait); datasource.settimebetweenevictionrunsmillis(timebetweenevictionrunsmillis); datasource.setminevictableidletimemillis(minevictableidletimemillis); datasource.setvalidationquery(validationquery); datasource.settestwhileidle(testwhileidle); datasource.settestonborrow(testonborrow); datasource.settestonreturn(testonreturn); datasource.setpoolpreparedstatements(poolpreparedstatements); datasource.setmaxpoolpreparedstatementperconnectionsize(maxpoolpreparedstatementperconnectionsize); try { datasource.setfilters(filters); } catch (sqlexception e) { system.err.println("druid configuration initialization filter: "+ e); } datasource.setconnectionproperties(connectionproperties); return datasource; } @bean public servletregistrationbean statviewservle(){ servletregistrationbean servletregistrationbean = new servletregistrationbean(new statviewservlet(),"/druid/*"); // ip白名单 servletregistrationbean.addinitparameter("allow","192.168.1.218,127.0.0.1"); // ip黑名单(共同存在时,deny优先于allow) servletregistrationbean.addinitparameter("deny","192.168.1.100"); //控制台管理用户 servletregistrationbean.addinitparameter("loginusername","druid"); servletregistrationbean.addinitparameter("loginpassword","761341"); //是否能够重置数据 servletregistrationbean.addinitparameter("resetenable","false"); return servletregistrationbean; } @bean public filterregistrationbean statfilter(){ filterregistrationbean filterregistrationbean = new filterregistrationbean(new webstatfilter()); //添加过滤规则 filterregistrationbean.addurlpatterns("/*"); //忽略过滤的格式 filterregistrationbean.addinitparameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterregistrationbean; } }
4、浏览器输入http://localhost:8081/druid/index.html
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Spring boot :使用 Swagger 2 构建 RESTful APIs
-
Spring Boot使用和配置Druid
-
Spring Boot使用模板freemarker的示例代码
-
Spring Boot集成Druid数据库连接池
-
彻底搞明白Spring中的自动装配和Autowired注解的使用
-
在spring boot中使用java线程池ExecutorService的讲解
-
关于Spring Boot WebSocket整合以及nginx配置详解
-
Spring Boot(四)之使用JWT和Spring Security保护REST API
-
spring boot使用sonarqube来检查技术债务
-
在Spring boot的项目中使用Junit进行单体测试