程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

springboot下使用druid-spring-boot-starter

发布于2021-06-12 15:26     阅读(560)     评论(0)     点赞(6)     收藏(4)


Druid声称是Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能。spring boot starter自动装配组件,简化组件引入的开发工作量,所以Druid推出了druid-spring-boot-starter。

1.pom.xml引入依赖包(parent中声明了spring-boot-starter-parent,所以可不声明版本号):

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

2.application.properties配置:

推荐的配置: 

  1. spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/yzh?useUnicode=true&characterEncoding=utf-8
  2. spring.datasource.druid.username=root
  3. spring.datasource.druid.password=root
  4. #spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
  5. spring.datasource.druid.initial-size=5
  6. spring.datasource.druid.max-active=20
  7. spring.datasource.druid.min-idle=5
  8. #获取连接时最大等待时间,单位毫秒
  9. spring.datasource.druid.max-wait=60000
  10. #用来检测连接是否有效
  11. spring.datasource.druid.validation-query=SELECT 1
  12. #申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
  13. spring.datasource.druid.test-on-borrow=false
  14. #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
  15. spring.datasource.druid.test-on-return=false
  16. #申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
  17. spring.datasource.druid.test-while-idle=true
  18. #间隔多久进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  19. spring.datasource.druid.time-between-eviction-runs-millis=600000

监控有关配置:

  1. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  2. spring.datasource.druid.filters=stat,wall
  3. # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
  4. spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
  5. #是否启用StatFilter默认值false,用于采集 web-jdbc 关联监控的数据。
  6. spring.datasource.druid.web-stat-filter.enabled=true
  7. #需要监控的 url
  8. spring.datasource.druid.web-stat-filter.url-pattern=/*
  9. #排除一些静态资源,以提高效率
  10. spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
  11. #是否启用StatViewServlet(监控页面)默认值为false(考虑到安全问题默认并未启动,如需启用建议设置密码或白名单以保障安全)
  12. spring.datasource.druid.stat-view-servlet.enabled=true
  13. #内置的监控页面地址,例如 /druid/*,则内置监控页面的首页是 /druid/index.html
  14. spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
  15. #是否允许清空统计数据
  16. spring.datasource.druid.stat-view-servlet.reset-enable=false
  17. spring.datasource.druid.stat-view-servlet.login-username=admin
  18. spring.datasource.druid.stat-view-servlet.login-password=admin

添加了监控配置后,启动项目访问http://localhost:8080/druid/,输入上面配置的用户名admin、密码admin,可看到如下监控页面:

官方文档:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

druid-spring-boot-starter的默认属性配置类DruidAbstractDataSource

  1. public DruidAbstractDataSource(boolean lockFair) {
  2. this.validationQuery = DEFAULT_VALIDATION_QUERY;
  3. this.validationQueryTimeout = -1;
  4. this.testOnBorrow = false;
  5. this.testOnReturn = false;
  6. this.testWhileIdle = true;
  7. this.poolPreparedStatements = false;
  8. this.sharePreparedStatements = false;
  9. this.maxPoolPreparedStatementPerConnectionSize = 10;
  10. this.inited = false;
  11. this.initExceptionThrow = true;
  12. this.logWriter = new PrintWriter(System.out);
  13. this.filters = new CopyOnWriteArrayList();
  14. this.clearFiltersEnable = true;
  15. this.exceptionSorter = null;
  16. this.maxWaitThreadCount = -1;
  17. this.accessToUnderlyingConnectionAllowed = true;
  18. this.timeBetweenEvictionRunsMillis = 60000L;
  19. this.numTestsPerEvictionRun = 3;
  20. this.minEvictableIdleTimeMillis = 1800000L;
  21. this.maxEvictableIdleTimeMillis = 25200000L;
  22. this.keepAliveBetweenTimeMillis = 120000L;
  23. this.phyTimeoutMillis = -1L;
  24. this.phyMaxUseCount = -1L;
  25. this.removeAbandonedTimeoutMillis = 300000L;
  26. this.maxOpenPreparedStatements = -1;
  27. this.timeBetweenConnectErrorMillis = 500L;
  28. this.validConnectionChecker = null;
  29. this.activeConnections = new IdentityHashMap();
  30. this.connectionErrorRetryAttempts = 1;
  31. this.breakAfterAcquireFailure = false;
  32. this.transactionThresholdMillis = 0L;
  33. this.createdTime = new Date();
  34. this.errorCount = 0L;
  35. this.dupCloseCount = 0L;
  36. this.startTransactionCount = 0L;
  37. this.commitCount = 0L;
  38. this.rollbackCount = 0L;
  39. this.cachedPreparedStatementHitCount = 0L;
  40. this.preparedStatementCount = 0L;
  41. this.closedPreparedStatementCount = 0L;
  42. this.cachedPreparedStatementCount = 0L;
  43. this.cachedPreparedStatementDeleteCount = 0L;
  44. this.cachedPreparedStatementMissCount = 0L;
  45. this.transactionHistogram = new Histogram(new long[]{1L, 10L, 100L, 1000L, 10000L, 100000L});
  46. this.dupCloseLogEnable = false;
  47. this.executeCount = 0L;
  48. this.executeQueryCount = 0L;
  49. this.executeUpdateCount = 0L;
  50. this.executeBatchCount = 0L;
  51. this.isOracle = false;
  52. this.isMySql = false;
  53. this.useOracleImplicitCache = true;
  54. this.activeConnectionLock = new ReentrantLock();
  55. this.createErrorCount = 0;
  56. this.creatingCount = 0;
  57. this.directCreateCount = 0;
  58. this.createCount = 0L;
  59. this.destroyCount = 0L;
  60. this.createStartNanos = 0L;
  61. this.useUnfairLock = null;
  62. this.useLocalSessionState = true;
  63. this.statLogger = new DruidDataSourceStatLoggerImpl();
  64. this.asyncCloseConnectionEnable = false;
  65. this.maxCreateTaskCount = 3;
  66. this.failFast = false;
  67. this.failContinuous = 0;
  68. this.failContinuousTimeMillis = 0L;
  69. this.initVariants = false;
  70. this.initGlobalVariants = false;
  71. this.onFatalError = false;
  72. this.onFatalErrorMaxActive = 0;
  73. this.fatalErrorCount = 0;
  74. this.fatalErrorCountLastShrink = 0;
  75. this.lastFatalErrorTimeMillis = 0L;
  76. this.lastFatalErrorSql = null;
  77. this.lastFatalError = null;
  78. this.connectionIdSeed = 10000L;
  79. this.statementIdSeed = 20000L;
  80. this.resultSetIdSeed = 50000L;
  81. this.transactionIdSeed = 60000L;
  82. this.metaDataIdSeed = 80000L;
  83. this.lock = new ReentrantLock(lockFair);
  84. this.notEmpty = this.lock.newCondition();
  85. this.empty = this.lock.newCondition();
  86. }

完整pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <!-- 父级支持,主要作用:引入默认配置;spring核心包、logger包、加载默认配置文件名等 -->
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>2.4.6</version>
  11. </parent>
  12. <groupId>org.example</groupId>
  13. <artifactId>yzh-maven</artifactId>
  14. <version>1.0-SNAPSHOT</version>
  15. <properties>
  16. <maven.compiler.source>8</maven.compiler.source>
  17. <maven.compiler.target>8</maven.compiler.target>
  18. <java.version>1.8</java.version>
  19. <spring-boot.version>2.4.6</spring-boot.version>
  20. </properties>
  21. <dependencies>
  22. <!-- springboot 基础包 -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter</artifactId>
  26. </dependency>
  27. <!-- springboot 测试包 -->
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. <!-- springboot web包 -->
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-web</artifactId>
  37. </dependency>
  38. <!-- springboot mybatis支持包 -->
  39. <dependency>
  40. <groupId>org.mybatis.spring.boot</groupId>
  41. <artifactId>mybatis-spring-boot-starter</artifactId>
  42. <version>2.2.0</version>
  43. </dependency>
  44. <!-- springboot mysql支持包 -->
  45. <dependency>
  46. <groupId>mysql</groupId>
  47. <artifactId>mysql-connector-java</artifactId>
  48. </dependency>
  49. <!-- 公共包 -->
  50. <dependency>
  51. <groupId>org.apache.commons</groupId>
  52. <artifactId>commons-lang3</artifactId>
  53. <version>3.12.0</version>
  54. </dependency>
  55. <!-- druid -->
  56. <dependency>
  57. <groupId>com.alibaba</groupId>
  58. <artifactId>druid-spring-boot-starter</artifactId>
  59. <!--<version>1.2.6</version>-->
  60. </dependency>
  61. </dependencies>
  62. <build>
  63. <plugins>
  64. <plugin>
  65. <!-- 当运行“mvn package”进行打包时,会打包成一个可以直接运行的 JAR 文件,使用“java -jar”命令就可以直接运行-。 -->
  66. <!-- 打的包里面才会有maven依赖的jar包和spring boot的启动类(独立启动) -->
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-maven-plugin</artifactId>
  69. </plugin>
  70. </plugins>
  71. </build>
  72. </project>

 



所属网站分类: 技术文章 > 博客

作者:天花灯

链接:http://www.javaheidong.com/blog/article/222571/f12c0bb6691a35a8810b/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

6 0
收藏该文
已收藏

评论内容:(最多支持255个字符)