spring-boot学习:九、数据库连接之集成alibaba druid

虽然spring-boot2中默认使用HikariCP作为默认连接池,而且该作者也宣传HikariCP是最快牛逼的连接池,但是,但是,但是我们还是不用。因为我们有更好的选择,阿里的druid,自带监控、中文文档、社区活跃,阿里自己都用,我们有理由不用么。

一、早期版本

1. pom.xml中引入jar包


	com.alibaba
	druid-spring-boot-starter
	1.1.10

2.配置database.properties

# database
spring.datasource.url=jdbc:mysql://localhost:3306/springboot2?characterEncoding=utf8&serverTimezone=Asia/Shanghai&autoReconnect=false&failOverReadOnly=false&maxReconnects=10&useSSL=false&rewriteBatchedStatements=true
spring.datasource.username=kevin
spring.datasource.password=123
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 可不配置,连接池自动选择

# connector pool
spring.datasource.initial-size=10
spring.datasource.max-active=50
spring.datasource.min-idle=10
spring.datasource.max-wait=60000
spring.datasource.query-timeout=10
spring.datasource.pool-prepared-statements=true
spring.datasource.max-open-prepared-statements=20
spring.datasource.test-on-borrow=false
spring.datasource.test-on-return=false
spring.datasource.test-while-idle=true
spring.datasource.asyncInit=true
spring.datasource.filters=stat,wall

3. Application.java

/**
 * prefix需要database.properties保持一致,比如spring.datasource.druid,两边都需要一样
 * @return
 */
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
//		return DataSourceBuilder.create().type(DruidDataSource.class).build();
		return DruidDataSourceBuilder.create().build();
}

@Bean
public ServletRegistrationBean druidServlet() {
	ServletRegistrationBean bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
	// IP白名单
    // bean.addInitParameter("allow", "");
	// IP黑名单(共同存在时,deny优先于allow)
    // bean.addInitParameter("deny", "192.168.1.100");
	//控制台管理用户
	bean.addInitParameter("loginUsername", "admin");
	bean.addInitParameter("loginPassword", "123");
	//是否能够重置数据 禁用HTML页面上的“Reset All”功能
	bean.addInitParameter("resetEnable", "false");
	return bean;
}

4. 测试

1)访问:http://localhost:8081/druid/index.html
spring-boot学习:九、数据库连接之集成alibaba druid_第1张图片
2)查看数据源,注意:至少访问过一次数据库此处才有数据,可以看到显示的参数与配置是一致的。
spring-boot学习:九、数据库连接之集成alibaba druid_第2张图片
3)SQL监控,可以看出哪些sql比较慢,需要优化
spring-boot学习:九、数据库连接之集成alibaba druid_第3张图片
4)URI监控,可以查看请求次数,执行快慢
spring-boot学习:九、数据库连接之集成alibaba druid_第4张图片
注意,可以看到上图中很多uri都是druid的请求,不是我们需要关注的,我们可以进行以下配置

@Bean
public FilterRegistrationBean filterRegistrationBean() {
	FilterRegistrationBean bean = new FilterRegistrationBean<>(new WebStatFilter());
	bean.addUrlPatterns("/*");
	bean.addInitParameter("sessionStatMaxCount", "1000");//缺省sessionStatMaxCount是1000个
//        bean.addInitParameter("sessionStatEnable", "false");//关闭session统计功能
	bean.addInitParameter("profileEnable", "true");//配置profileEnable能够监控单个url调用的sql列表
	bean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
	return bean;
}

然后再测试:
spring-boot学习:九、数据库连接之集成alibaba druid_第5张图片
二、最新版本

上面的demo中还有一些druid配置写在java代码中,相当于在代码中写了hardcode,这是件让人不爽的事;以下以druid目前最新版本全配置文件介绍:

1. pom.xml中引入jar包


	com.alibaba
	druid-spring-boot-starter
	1.1.20

2. database.properties加入druid配置

# mysql
spring.datasource.druid.url=jdbc:mysql://localhost:3306/springboot2?characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.druid.username=kevin
spring.datasource.druid.password=123

# connector pool
spring.datasource.druid.initial-size=10
spring.datasource.druid.max-active=50
spring.datasource.druid.min-idle=10
spring.datasource.druid.max-wait=60000
spring.datasource.druid.query-timeout=10
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-open-prepared-statements=20
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.asyncInit=true
spring.datasource.druid.filters=stat,wall

# WebStatFilter 监控web情况,默认不开启
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.profile-enable=true

# StatViewServlet druid监控页面配置,默认不开启
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123
spring.datasource.druid.stat-view-servlet.reset-enable=false
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=

3. 启动类不需要额外配置

package com.kevin.springbootstudy;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@SpringBootApplication
@PropertySource(value={"classpath:database.properties"})
public class SpringBootStudyApplication{

    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(SpringBootStudyApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);

    }
}

4. 测试,结果与上面的demo一致
spring-boot学习:九、数据库连接之集成alibaba druid_第6张图片

参考文档:
https://github.com/alibaba/druid
https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
真的是业界良心典范,要啥有啥,缺啥有啥,详细文档告诉你springboot如何继承druid

https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE 通用配置参考

你可能感兴趣的:(spring-boot)