服务器之家

服务器之家 > 正文

SpringBoot使用Druid数据源的配置方法

时间:2021-04-24 11:06     来源/作者:crazy_bai

druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了c3p0、dbcp、proxool等db池的优点,同时加入了日志监控,可以很好的监控db池连接和sql的执行情况,可以说是针对监控而生的db连接池(据说是目前最好的连接池)

一、依赖

为了测试,使用jdbctemplate

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- jdbctemplate -->
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-jdbc</artifactid>
</dependency>
<!-- druid数据库连接池 -->
<dependency>
  <groupid>com.alibaba</groupid>
  <artifactid>druid</artifactid>
  <version>1.0.26</version>
</dependency>
<!-- mysql connector -->
<dependency>
  <groupid>mysql</groupid>
  <artifactid>mysql-connector-java</artifactid>
  <scope>runtime</scope>
</dependency>

二、druid配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
druid.properties
#数据库设置
spring.datasource.driverclassname=com.mysql.jdbc.driver
spring.datasource.url=jdbc:mysql://localhost:3306/uu_core?useunicode=true&characterencoding=utf8&autoreconnect=true&usessl=true
spring.datasource.username=root
spring.datasource.password=root
#--------------------------
# 下面为连接池的补充设置,应用到上面所有数据源
# 初始化大小,最小,最大
spring.datasource.initialsize=5
spring.datasource.minidle=1
spring.datasource.maxactive=50
# 配置获取连接等待超时的时间
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=false
#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

三、实例化druid datasource

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cn.aduu.config;
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 org.springframework.context.annotation.propertysource;
import javax.sql.datasource;
/**
 * @author zh
 * @classname cn.aduu.config.druidconfiguration
 * @description
 */
@configuration
@propertysource(value = "classpath:druid.properties")
public class druidconfiguration {
  @bean(destroymethod = "close", initmethod = "init")
  @configurationproperties(prefix = "spring.datasource")
  public datasource druiddatasource() {
    druiddatasource druiddatasource = new druiddatasource();
    return druiddatasource;
  }
  /**
   * 注册一个statviewservlet
   * @return
   */
  @bean
  public servletregistrationbean druidstatviewservlet(){
    //org.springframework.boot.context.embedded.servletregistrationbean提供类的进行注册.
    servletregistrationbean servletregistrationbean = new servletregistrationbean(new statviewservlet(),"/druid/*");
    //添加初始化参数:initparams
    //白名单:
    servletregistrationbean.addinitparameter("allow","127.0.0.1");
    //ip黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:sorry, you are not permitted to view this page.
    servletregistrationbean.addinitparameter("deny","192.168.1.73");
    //登录查看信息的账号密码.
    servletregistrationbean.addinitparameter("loginusername","admin");
    servletregistrationbean.addinitparameter("loginpassword","123456");
    //是否能够重置数据.
    servletregistrationbean.addinitparameter("resetenable","false");
    return servletregistrationbean;
  }
  /**
   * 注册一个:filterregistrationbean
   * @return
   */
  @bean
  public filterregistrationbean druidstatfilter(){
    filterregistrationbean filterregistrationbean = new filterregistrationbean(new webstatfilter());
    //添加过滤规则.
    filterregistrationbean.addurlpatterns("/*");
    //添加不需要忽略的格式信息.
    filterregistrationbean.addinitparameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
    return filterregistrationbean;
  }
}

四、监控

访问 http://localhost:8080/druid, 使用上面配置的账号密码。

SpringBoot使用Druid数据源的配置方法

SpringBoot使用Druid数据源的配置方法

五、测试

?
1
2
3
4
5
6
7
8
9
10
11
@restcontroller
public class hellocontroller{
  private static final logger logger = loggerfactory.getlogger(hellocontroller.class);
  @autowired
  private jdbctemplate jdbctemplate;
  @requestmapping("hello")
  public list<map<string, object>> hello() {
    list<map<string, object>> list = jdbctemplate.queryforlist("select user,password from mysql.user ", new object[]{});
    return list;
  }
}

访问 localhost:8080/hello

?
1
2
3
4
5
6
7
8
9
10
[
  {
    "user": "root",
    "password": "*81f5e21e35407d884a6cd4a731aebfb6af209e1b"
  },
  {
    "user": "root",
    "password": "*81f5e21e35407d884a6cd4a731aebfb6af209e1b"
  }
]

总结

以上所述是小编给大家介绍的springboot使用druid数据源,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/qq_36640744/article/details/80096475

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部