1.Druid简介
Druid是一个JDBC组件库,包括数据库连接池、SQL Parser等组件。DruidDataSource是最好的数据库连接池
2.maven引入druid
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
3. application.properties
具体的配置文件详解 可以参照DruidDataSource配置属性列表
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#dataSource Pool configuration
spring.datasource.initialSize=5
#最小连接池数量
spring.datasource.minIdle=5
#最大连接池数量
spring.datasource.maxActive=20
#获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下#降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.druid.exceptionSorter=true
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.druid.filters=stat,wall
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 慢SQL
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
spring.datasource.useGlobalDataSourceStat=true
4.配置 Durid dataSource
/**
* dataSource 数据源配置
*/
@Configuration
@Data
public class DruidDataSourceConfiguration {
@Value("${spring.datasource.druid.filters}")
private String filters ;
@Value("${spring.datasource.druid.exceptionSorter}")
private String exceptionSorter;
/**
* @see org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.Tomcat 仿写的你可以去了解
* @return DruidDataSource
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
try {
System.out.println(filters);
dataSource.setFilters(filters);
dataSource.setExceptionSorter(exceptionSorter);
} catch (SQLException e) {
e.printStackTrace();
}
return dataSource;
}
}
5. 配置DruidStatFilter
Druid的监控统计功能是通过filter-chain扩展实现,如果你要打开监控统计功能,配置StatFilter
import com.alibaba.druid.support.http.WebStatFilter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
/**
打开监控统计功能
*/
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//忽略资源
}
)
public class DruidStatFilter extends WebStatFilter {
}
6. 配置DruidStatViewServlet
启用Druid的内置监控页面
import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* @author hu
* Created by hu on 2018/2/27.
*/
//urlPatterns 指定监控页面url
@WebServlet(urlPatterns="/druid/*",
initParams={
@WebInitParam(name="allow",value="192.168.33.108,127.0.0.1"),// IP白名单(没有配置或者为空,则允许所有访问)
@WebInitParam(name="deny",value="192.168.1.73"),// IP黑名单 (存在共同时,deny优先于allow)
@WebInitParam(name="loginUsername",value="admin"),// 用户名
@WebInitParam(name="loginPassword",value="123456"),// 密码
@WebInitParam(name="resetEnable",value="true")// 禁用HTML页面上的“Reset All”功能
}
)
public class DruidStatViewServlet extends StatViewServlet {
}
7. 添加@ServletComponentScan注解
需要在spring boot 启动类上面 添加@ServletComponentScan,否则监控页面的servlet 不会被加载。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
/**
* @author hu
* @date 2018/5/9 17:25
*/
@ServletComponentScan
@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.dream.repository")
public class DreamApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(DreamApplication.class, args);
}
}