spring boot 使用的是 2.3.3.RELEASE,mysql数据库
1.导入依赖包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
2.yml配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?
username: root
password: 123456
#这个是必须添加
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 初始化,最小,最大连接数
initial-size: 5
min-idle: 10
max-active: 20
# 获取数据库连接等待的超时时间
max-wait: 60000
time-between-eviction-runs-millis: 6000
validation-query: SELECT 1
# filters: stat,wall,log4j
3.打开链接http://localhost:8080/druid/datasource.html 检查初始化连接大小是否和设置相同
4.配置监控页面权限
@Configuration
public class DruidConfig {
@Bean
public ServletRegistrationBean druidServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new StatViewServlet());
servletRegistrationBean.addUrlMappings("/druid/*");
Map<String, String> initParameters = new HashMap<>();
initParameters.put("resetEnable", "false"); //禁用HTML页面上的“Rest All”功能
// initParameters.put("allow", "10.8.9.115"); //ip白名单(没有配置或者为空,则允许所有访问)
initParameters.put("loginUsername", "admin"); //++监控页面登录用户名
initParameters.put("loginPassword", "123456"); //++监控页面登录用户密码
initParameters.put("deny", ""); //ip黑名单
//如果某个ip同时存在,deny优先于allow
servletRegistrationBean.setInitParameters(initParameters);
return servletRegistrationBean;
}