发布于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,所以可不声明版本号):
- <!-- druid -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <!--<version>1.2.6</version>-->
- </dependency>
2.application.properties配置:
推荐的配置:
- spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/yzh?useUnicode=true&characterEncoding=utf-8
- spring.datasource.druid.username=root
- spring.datasource.druid.password=root
- #spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
- spring.datasource.druid.initial-size=5
- spring.datasource.druid.max-active=20
- spring.datasource.druid.min-idle=5
- #获取连接时最大等待时间,单位毫秒
- spring.datasource.druid.max-wait=60000
- #用来检测连接是否有效
- spring.datasource.druid.validation-query=SELECT 1
- #申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
- spring.datasource.druid.test-on-borrow=false
- #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
- spring.datasource.druid.test-on-return=false
- #申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
- spring.datasource.druid.test-while-idle=true
- #间隔多久进行一次检测,检测需要关闭的空闲连接,单位是毫秒
- spring.datasource.druid.time-between-eviction-runs-millis=600000
监控有关配置:
- # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
- spring.datasource.druid.filters=stat,wall
- # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
- spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
- #是否启用StatFilter默认值false,用于采集 web-jdbc 关联监控的数据。
- spring.datasource.druid.web-stat-filter.enabled=true
- #需要监控的 url
- spring.datasource.druid.web-stat-filter.url-pattern=/*
- #排除一些静态资源,以提高效率
- spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
- #是否启用StatViewServlet(监控页面)默认值为false(考虑到安全问题默认并未启动,如需启用建议设置密码或白名单以保障安全)
- spring.datasource.druid.stat-view-servlet.enabled=true
- #内置的监控页面地址,例如 /druid/*,则内置监控页面的首页是 /druid/index.html
- spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
- #是否允许清空统计数据
- spring.datasource.druid.stat-view-servlet.reset-enable=false
- spring.datasource.druid.stat-view-servlet.login-username=admin
- 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:
- public DruidAbstractDataSource(boolean lockFair) {
- this.validationQuery = DEFAULT_VALIDATION_QUERY;
- this.validationQueryTimeout = -1;
- this.testOnBorrow = false;
- this.testOnReturn = false;
- this.testWhileIdle = true;
- this.poolPreparedStatements = false;
- this.sharePreparedStatements = false;
- this.maxPoolPreparedStatementPerConnectionSize = 10;
- this.inited = false;
- this.initExceptionThrow = true;
- this.logWriter = new PrintWriter(System.out);
- this.filters = new CopyOnWriteArrayList();
- this.clearFiltersEnable = true;
- this.exceptionSorter = null;
- this.maxWaitThreadCount = -1;
- this.accessToUnderlyingConnectionAllowed = true;
- this.timeBetweenEvictionRunsMillis = 60000L;
- this.numTestsPerEvictionRun = 3;
- this.minEvictableIdleTimeMillis = 1800000L;
- this.maxEvictableIdleTimeMillis = 25200000L;
- this.keepAliveBetweenTimeMillis = 120000L;
- this.phyTimeoutMillis = -1L;
- this.phyMaxUseCount = -1L;
- this.removeAbandonedTimeoutMillis = 300000L;
- this.maxOpenPreparedStatements = -1;
- this.timeBetweenConnectErrorMillis = 500L;
- this.validConnectionChecker = null;
- this.activeConnections = new IdentityHashMap();
- this.connectionErrorRetryAttempts = 1;
- this.breakAfterAcquireFailure = false;
- this.transactionThresholdMillis = 0L;
- this.createdTime = new Date();
- this.errorCount = 0L;
- this.dupCloseCount = 0L;
- this.startTransactionCount = 0L;
- this.commitCount = 0L;
- this.rollbackCount = 0L;
- this.cachedPreparedStatementHitCount = 0L;
- this.preparedStatementCount = 0L;
- this.closedPreparedStatementCount = 0L;
- this.cachedPreparedStatementCount = 0L;
- this.cachedPreparedStatementDeleteCount = 0L;
- this.cachedPreparedStatementMissCount = 0L;
- this.transactionHistogram = new Histogram(new long[]{1L, 10L, 100L, 1000L, 10000L, 100000L});
- this.dupCloseLogEnable = false;
- this.executeCount = 0L;
- this.executeQueryCount = 0L;
- this.executeUpdateCount = 0L;
- this.executeBatchCount = 0L;
- this.isOracle = false;
- this.isMySql = false;
- this.useOracleImplicitCache = true;
- this.activeConnectionLock = new ReentrantLock();
- this.createErrorCount = 0;
- this.creatingCount = 0;
- this.directCreateCount = 0;
- this.createCount = 0L;
- this.destroyCount = 0L;
- this.createStartNanos = 0L;
- this.useUnfairLock = null;
- this.useLocalSessionState = true;
- this.statLogger = new DruidDataSourceStatLoggerImpl();
- this.asyncCloseConnectionEnable = false;
- this.maxCreateTaskCount = 3;
- this.failFast = false;
- this.failContinuous = 0;
- this.failContinuousTimeMillis = 0L;
- this.initVariants = false;
- this.initGlobalVariants = false;
- this.onFatalError = false;
- this.onFatalErrorMaxActive = 0;
- this.fatalErrorCount = 0;
- this.fatalErrorCountLastShrink = 0;
- this.lastFatalErrorTimeMillis = 0L;
- this.lastFatalErrorSql = null;
- this.lastFatalError = null;
- this.connectionIdSeed = 10000L;
- this.statementIdSeed = 20000L;
- this.resultSetIdSeed = 50000L;
- this.transactionIdSeed = 60000L;
- this.metaDataIdSeed = 80000L;
- this.lock = new ReentrantLock(lockFair);
- this.notEmpty = this.lock.newCondition();
- this.empty = this.lock.newCondition();
- }
完整pom.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <!-- 父级支持,主要作用:引入默认配置;spring核心包、logger包、加载默认配置文件名等 -->
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.4.6</version>
- </parent>
-
- <groupId>org.example</groupId>
- <artifactId>yzh-maven</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- <java.version>1.8</java.version>
- <spring-boot.version>2.4.6</spring-boot.version>
- </properties>
-
- <dependencies>
- <!-- springboot 基础包 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
-
- <!-- springboot 测试包 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
- <!-- springboot web包 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <!-- springboot mybatis支持包 -->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.2.0</version>
- </dependency>
- <!-- springboot mysql支持包 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
-
- <!-- 公共包 -->
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.12.0</version>
- </dependency>
-
- <!-- druid -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <!--<version>1.2.6</version>-->
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <!-- 当运行“mvn package”进行打包时,会打包成一个可以直接运行的 JAR 文件,使用“java -jar”命令就可以直接运行-。 -->
- <!-- 打的包里面才会有maven依赖的jar包和spring boot的启动类(独立启动) -->
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
作者:天花灯
链接:http://www.javaheidong.com/blog/article/222571/f12c0bb6691a35a8810b/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!