背景
Druid连接池是阿里巴巴开源的数据库连接池项目。Druid连接池为监控而生,内置强大的监控功能,监控特性不影响性能。功能强大,能防SQL注入,内置Loging能诊断Hack应用行为。现在已经SpringBoot3,Druid的配置也需要随着更新。
SpringBoot3整合Druid
在SpringBoot3中整合Druid连接池,可以通过以下步骤进行配置:
-
添加依赖:在项目的
pom.xml
文件中添加Druid的Spring Boot Starter依赖。<druid.version>1.2.23</druid.version><!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-3-starter --> <dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-3-starter</artifactId><version>${druid.version}</version> </dependency>
同时,确保已经添加了数据库驱动的依赖,例如MySQL:
<!--<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version> </dependency>--> <dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><version>${postgresql.version}</version> </dependency>
-
配置YAML:在
application.yml
中配置数据源和Druid连接池的参数。spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghaiusername: rootpassword: root123druid:initial-size: 5min-idle: 5max-active: 20max-wait: 60000time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 30000validation-query: select 'x'test-while-idle: truetest-on-borrow: falsetest-on-return: falsepool-prepared-statements: falsefilters: stat,wall,slf4jmax-pool-prepared-statement-per-connection-size: -1use-global-data-source-stat: trueconnect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000web-stat-filter:enabled: trueurl-pattern: /*exclusions: /druid/*,*.js,*.gif,*.jpg,*.png,*.css,*.icostat-view-servlet:enabled: trueurl-pattern: /druid/*reset-enable: falselogin-username: druidlogin-password: druidallow: 127.0.0.1deny:
-
启动项目:启动Spring Boot应用后,Druid连接池将自动配置并可以使用。你可以通过访问Druid提供的监控页面来查看数据库连接池的状态。监控页面的访问地址通常是
http://localhost:8080/druid/login.html
,输入配置的用户名和密码即可登录。
以上步骤是基本方法,仅供,具体配置可能会根据Druid版本和Spring Boot版本的不同有所变化。在实际操作时,建议参考Druid的官方文档和Spring Boot的官方文档进行配置。
Druid监控
http://localhost:12666/api/system/druid/login.html
http://localhost:12666/api/system/druid/index.html
Github参考代码
当然你也可以参考SpringBoot3脚手架之MySpringBootAPI部分,下载即可开始使用,DRUID已配置。https://github.com/moshowgame/MySpringBootAPIhttps://github.com/moshowgame/MySpringBootAPI
filters=log4j "ClassNotFoundException: org.apache.log4j.Priority" issue
可以改为 filters: stat,wall,slf4j 或者干脆不要log4j和slf4j
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-09-29T19:24:22.892+08:00 ERROR 9648 --- [MySpringBootAPI] [ main] o.s.b.d.LoggingFailureAnalysisReporter : ***************************
APPLICATION FAILED TO START
***************************Description:Failed to bind properties under 'spring.datasource.druid' to com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceWrapper:Property: spring.datasource.druid.filtersValue: "stat,wall,log4j"Origin: class path resource [application-dev.yml] - 52:18Reason: java.lang.ClassNotFoundException: org.apache.log4j.PriorityAction:Update your application's configurationProcess finished with exit code 1
Application 完整YAML参考
server:port: 12666servlet:context-path: /apiundertow:threads:io: 4worker: 20url-charset: UTF-8
spring:banner:charset: UTF-8application:name: MySpringBootAPIfreemarker:request-context-attribute: requestsuffix: .htmlcontent-type: text/htmlenabled: truecache: falsecharset: UTF-8allow-request-override: falseexpose-request-attributes: trueexpose-session-attributes: trueexpose-spring-macro-helpers: truetemplate-loader-path: classpath:/templates/settings:number_format: 0.##mvc:static-path-pattern: /static/**servlet:multipart:max-file-size: 10MBmax-request-size: 10MBdatasource:type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: org.postgresql.Driverurl: jdbc:postgresql://127.0.0.1:5432/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghaiusername: postgrespassword: root123druid:initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1testWhileIdle: truetestOnBorrow: falsetestOnReturn: falsefilters: stat,wall,slf4j# 通过connectProperties属性来打开mergeSql功能;慢SQL记录connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000# 配置DruidStatFilterweb-stat-filter:enabled: trueurl-pattern: "/*"exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*,/system/druid/*"# 配置DruidStatViewServletstat-view-servlet:enabled: trueurl-pattern: "/system/druid/*"# IP白名单(没有配置或者为空,则允许所有访问)allow: localhost,127.0.0.1,192.168.*# IP黑名单 (存在共同时,deny优先于allow)deny: 10.1.*# 禁用HTML页面上的“Reset All”功能reset-enable: false# 登录名login-username: admin# 登录密码login-password: 123456