Spring Boot 使用 Druid 和监控配置
2016-08-21 10:47
971 查看
转:http://412887952-qq-com.iteye.com/blog/2292362,http://blog.csdn.net/catoop/article/details/50925337
Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource
整体步骤:
(1) —— Druid简单介绍,具体看官网;
(2) —— 在pom.xml配置druid依赖包;
(3) —— 配置application.properties加入数据库源类型等参数;
(4) —— 编写druid servlet和filter提供监控页面访问;
(5) —— 输入地址进行测试;
1、添加Maven依赖 (或jar包)
配置Servlet
如下是在SpringBoot项目中基于注解的配置,如果是web.xml配置,按规则配置即可。
最后在Application.java类上加上注解:@ServletComponentScan是的spring能够扫描到我们自己编写的servlet和filter。
注意不要忘记在Application.java 上添加 @ServletComponentScan 注解,不然就是404了。
然后启动项目后访问 http://127.0.0.1:8080/druid/index.html 即可查看数据源及SQL统计等。
需要注意的是:spring.datasource.type旧的spring
boot版本是不能识别的。(后话:更新于2016年8月20日,很抱歉的是新版本也废弃了这个属性,配置完之后启动就会报错,所以现在知道的情况是1.2版本不支持,1.3版本支持,1.4版本不支持,
1.4版本可以新建类去实现EnvironmentAware接口和@Configuration@EnableTransactionManagement的方式注入druid数据源
application.properties
Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource
整体步骤:
(1) —— Druid简单介绍,具体看官网;
(2) —— 在pom.xml配置druid依赖包;
(3) —— 配置application.properties加入数据库源类型等参数;
(4) —— 编写druid servlet和filter提供监控页面访问;
(5) —— 输入地址进行测试;
1、添加Maven依赖 (或jar包)
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency>application.properties
# 数据库访问配置 # 主数据源,默认的 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置获取连接等待超时的时间 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.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=true3、配置监控统计功能
配置Servlet
如下是在SpringBoot项目中基于注解的配置,如果是web.xml配置,按规则配置即可。
package com.example.servlet; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; /** * druid数据源状态监控 * * @author Administrator * */ @WebServlet(urlPatterns = "/druid/*", initParams = { @WebInitParam(name = "allow", value = "192.168.16.110,127.0.0.1"), // IP白名单// (没有配置或者为空,则允许所有访问) @WebInitParam(name = "deny", value = "192.168.1.111"), // IP黑名单// (存在共同时,deny优先于allow) @WebInitParam(name = "loginUsername", value = "root"), // 用户名 @WebInitParam(name = "loginPassword", value = "root"), // 密码 @WebInitParam(name = "resetEnable", value = "false")// 禁用HTML页面上的“Reset// All”功能 }) public class DruidStatViewServlet extends com.alibaba.druid.support.http.StatViewServlet{ /** * */ private static final long serialVersionUID = -2419291060366082741L; }配置Filter
package com.example.servlet; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import com.alibaba.druid.support.http.WebStatFilter; @WebFilter(filterName="druidWebStatFilter",urlPatterns="/*", initParams={ @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源 }) public class DruidStatFilter extends WebStatFilter{ }
最后在Application.java类上加上注解:@ServletComponentScan是的spring能够扫描到我们自己编写的servlet和filter。
注意不要忘记在Application.java 上添加 @ServletComponentScan 注解,不然就是404了。
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @ServletComponentScan @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } /*@Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(new DruidStatViewServlet(), "/druid/*");// ServletName默认值为首字母小写,即myServlet }*/ /* * @Autowired private Environment env; * * @Bean public DataSource dataSource() { DruidDataSource dataSource = new * DruidDataSource(); * dataSource.setUrl(env.getProperty("spring.datasource.url")); * dataSource.setUsername(env.getProperty("spring.datasource.username"));// * 用户名 * dataSource.setPassword(env.getProperty("spring.datasource.password"));// * 密码 dataSource.setDriverClassName(env.getProperty( * "spring.datasource.driver-class-name")); dataSource.setInitialSize(2); * dataSource.setMaxActive(20); dataSource.setMinIdle(0); * dataSource.setMaxWait(60000); dataSource.setValidationQuery("SELECT 1"); * dataSource.setTestOnBorrow(false); dataSource.setTestWhileIdle(true); * dataSource.setPoolPreparedStatements(false); return dataSource; } */ }
然后启动项目后访问 http://127.0.0.1:8080/druid/index.html 即可查看数据源及SQL统计等。
需要注意的是:spring.datasource.type旧的spring
boot版本是不能识别的。(后话:更新于2016年8月20日,很抱歉的是新版本也废弃了这个属性,配置完之后启动就会报错,所以现在知道的情况是1.2版本不支持,1.3版本支持,1.4版本不支持,
1.4版本可以新建类去实现EnvironmentAware接口和@Configuration@EnableTransactionManagement的方式注入druid数据源
package com.example.configuration; import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.alibaba.druid.pool.DruidDataSource; @Configuration @EnableTransactionManagement public class DataBaseConfiguration implements EnvironmentAware { private RelaxedPropertyResolver propertyResolver; @Override public void setEnvironment(Environment env) { this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource."); } @Bean(destroyMethod = "close", initMethod = "init") public DataSource writeDataSource() throws SQLException { System.out.println("注入druid!!!"); DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(propertyResolver.getProperty("url")); dataSource.setUsername(propertyResolver.getProperty("username"));//用户名 dataSource.setPassword(propertyResolver.getProperty("password"));//密码 dataSource.setDriverClassName(propertyResolver.getProperty("driver-class-name")); dataSource.setInitialSize(Integer.parseInt(propertyResolver.getProperty("initialSize"))); dataSource.setMaxActive(Integer.parseInt(propertyResolver.getProperty("maxActive"))); dataSource.setMinIdle(Integer.parseInt(propertyResolver.getProperty("minIdle"))); dataSource.setMaxWait(Integer.parseInt(propertyResolver.getProperty("maxWait"))); dataSource.setTimeBetweenEvictionRunsMillis(Integer.parseInt(propertyResolver.getProperty("timeBetweenEvictionRunsMillis"))); dataSource.setMinEvictableIdleTimeMillis(Integer.parseInt(propertyResolver.getProperty("minEvictableIdleTimeMillis"))); dataSource.setValidationQuery(propertyResolver.getProperty("validationQuery")); dataSource.setTestOnBorrow(Boolean.getBoolean(propertyResolver.getProperty("testOnBorrow"))); dataSource.setTestWhileIdle(Boolean.getBoolean(propertyResolver.getProperty("testWhileIdle"))); dataSource.setTestOnReturn(Boolean.getBoolean(propertyResolver.getProperty("testOnReturn"))); dataSource.setPoolPreparedStatements(Boolean.getBoolean(propertyResolver.getProperty("poolPreparedStatements"))); dataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(propertyResolver.getProperty("maxPoolPreparedStatementPerConnectionSize"))); //配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 dataSource.setFilters(propertyResolver.getProperty("filters")); return dataSource; } }
application.properties
######################################################## ###datasource ######################################################## # 数据库访问配置 # 主数据源,默认的 #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置获取连接等待超时的时间 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.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
相关文章推荐
- Java 接口和抽象类的区别
- java.io.IOException Connection Reset by Peer 的原因是什么
- leetcode-java-326. Power of Three
- java 基础学习之线程--多线程并发及线程锁
- Java方向软件笔试题
- JAVA 注解的几大作用及使用方法详解
- 20145234黄斐《Java程序设计》第二周学习总结
- Java IO系列(三):DataInputStream,DataOutputStream和RandomAccessFile
- Java基本数据类型
- Java程序开发中如何应用线程
- JAVA实现双向链表终极解析!!熟练使用接口
- java arrays sort
- 介绍一下Java的事务处理
- 某公司Java笔试题
- 将java类对象序列化到文件里
- 软件开发工程师(JAVA)笔试题
- java常见的内存资源分析、性能分析工具有哪些?
- JAVA开发笔试题 ; 综合知识部分
- Java Q & A
- java基础笔试题