1.添加jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
2.配置文件
#数据源基本信息
spring:
datasource:
druid:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/daily?useSSl=ture&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.cj.jdbc.Driver
#连接池属性
initial-size: 15
max-active: 100
min-idle: 15
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
test-on-borrow: false
test-on-return: false
test-while-idle: true
validation-query: SELECT 1
validation-query-timeout: 1000
keep-alive: true
remove-abandoned: true
remove-abandoned-timeout: 180
log-abandoned: true
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
filters: stat,wall,slf4j
use-global-data-source-stat: true
maxOpenPreparedStatements: 100
connect-properties.mergeSql: true
connect-properties.slowSqlMillis: 5000
# 配置DruidStatFilter
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
# 配置DruidStatViewServlet
stat-view-servlet:
url-pattern: "/druid/*"
# IP白名单(没有配置或者为空,则允许所有访问)
allow: 127.0.0.1
# IP黑名单 (存在共同时,deny优先于allow)
deny: 192.168.0.1
# 禁用HTML页面上的“Reset All”功能
reset-enable: false
# 登录名
login-username: admin
# 登录密码
login-password: 123456
# 新版需要配置这个属性才能访问监控页面
enabled: true
3.如果使用的是以下这个jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
4.配置文件
spring:
datasource:
username: root
password: root
# 假如时区报错,url中添加
# url: jdbc:mysql://localhost:3306/mybatis?ServerTimezone=UTC&useUnicode=true&characterEncoding=utf-8
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#SpringBoot默认是不注入这些的,需要自己绑定
#druid数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000 #60秒
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许报错,java.lang.ClassNotFoundException: org.apache.Log4j.Properity
#则导入log4j 依赖就行
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionoProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
5.配置类
@Configuration
public class DruidConfig {
//绑定配置文件,return的DruidDataSource就能使用配置的属性
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource DruidDataSource(){
return new DruidDataSource();
}
//因为springboot内置了servlet容器,所有没有web。xml 替代方法就是将ServletRegistrationBean 注册进spring容器,
//监控需要用到Servlet,所以通过ServletRegistrationBean注册, 需要什么Servlet就在参数中new
@Bean
public ServletRegistrationBean statViewServlet(){
//固定写法, /druid/*为访问druid监控页面的路径
ServletRegistrationBean<StatViewServlet> bean=new ServletRegistrationBean<>( new StatViewServlet(),"/druid/*");
//后台需要有人登录,配置账号密码
HashMap<String, String> initParameters = new HashMap<>();
//增加配置,key是固定的,value自定义
//在 StatViewServlet 的父类中可以查看
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","111");
//允许谁访问 参数为空表示允许所有人可以访问 localhost 只允许本机访问
initParameters.put("allow","");
//禁止谁访问
initParameters.put("deny","192.168.6.51");
bean.setInitParameters(initParameters); //设置初始化参数
return bean;
}
//注册过滤器
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>(new WebStatFilter());
HashMap<String, String> initParameters = new HashMap<>();
initParameters.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParameters);
// FilterRegistrationBean bean = new FilterRegistrationBean();
// bean.setFilter(new WebStatFilter());
// HashMap<String, String> initParameters = new HashMap<>();
// //这些东西不进行过滤
// initParameters.put("exclusions","*.js,*.css,/druid/*");
// //过滤哪些请求
// bean.setInitParameters(initParameters);
return bean;
}
}
标签:stat,SpringBoot,druid,Druid,bean,整合,initParameters,new,true
From: https://www.cnblogs.com/ggzs/p/16657946.html