配置篇
① 在pom文件中添加druid依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
② 在配置文件中添加相关配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&useSSL=true&serverTimezone=GMT%2b8
username: root
password: root
filters:
commons-log:
connection-logger-name: stat,wall,log4j
dbcp2:
min-idle: 5
initial-size: 5
max-total: 10
max-wait-millis: 200
③ 添加configure配置
@Configuration
public class DruidConfig {
@Bean
public ServletRegistrationBean druidServlet() {
// 主要实现web监控的配置处理
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
new StatViewServlet(), "/druid/*");//表示进行druid监控的配置处理操作
servletRegistrationBean.addInitParameter("allow", "127.0.0.1,129.168.1.11");//白名单
servletRegistrationBean.addInitParameter("deny", "129.168.1.12");//黑名单
servletRegistrationBean.addInitParameter("loginUsername", "root");//用户名
servletRegistrationBean.addInitParameter("loginPassword", "root");//密码
servletRegistrationBean.addInitParameter("resetEnable", "false");//是否可以重置数据源
return servletRegistrationBean;
}
@Bean //监控
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");//所有请求进行监控处理
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");//排除
return filterRegistrationBean;
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
}
这样基本就完成了druid的配置,启动后可以访问页面查看具体参数
访问地址:http://localhost:port/projectName/druid/login.html
跳出这个界面就算成功了。
之后的调优使用将在第二块中进行补充
使用篇