1.pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.20</version> </dependency>
2.application.properties:
# 数据库访问配置 # 主数据源,默认的 spring.druid.type=com.alibaba.druid.pool.DruidDataSource spring.druid.url=jdbc:mysql://120.26.130.187:3306/huishi-server?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.druid.username=root spring.druid.password=jsy2016memeda # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 spring.druid.initialSize=5 spring.druid.minIdle=15 spring.druid.maxActive=120 # 配置获取连接等待超时的时间 spring.druid.maxWait=10000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.druid.timeBetweenEvictionRunsMillis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.druid.minEvictableIdleTimeMillis=300000 spring.druid.validationQuery=SELECT 1 spring.druid.testWhileIdle=true spring.druid.testOnBorrow=true spring.druid.testOnReturn=false # 打开PSCache,并且指定每个连接上PSCache的大小 spring.druid.poolPreparedStatements=false spring.druid.maxPoolPreparedStatementPerConnectionSize=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.druid.filters=stat,wall,log4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多个DruidDataSource的监控数据 spring.druid.useGlobalDataSourceStat=true # 自动提交设置为false spring.druid.defaultAutoCommit=false
3.配置Druid监控统计功能
import org.springframework.boot.context.properties.ConfigurationProperties; /** * Created by xulingming on 2017/10/27. */ @ConfigurationProperties(prefix = "spring.druid") public class DruidProperties { private String type; private String url; private String username; private String password; private String driverClassName ; private int initialSize; private int minIdle; private int maxActive; private long maxWait; private long timeBetweenEvictionRunsMillis; private long minEvictableIdleTimeMillis; private String validationQuery; private boolean testWhileIdle; private boolean testOnBorrow; private boolean testOnReturn; private String poolPreparedStatements; private String maxPoolPreparedStatementPerConnectionSize; private String filters; private String connectionProperties; private boolean useGlobalDataSourceStat; private boolean defaultAutoCommit; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public int getInitialSize() { return initialSize; } public void setInitialSize(int initialSize) { this.initialSize = initialSize; } public int getMinIdle() { return minIdle; } public void setMinIdle(int minIdle) { this.minIdle = minIdle; } public int getMaxActive() { return maxActive; } public void setMaxActive(int maxActive) { this.maxActive = maxActive; } public long getMaxWait() { return maxWait; } public void setMaxWait(long maxWait) { this.maxWait = maxWait; } public long getTimeBetweenEvictionRunsMillis() { return timeBetweenEvictionRunsMillis; } public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; } public long getMinEvictableIdleTimeMillis() { return minEvictableIdleTimeMillis; } public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; } public String getValidationQuery() { return validationQuery; } public void setValidationQuery(String validationQuery) { this.validationQuery = validationQuery; } public boolean getTestWhileIdle() { return testWhileIdle; } public void setTestWhileIdle(boolean testWhileIdle) { this.testWhileIdle = testWhileIdle; } public boolean getTestOnBorrow() { return testOnBorrow; } public void setTestOnBorrow(boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } public boolean getTestOnReturn() { return testOnReturn; } public void setTestOnReturn(boolean testOnReturn) { this.testOnReturn = testOnReturn; } public String getPoolPreparedStatements() { return poolPreparedStatements; } public void setPoolPreparedStatements(String poolPreparedStatements) { this.poolPreparedStatements = poolPreparedStatements; } public String getMaxPoolPreparedStatementPerConnectionSize() { return maxPoolPreparedStatementPerConnectionSize; } public void setMaxPoolPreparedStatementPerConnectionSize(String maxPoolPreparedStatementPerConnectionSize) { this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize; } public String getFilters() { return filters; } public void setFilters(String filters) { this.filters = filters; } public String getConnectionProperties() { return connectionProperties; } public void setConnectionProperties(String connectionProperties) { this.connectionProperties = connectionProperties; } public boolean getUseGlobalDataSourceStat() { return useGlobalDataSourceStat; } public void setUseGlobalDataSourceStat(boolean useGlobalDataSourceStat) { this.useGlobalDataSourceStat = useGlobalDataSourceStat; } public boolean isTestWhileIdle() { return testWhileIdle; } public boolean isTestOnBorrow() { return testOnBorrow; } public boolean isTestOnReturn() { return testOnReturn; } public boolean isUseGlobalDataSourceStat() { return useGlobalDataSourceStat; } public boolean isDefaultAutoCommit() { return defaultAutoCommit; } public void setDefaultAutoCommit(boolean defaultAutoCommit) { this.defaultAutoCommit = defaultAutoCommit; } }
配置类:
package com.shitou.huishi.domain.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; 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 org.springframework.core.env.Environment; /** * Created by xulingming on 2017/10/27. */ @Configuration @EnableConfigurationProperties(DruidProperties.class) public class DruidConfig { @Autowired Environment environment; @Bean public ServletRegistrationBean druidServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); // IP白名单 servletRegistrationBean.addInitParameter("allow", "192.168.2.25,127.0.0.1"); // IP黑名单(共同存在时,deny优先于allow) servletRegistrationBean.addInitParameter("deny", "192.168.1.100"); //控制台管理用户 servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "admin"); //是否能够重置数据 禁用HTML页面上的“Reset All”功能 servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } @Bean DataSource dataSource(@Autowired DruidProperties druidProperties) throws Exception { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(druidProperties.getUrl()); String[] activeProfiles = environment.getActiveProfiles(); if (activeProfiles != null && activeProfiles.length > 0) { dataSource.setUsername(druidProperties.getUsername()); dataSource.setPassword(druidProperties.getPassword()); } dataSource.setDefaultAutoCommit(druidProperties.isDefaultAutoCommit()); dataSource.setDriverClassName(druidProperties.getDriverClassName()); dataSource.setInitialSize(druidProperties.getInitialSize()); dataSource.setMinIdle(druidProperties.getMinIdle()); dataSource.setMaxActive(druidProperties.getMaxActive()); dataSource.setMaxWait(druidProperties.getMaxWait()); dataSource.setTimeBetweenEvictionRunsMillis(druidProperties.getTimeBetweenEvictionRunsMillis()); dataSource.setMinEvictableIdleTimeMillis(druidProperties.getMinEvictableIdleTimeMillis()); dataSource.setValidationQuery(druidProperties.getValidationQuery()); dataSource.setTestWhileIdle(druidProperties.getTestWhileIdle()); dataSource.setTestOnBorrow(druidProperties.getTestOnBorrow()); dataSource.setTestOnReturn(druidProperties.getTestOnReturn()); try { dataSource.setFilters(druidProperties.getFilters()); } catch (SQLException e) { throw e; } return dataSource; } }
效果:
访问http://127.0.0.1:8080/druid/index.html
参考:
https://segmentfault.com/a/1190000009132815
https://blog.csdn.net/blueheart20/article/details/52384032
https://www.jianshu.com/p/c8a01ae9f779
https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter