参考网页
为何要集成Druid
性能
官方数据Benchmark_aliyun,druid在响应时间上优于其他几个线程池。非官方的测试数据可能差距没这么明显,但仍然高于其他几个线程池。
自带监控功能
自带监控,可帮助开发者找出慢查询,查看并发数等。
步骤1--pom文件中加入Druid依赖
com.alibaba druid 1.0.31
步骤2--在application.properties配置文件中加入druid配置
############################################# Druid参数# 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置获取连接等待超时的时间 spring.datasource.maxWait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 # 校验SQL,Oracle配置 spring.datasource.validationQuery=SELECT 1 FROM DUAL,如果不配validationQuery项,则下面三项配置无用 spring.datasource.validationQuery=SELECT 'x' spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打开PSCache,并且指定每个连接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.filters=stat,wall,log4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多个DruidDataSource的监控数据 spring.datasource.useGlobalDataSourceStat=true
步骤3--配置WebFilter【过滤不需要监控的后缀】
package com.richfit.ruiche.common.druidConfig;import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import com.alibaba.druid.support.http.WebStatFilter; @WebFilter(filterName="druidStatFilter",urlPatterns="/*", initParams={ @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源 }) public class DruidStatFilter extends WebStatFilter { }
步骤4--配置WebServlet【监控视图配置】
package com.richfit.ruiche.common.druidConfig;import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import com.alibaba.druid.support.http.StatViewServlet; @WebServlet(urlPatterns = "/druid/*", initParams={ @WebInitParam(name="allow",value=""),// IP白名单 (没有配置或者为空,则允许所有访问) // @WebInitParam(name="deny",value="192.168.0.0"),// IP黑名单 (存在共同时,deny优先于allow) @WebInitParam(name="loginUsername",value="admin"),// druid监控页面登陆用户名 @WebInitParam(name="loginPassword",value="admin"),// druid监控页面登陆密码 @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能 }) public class DruidStatViewServlet extends StatViewServlet { /** * */ private static final long serialVersionUID = 1L; }
步骤5--不使用spring boot自动配置功能,手动初始化DataSource
package com.richfit.ruiche.common.druidConfig;import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import com.alibaba.druid.pool.DruidDataSource; /** * * 描述:如果不使用代码手动初始化DataSource的话,监控界面的SQL监控会没有数据("是spring boot的bug???") * @author chhliu * 创建时间:2017年2月9日 下午10:33:08 * @version 1.2.0 */ @Configuration public class DruidConfiguration { @Value("${spring.datasource.url}") private String dbUrl; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; // @Value("${spring.datasource.driverClassName}") @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Value("${spring.datasource.initialSize}") private int initialSize; @Value("${spring.datasource.minIdle}") private int minIdle; @Value("${spring.datasource.maxActive}") private int maxActive; @Value("${spring.datasource.maxWait}") private int maxWait; @Value("${spring.datasource.timeBetweenEvictionRunsMillis}") private int timeBetweenEvictionRunsMillis; @Value("${spring.datasource.minEvictableIdleTimeMillis}") private int minEvictableIdleTimeMillis; @Value("${spring.datasource.validationQuery}") private String validationQuery; @Value("${spring.datasource.testWhileIdle}") private boolean testWhileIdle; @Value("${spring.datasource.testOnBorrow}") private boolean testOnBorrow; @Value("${spring.datasource.testOnReturn}") private boolean testOnReturn; @Value("${spring.datasource.poolPreparedStatements}") private boolean poolPreparedStatements; @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}") private int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.filters}") private String filters; @Value("${spring.datasource.connectionProperties}") private String connectionProperties; @Value("${spring.datasource.useGlobalDataSourceStat}") private boolean useGlobalDataSourceStat; @Bean //声明其为Bean实例 @Primary //在同样的DataSource中,首先使用被标注的DataSource public DataSource dataSource(){ DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.dbUrl); 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); datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat); try { datasource.setFilters(filters); } catch (SQLException e) { System.err.println("druid configuration initialization filter: "+ e); } datasource.setConnectionProperties(connectionProperties); return datasource; } }
步骤6--主启动类上设置扫描Servlet
package com.richfit.ruiche;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.annotation.EnableScheduling;import com.richfit.ruiche.common.globalSetting.RunEnvSetting;import com.richfit.ruiche.framework.threadPool.TaskThreadPoolConfig;import com.richfit.ruiche.util.EMailConfig;//@SpringBootApplication(exclude = SpringDataWebAutoConfiguration.class)//@EnableConfigurationProperties({EMailConfig.class,SSLEMailConfig.class,WebVersion.class})@SpringBootApplication@EnableConfigurationProperties({EMailConfig.class,RunEnvSetting.class,TaskThreadPoolConfig.class}) //@RestController//@Configuration //@ComponentScan(basePackages = "com.richfit.ruiche")//@EnableAutoConfiguration@EnableScheduling//@EnableCaching//开启对异步任务的支持@EnableAsync//注意要加上@ServletComponentScan注解,否则Servlet无法生效 -----Druid监控@ServletComponentScanpublic class RichfitCBMRest { public static void main(String[] args) { SpringApplication.run(RichfitCBMRest.class, args); //打印系统中定义的服务类 // ApplicationContext ctx = SpringApplication.run(RichfitCBMRest.class,// args);// String[] beanNames = ctx.getBeanNamesForAnnotation(Service.class);// System.out.println("Service注解beanNames个数:" + beanNames.length);//// for (String bn : beanNames) {// System.out.println(bn);// }//// System.out.println("打印结束"); }}
测试
监控页面登录地址
Sql监控
可以看到sql监控页面已经监控到sql语句
Uri监控
Uri监控页面,可以看到监控到了刚才访问的uri地址:
/LoginService/login