连接池maven地址:
<!--druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
application.yml配置:
spring:
datasource:
# 数据源基本配置
url: jdbc:mysql://*.*.*.*:3306/lt?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: *
password: *
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 数据源其他配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
配置类
数据源绑定:
连接池监控及filter拦截器
package cn.hm1006.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
public class DruidConfig {
/**
*绑定数据源配置
*/
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource dataSource(){
return new DruidDataSource();
}
/**
* 配置监控服务器
* @return 返回监控注册的servlet对象
*/
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
//方式一:
HashMap<String, String> map = new HashMap<>();
//增强配置 登录key密码key固定的 loginUsername loginPassword
map.put("loginUsername","admin");
map.put("loginPassword","123456");
//允许谁能访问
map.put("allow","");
/* // 添加IP白名单
map.put("allow", "127.0.0.1");
// 添加IP黑名单,当白名单和黑名单重复时,黑名单优先级更高
map.put("deny", "127.0.0.1");
// 是否能够重置数据
map.put("resetEnable", "false");*/
bean.setInitParameters(map);
// //方式二:
// // 添加IP白名单
// bean.addInitParameter("allow", "127.0.0.1");
// // 添加IP黑名单,当白名单和黑名单重复时,黑名单优先级更高
// bean.addInitParameter("deny", "127.0.0.1");
// // 添加控制台管理用户
// bean.addInitParameter("loginUsername", "admin");
// bean.addInitParameter("loginPassword", "123456");
// // 是否能够重置数据
// bean.addInitParameter("resetEnable", "false");
return bean;
}
/**
* 配置服务过滤器
* @return 返回过滤器配置对象
*/
@Bean
public FilterRegistrationBean FilterRegistration(){
FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
//方式一:
bean.setFilter(new WebStatFilter());
//过滤那些请求
HashMap<String, String> initParameters = new HashMap<>();
//排除不拦截的请求、exclusions
initParameters.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParameters);
// //方式二:
// // 添加过滤规则
// bean.addUrlPatterns("/*");
// // 忽略过滤格式
// bean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,");
return bean;
}
}
配置完后启动SpringBoot程序访问:
http://localhost:8080/druid/ 就可以来到登录页面,就是上面添加的控制台管理用户,可以在上面很好的看到运行状况。
Q.E.D.