服务器之家

服务器之家 > 正文

Spring Boot 使用Druid详解

时间:2020-10-08 16:32     来源/作者:心碎落地的声音

druid是java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能,下面来说明如何在 springboot 中配置使用druid。
步骤:

1. 在pom.xml中加载依赖

2. 在application.properties中加入数据源配置

3. 编写druidconfiguration,通过@bean注解的方式注入druid servlet和filter,以便提供监控页面访问

4. 浏览器访问

一、在pom.xml中加入依赖

?
1
2
3
4
5
<dependency>
      <groupid>com.alibaba</groupid>
      <artifactid>druid</artifactid>
      <version>1.0.18</version>
  </dependency>

二、在application.properties中加入数据源配置

?
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
################## server ################################
server.port=8080
 
################## datasource ################################
spring.datasource.url = jdbc:mysql://localhost:3306/spring
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverclassname = com.mysql.jdbc.driver
 
#使用druid数据源
spring.datasource.type=com.alibaba.druid.pool.druiddatasource
# 下面为连接池的补充设置,应用到上面所有数据源中
 
# 初始化大小,最小,最大
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 fromdual
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
 
 
################ java persistence api ###########################
# specify the dbms
spring.jpa.database = mysql
# show or not log for each sql query
spring.jpa.show-sql = true
# hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
#[org.hibernate.cfg.improvednamingstrategy #org.hibernate.cfg.defaultnamingstrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.improvednamingstrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.mysql5dialect
 
#暂时不用redis,所以none,spring.session.store-type=reids
spring.session.store-type=none

以上是我项目中的配置信息,大家只要关注datasource部分即可。

三、编写druidconfiguration配置类,通过@bean注解的方式注入druid servlet和filter,以便提供监控页面访问

?
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.example.config;
 
import java.sql.sqlexception;
 
import javax.sql.datasource;
 
import org.slf4j.logger;
import org.slf4j.loggerfactory;
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 com.alibaba.druid.pool.druiddatasource;
import com.alibaba.druid.support.http.statviewservlet;
import com.alibaba.druid.support.http.webstatfilter;
 
/**
 * 配置druid数据源
 *
 */
@configuration
public class druidconfiguration {
   
  private logger logger = loggerfactory.getlogger(druidconfiguration.class);
 
  @bean
  public servletregistrationbean druidstatviewservlet() {
    servletregistrationbean registrationbean = new servletregistrationbean(new statviewservlet(), "/druid/*");
    registrationbean.addinitparameter("allow", "127.0.0.1");
    registrationbean.addinitparameter("deny", "192.168.31.234");
    registrationbean.addinitparameter("loginusername", "admin");
    registrationbean.addinitparameter("loginpassword", "123456");
    registrationbean.addinitparameter("resetenable", "false");
 
    return registrationbean;
  }
 
  @bean
  public filterregistrationbean druidwebstatviewfilter() {
    filterregistrationbean registrationbean = new filterregistrationbean(new webstatfilter());
    registrationbean.addinitparameter("urlpatterns", "/*");
    registrationbean.addinitparameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
 
    return registrationbean;
 
  }
 
  @bean
  public datasource druiddatasource(@value("${spring.datasource.url}") string url,
      @value("${spring.datasource.driverclassname}") string driver,
      @value("${spring.datasource.username}") string username,
      @value("${spring.datasource.password}") string password,
      @value("${spring.datasource.maxactive}") int maxactive,
      @value("${spring.datasource.filters}") string filters,
      @value("${spring.datasource.initialsize}")
      int initialsize,
      @value("${spring.datasource.minidle}")
      int minidle,
      @value("${spring.datasource.maxwait}")
      int maxwait,
      @value("${spring.datasource.timebetweenevictionrunsmillis}")
      int timebetweenevictionrunsmillis,
      @value("${spring.datasource.minevictableidletimemillis}")
      int minevictableidletimemillis,
      @value("${spring.datasource.validationquery}")
      string validationquery,
      @value("${spring.datasource.testwhileidle}")
      boolean testwhileidle,
      @value("${spring.datasource.testonborrow}")
      boolean testonborrow,
      @value("${spring.datasource.testonreturn}")
      boolean testonreturn,
      @value("${spring.datasource.poolpreparedstatements}")
      boolean poolpreparedstatements,
      @value("${spring.datasource.maxpoolpreparedstatementperconnectionsize}")
      int maxpoolpreparedstatementperconnectionsize,
      @value("${spring.datasource.connectionproperties}")
      string connectionproperties,
      @value("${spring.datasource.useglobaldatasourcestat}")
      boolean useglobaldatasourcestat
 
  ) {
    druiddatasource datasource = new druiddatasource();
    /*数据源主要配置*/
    datasource.seturl(url);
    datasource.setdriverclassname(driver);
    datasource.setusername(username);
    datasource.setpassword(password);
     
    /*数据源补充配置*/
    datasource.setmaxactive(maxactive);
    datasource.setinitialsize(initialsize);
    datasource.setminidle(minidle);
    datasource.setmaxwait(maxwait);
    datasource.settimebetweenevictionrunsmillis(timebetweenevictionrunsmillis);
    datasource.setminevictableidletimemillis(minevictableidletimemillis);
    datasource.setvalidationquery(validationquery);
    datasource.settestonborrow(testonborrow);
    datasource.settestonreturn(testonreturn);
    datasource.settestwhileidle(testwhileidle);
    datasource.setpoolpreparedstatements(poolpreparedstatements);
    datasource.setmaxpoolpreparedstatementperconnectionsize(maxpoolpreparedstatementperconnectionsize);
    datasource.setconnectionproperties(connectionproperties);
    datasource.setuseglobaldatasourcestat(useglobaldatasourcestat);
     
    try {
      datasource.setfilters(filters);
      logger.info("druid数据源初始化设置成功......");
    } catch (sqlexception e) {
      e.printstacktrace();
      logger.info("druid数据源filters设置失败......");
    }
    return datasource;
 
  }
}

四、浏览器访问

在浏览器中输入http://localhost:8080/druid/index.html,首次访问需要使用账号和密码登录

Spring Boot 使用Druid详解

Spring Boot 使用Druid详解

在数据源一栏中,所有加"*"标识的都不能为空,,尤其是filter类名,否则会看不到sql监控数据。

五、查看sql监控

执行删除某条记录后,查看sql监控

Spring Boot 使用Druid详解

到这里,我们就把druid配置成功了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/linabc123000/article/details/69062204

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部