三步配置阿里巴巴durid监控
官方文档 环境:jdk17, 框架springboot3
引入依赖
<properties>
<druid>1.2.21</druid>
<fastjson2>2.0.33</fastjson2>
<mysql.connector.j>8.2.0</mysql.connector.j>
<lombok>1.18.30</lombok>
<mybatis.plus.spring.boot3>3.5.5</mybatis.plus.spring.boot3>
</properties>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid}</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>${mysql.connector.j}</version>
</dependency>
配置文件
spring:
profiles:
active: dev
main:
allow-circular-references: true
datasource:
#1.JDBC
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: ${cookbook.datasource.driver-class-name}
url: jdbc:mysql://${cookbook.datasource.host}:${cookbook.datasource.port}/${cookbook.datasource.database}?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
username: ${cookbook.datasource.username}
password: ${cookbook.datasource.password}
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 1 FROM DUAL
test-while-idle: true
test-on-borrow: true
test-on-return: false
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
filter:
stat:
merge-sql: true
slow-sql-millis: 5000
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: true
session-stat-max-count: 100
DruidConfig配置类
@Configuration
public class DruidConfig {
@Bean
ServletRegistrationBean regisDruid() {
//固定写法,配置访问路径
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
//配置登录信息,固定写法
HashMap<String, String> initParams = new HashMap<>();
//账号和密码的key是固定的
initParams.put("loginUsername", "admin");
initParams.put("loginPassword", "xxx");
//允许谁可以访问
initParams.put("allow", "127.0.0.1");
bean.setInitParameters(initParams);
return bean;
}
}
启动
http://127.0.0.1:8080/druid/index.html