这篇文章记录一下springboot
具体是怎么样配合druid
数据源,和druid
监控的。更多的知识点讲解,可以查看文章末尾处的相关链接。
辣么多的连接池技术,为什么偏偏采用druid
连接池?具体原因查看文章末尾的相关链接的:数据库连接池性能比对
Druid
是Java
语言中最好的数据库连接池。Druid
能够提供强大的监控和扩展功能。
创建一个空的springboot
项目,引入以下几个依赖:
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.16version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
在application.yml
中写入以下druid
核心配置:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
platform: mysql
url: jdbc:mysql://127.0.0.1:3306/springboot-druid?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT1FROMDUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
filters: stat,wall,log4j
logSlowSql: true
这里核心配置的具体相关说明,看单词的字面意思,我相信大家应该都能认识的。接下来,配置druid
监控的相关信息。
在启动类能扫面到的包之下,增加一个配置类,配置类的内容如下:
@Slf4j
@Configuration
public class DruidConfig {
private static final String DB_PREFIX = "spring.datasource";
@Bean
public ServletRegistrationBean druidServlet() {
log.info("初始化druid servlet配置 ");
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
// IP白名单
servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
// IP黑名单(共同存在时,deny优先于allow)
servletRegistrationBean.addInitParameter("deny", "192.168.1.100");
//控制台管理用户
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
//是否能够重置数据 禁用HTML页面上的“Reset All”功能
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
//解决 spring.datasource.filters=stat,wall,log4j 无法正常注册进去
@Data
@Component
@ConfigurationProperties(prefix = DB_PREFIX)
class IDataSourceProperties {
private String url;
private String username;
private String password;
private String driverClassName;
private int initialSize;
private int minIdle;
private int maxActive;
private int maxWait;
private int timeBetweenEvictionRunsMillis;
private int minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxPoolPreparedStatementPerConnectionSize;
private String filters;
private String connectionProperties;
@Bean //声明其为Bean实例
@Primary //在同样的DataSource中,首先使用被标注的DataSource
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
//configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
System.err.println("ruid配置初始化filter: " + e);
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
}
}
需要特别注意的点
new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
这里配置的ServletRegistrationBean
的第二个参数就是druid
监控的入口,也就是说访问地址为:http://localhost:8080/druid
。
servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
不配置白名单为127.0.0.1
的话,是不能直接访问的,同理,将来项目部署到服务器上,也是不开放 IP 地址,是不能访问监控信息的。
servletRegistrationBean.addInitParameter("deny", "192.168.1.100");
IP 黑名单与 IP 白名单共同存在时,deny 优先于 allow。
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
控制台管理用户名,和密码,前面的参数名称是固定的。
servletRegistrationBean.addInitParameter("resetEnable", "false");
是否能够重置数据,false
则为禁用HTML
页面上的Reset All
功能。
配置完成以上相关信息之后,启动项目,浏览器中访问http://localhost:8080/druid
,输入配置的用户名、密码,即可查看详细的 druid 监控信息。
SpringBoot 配置 Druid 连接池
阿里巴巴 Druid 官方 Github
阿里巴巴 Druid 中文文档
数据库连接池性能比对