SpringBoot3+Druid YAML配置
Druid连接池是阿里巴巴开源的数据库连接池项目。Druid连接池为监控而生,内置强大的监控功能,监控特性不影响性能。功能强大,能防SQL注入,内置Loging能诊断Hack应用行为。现在已经SpringBoot3,Druid的配置也需要随着更新。
背景
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.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root password: root123 druid: initial-size: 5 min-idle: 5 max-active: 20 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 30000 validation-query: select 'x' test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: false filters: stat,wall,slf4j max-pool-prepared-statement-per-connection-size: -1 use-global-data-source-stat: true connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 web-stat-filter: enabled: true url-pattern: /* exclusions: /druid/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico stat-view-servlet: enabled: true url-pattern: /druid/* reset-enable: false login-username: druid login-password: druid allow: 127.0.0.1 deny:
-
启动项目:启动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.filters
Value: "stat,wall,log4j"
Origin: class path resource [application-dev.yml] - 52:18
Reason: java.lang.ClassNotFoundException: org.apache.log4j.Priority
Action:
Update your application's configuration
Process finished with exit code 1
Application 完整YAML参考
server:
port: 12666
servlet:
context-path: /api
undertow:
threads:
io: 4
worker: 20
url-charset: UTF-8
spring:
banner:
charset: UTF-8
application:
name: MySpringBootAPI
freemarker:
request-context-attribute: request
suffix: .html
content-type: text/html
enabled: true
cache: false
charset: UTF-8
allow-request-override: false
expose-request-attributes: true
expose-session-attributes: true
expose-spring-macro-helpers: true
template-loader-path: classpath:/templates/
settings:
number_format: 0.##
mvc:
static-path-pattern: /static/**
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://127.0.0.1:5432/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: postgres
password: root123
druid:
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
filters: stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
# 配置DruidStatFilter
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*,/system/druid/*"
# 配置DruidStatViewServlet
stat-view-servlet:
enabled: true
url-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
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)