1 介绍

1.1数据库连接池介绍

数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是在重新建立一个,通过数据库连接池能明显提高对数据库操作的性能。
在Java应用程序中,常用的数据库连接池有DBCP、C3P0、Proxool、Druid等

1.2 数据库连接池性能比较

参考:https://blog.csdn.net/u011389515/article/details/76578129

1.3 Druid介绍

Druid 是阿里开源的一个JDBC应用组件,提供了非常优秀的数据库监控和扩展功能。
网址:Druid:https://github.com/alibaba/druid/

2集成

Springboot 默认数据库连接池 hikari

2.1加入依赖

  1. <!--druid-->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>druid-spring-boot-starter</artifactId>
  5. <version>1.2.8</version>
  6. </dependency>

2.2 添加配置

修改配置文件,将原有的数据源配置替换成Druid数据源并配置数据源相关参数。

  1. spring:
  2. datasource:
  3. name: druidDataSource
  4. type: com.alibaba.druid.pool.DruidDataSource
  5. druid:
  6. # JDBC 配置
  7. driver-class-name: com.mysql.jdbc.Driver
  8. url: jdbc:mysql://localhost:3306/mybatis_plus_demo?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8
  9. username: root
  10. password: root
  11. filters: stat,wall
  12. # filters: stat,wall,log4j,config
  13. #连接池配置
  14. max-active: 100
  15. initial-size: 1
  16. max-wait: 60000
  17. min-idle: 1
  18. time-between-eviction-runs-millis: 60000
  19. min-evictable-idle-time-millis: 300000
  20. validation-query: select 'x'
  21. test-while-idle: true
  22. test-on-borrow: false
  23. test-on-return: false
  24. pool-prepared-statements: true
  25. max-open-prepared-statements: 50
  26. max-pool-prepared-statement-per-connection-size: 20

2.3 自定义属性配置

如果需要通过定制的配置文件对Druid进行自定义属性配置,添加配置类如下:
edu.xja.config 包下新建一个DruidDataSourceProperties 类

  1. /**
  2. * 数据源属性
  3. */
  4. @ConfigurationProperties(prefix = "spring.datasource.druid")
  5. public class DruidDataSourceProperties {
  6. // jdbc
  7. private String driverClassName;
  8. private String url;
  9. private String username;
  10. private String password;
  11. // jdbc connection pool
  12. private int initialSize;
  13. private int minIdle;
  14. private int maxActive = 100;
  15. private long maxWait;
  16. private long timeBetweenEvictionRunsMillis;
  17. private long minEvictableIdleTimeMillis;
  18. private String validationQuery;
  19. private boolean testWhileIdle;
  20. private boolean testOnBorrow;
  21. private boolean testOnReturn;
  22. private boolean poolPreparedStatements;
  23. private int maxPoolPreparedStatementPerConnectionSize;
  24. // filter
  25. private String filters;
  26. public int getInitialSize() {
  27. return initialSize;
  28. }
  29. public void setInitialSize(int initialSize) {
  30. this.initialSize = initialSize;
  31. }
  32. public int getMinIdle() {
  33. return minIdle;
  34. }
  35. public void setMinIdle(int minIdle) {
  36. this.minIdle = minIdle;
  37. }
  38. public int getMaxActive() {
  39. return maxActive;
  40. }
  41. public void setMaxActive(int maxActive) {
  42. this.maxActive = maxActive;
  43. }
  44. public long getMaxWait() {
  45. return maxWait;
  46. }
  47. public void setMaxWait(long maxWait) {
  48. this.maxWait = maxWait;
  49. }
  50. public long getTimeBetweenEvictionRunsMillis() {
  51. return timeBetweenEvictionRunsMillis;
  52. }
  53. public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
  54. this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
  55. }
  56. public long getMinEvictableIdleTimeMillis() {
  57. return minEvictableIdleTimeMillis;
  58. }
  59. public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
  60. this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
  61. }
  62. public String getValidationQuery() {
  63. return validationQuery;
  64. }
  65. public void setValidationQuery(String validationQuery) {
  66. this.validationQuery = validationQuery;
  67. }
  68. public boolean isTestWhileIdle() {
  69. return testWhileIdle;
  70. }
  71. public void setTestWhileIdle(boolean testWhileIdle) {
  72. this.testWhileIdle = testWhileIdle;
  73. }
  74. public boolean isTestOnBorrow() {
  75. return testOnBorrow;
  76. }
  77. public void setTestOnBorrow(boolean testOnBorrow) {
  78. this.testOnBorrow = testOnBorrow;
  79. }
  80. public boolean isTestOnReturn() {
  81. return testOnReturn;
  82. }
  83. public void setTestOnReturn(boolean testOnReturn) {
  84. this.testOnReturn = testOnReturn;
  85. }
  86. public boolean isPoolPreparedStatements() {
  87. return poolPreparedStatements;
  88. }
  89. public void setPoolPreparedStatements(boolean poolPreparedStatements) {
  90. this.poolPreparedStatements = poolPreparedStatements;
  91. }
  92. public int getMaxPoolPreparedStatementPerConnectionSize() {
  93. return maxPoolPreparedStatementPerConnectionSize;
  94. }
  95. public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
  96. this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
  97. }
  98. public String getFilters() {
  99. return filters;
  100. }
  101. public void setFilters(String filters) {
  102. this.filters = filters;
  103. }
  104. public String getDriverClassName() {
  105. return driverClassName;
  106. }
  107. public void setDriverClassName(String driverClassName) {
  108. this.driverClassName = driverClassName;
  109. }
  110. public String getUrl() {
  111. return url;
  112. }
  113. public void setUrl(String url) {
  114. this.url = url;
  115. }
  116. public String getUsername() {
  117. return username;
  118. }
  119. public void setUsername(String username) {
  120. this.username = username;
  121. }
  122. public String getPassword() {
  123. return password;
  124. }
  125. public void setPassword(String password) {
  126. this.password = password;
  127. }
  128. }

2.4 config包下配置Servlet和Filter

Config包下新建一个DruidConfig配置类,主要是注入属性和配置连接池相关的配置,如黑白名单、监控管理后台登录账户密码等

  1. /**
  2. * Druid数据源配置
  3. */
  4. @Configuration
  5. @EnableConfigurationProperties({DruidDataSourceProperties.class})
  6. public class DruidConfig {
  7. @Autowired
  8. private DruidDataSourceProperties properties;
  9. @Bean
  10. @ConditionalOnMissingBean
  11. public DataSource druidDataSource() {
  12. DruidDataSource druidDataSource = new DruidDataSource();
  13. druidDataSource.setDriverClassName(properties.getDriverClassName());
  14. druidDataSource.setUrl(properties.getUrl());
  15. druidDataSource.setUsername(properties.getUsername());
  16. druidDataSource.setPassword(properties.getPassword());
  17. druidDataSource.setInitialSize(properties.getInitialSize());
  18. druidDataSource.setMinIdle(properties.getMinIdle());
  19. druidDataSource.setMaxActive(properties.getMaxActive());
  20. druidDataSource.setMaxWait(properties.getMaxWait());
  21. druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
  22. druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
  23. druidDataSource.setValidationQuery(properties.getValidationQuery());
  24. druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());
  25. druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
  26. druidDataSource.setTestOnReturn(properties.isTestOnReturn());
  27. druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
  28. druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());
  29. try {
  30. druidDataSource.setFilters(properties.getFilters());
  31. druidDataSource.init();
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. return druidDataSource;
  36. }
  37. /**
  38. * 注册Servlet信息, 配置监控视图
  39. *
  40. * @return
  41. */
  42. @Bean
  43. @ConditionalOnMissingBean
  44. public ServletRegistrationBean<Servlet> druidServlet() {
  45. ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<Servlet>(new StatViewServlet(), "/druid/*");
  46. //白名单:
  47. // servletRegistrationBean.addInitParameter("allow","127.0.0.1,139.196.87.48");
  48. //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
  49. servletRegistrationBean.addInitParameter("deny","192.168.1.119");
  50. //登录查看信息的账号密码, 用于登录Druid监控后台
  51. servletRegistrationBean.addInitParameter("loginUsername", "admin");
  52. servletRegistrationBean.addInitParameter("loginPassword", "admin");
  53. //是否能够重置数据.
  54. servletRegistrationBean.addInitParameter("resetEnable", "true");
  55. return servletRegistrationBean;
  56. }
  57. /**
  58. * 注册Filter信息, 监控拦截器
  59. *
  60. * @return
  61. */
  62. @Bean
  63. @ConditionalOnMissingBean
  64. public FilterRegistrationBean<Filter> filterRegistrationBean() {
  65. FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<Filter>();
  66. filterRegistrationBean.setFilter(new WebStatFilter());
  67. filterRegistrationBean.addUrlPatterns("/*");
  68. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  69. return filterRegistrationBean;
  70. }
  71. }

3 查看监控

3.1 登录页面

http://localhost:9999/druid/login.html
image.png

3.2监控首页

image.png