SpringBoot:spring boot使用Druid和监控配置
Druid是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能。
Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource
业界把 Druid 和 HikariCP 做对比后,虽说 HikariCP 的性能比 Druid 高,但是因为 Druid 包括很多维度的统计和分析功能,所以这也是大家都选择使用它的原因。
下面来说明如何在 spring Boot 中配置使用Druid
整体步骤:
(1) —— Druid简单介绍,具体看官网;
(2) —— 在pom.xml配置druid依赖包;
(3) —— 配置application.properties加入数据库源类型等参数;
(4) —— 编写druid servlet和filter提供监控页面访问;
(5) —— 输入地址进行测试;
(1)添加Maven依赖 (或jar包)
|
1
2
3
4
5
|
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version></dependency> |
(2)、配置数据源相关信息
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# 数据库访问配置# 主数据源,默认的spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=123456 # 下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大spring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20# 配置获取连接等待超时的时间spring.datasource.maxWait=60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒spring.datasource.timeBetweenEvictionRunsMillis=60000# 配置一个连接在池中最小生存的时间,单位是毫秒spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=false# 打开PSCache,并且指定每个连接上PSCache的大小spring.datasource.poolPreparedStatements=truespring.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 |
需要注意的是:spring.datasource.type旧的spring boot版本是不能识别的
后话1(更新于2016年8月20日):很抱歉的是新版本也废弃了这个属性,配置完之后启动就会报错,所以现在知道的情况是1.2版本不支持,1.3版本支持,1.4版本不支持。具体信息可以看链接:http://stackoverflow.com/questions/39032368/not-able-to-set-spring-datasource-type-in-spring-boot-1-4 )
后话2(更新于2016年9月24日):spring boot 1.4.1重新支持了spring.datasource.type属性,从1.4.0的启动报异常到1.4.1支持,个人觉得这个应该是官方在升级的时候,影响到了spring.datasource.type属性的使用。不管怎么样,希望spring boot做的越来越好。
这时候启动应用就可以看到看到打印信息就是使用我们配置的数据源了:
[main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
(3) 配置监控统计功能
配置Servlet
如下是在SpringBoot项目中基于注解的配置,如果是web.xml配置,按规则配置即可。
com.kfit.base.servlet.DruidStatViewServlet :
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.kfit.base.servlet; import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet; import com.alibaba.druid.support.http.StatViewServlet; /** * druid数据源状态监控. * @author Administrator * */ @WebServlet(urlPatterns="/druid/*", initParams={ @WebInitParam(name="allow",value="192.168.1.72,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="false")// 禁用HTML页面上的“Reset All”功能 })public class DruidStatViewServlet extends StatViewServlet{ privatestaticfinallongserialVersionUID = 1L; } |
配置Filter
com.kfit.base.servlet.DruidStatFilter :
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.kfit.base.servlet; import javax.servlet.annotation.WebFilter;import javax.servlet.annotation.WebInitParam; import com.alibaba.druid.support.http.WebStatFilter; /** * druid过滤器. * @author Administrator * */@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*", initParams={ @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//忽略资源 })public class DruidStatFilter extends WebStatFilter{ } |
最后在App.java类上加上注解:@ServletComponentScan是的spring能够扫描到我们自己编写的servlet和filter。
注意不要忘记在 SpringBootSampleApplication.java 上添加 @ServletComponentScan 注解,不然就是404了。
然后启动项目后访问 http://127.0.0.1:8080/druid/index.html 即可查看数据源及SQL统计等。
(4)配置监控系统方式二:
以上配置的监控方式是使用了原生的servlet,filter方式,然后通过@ServletComponentScan进行启动扫描包的方式进行处理的,你会发现我们的servlet,filter根本没有任何的编码。
在这里我们将使用另外一种方式进行处理:使用代码注册Servlet:
编写类:com.kfit.base.servlet.DruidConfiguration :
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package com.kfit.base.servlet; import org.springframework.boot.context.embedded.FilterRegistrationBean;import org.springframework.boot.context.embedded.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; import com.alibaba.druid.support.http.StatViewServlet;import com.alibaba.druid.support.http.WebStatFilter; /** * druid 配置. * * 这样的方式不需要添加注解:@ServletComponentScan * @author Administrator * */@Configurationpublic class DruidConfiguration { /** * 注册一个StatViewServlet * @return */ @Bean public ServletRegistrationBean DruidStatViewServle2(){ //org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(newStatViewServlet(),"/druid2/*"); //添加初始化参数:initParams //白名单: servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page. servletRegistrationBean.addInitParameter("deny","192.168.1.73"); //登录查看信息的账号密码. servletRegistrationBean.addInitParameter("loginUsername","admin2"); servletRegistrationBean.addInitParameter("loginPassword","123456"); //是否能够重置数据. servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; } /** * 注册一个:filterRegistrationBean * @return */ @Bean public FilterRegistrationBean druidStatFilter2(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(newWebStatFilter()); //添加过滤规则. filterRegistrationBean.addUrlPatterns("/*"); //添加不需要忽略的格式信息. filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*"); return filterRegistrationBean; } } |
启动应用就可以访问:http://127.0.0.1:8080/druid2/index.html输入账号和密码:admin2/123456 就可以访问了。
SpringBoot:spring boot使用Druid和监控配置的更多相关文章
- Spring Boot使用Druid和监控配置
Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource 整体步骤: (1) -- Druid简单介绍,具体看官网: (2) ...
- 【转】spring boot使用Druid和监控配置
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012100371/article/details/76602612 Druid是Java语言中最好 ...
- 15、Spring Boot使用Druid和监控配置【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52001740目录(?)[-] 1添加Maven依赖 或jar包 2配置数据源相关信息 3 ...
- (15)Spring Boot使用Druid和监控配置【从零开始学Spring Boot】
Spring Boot 系列博客] 更多查看博客:http://412887952-qq-com.iteye.com/blog Spring Boot默认的数据源是:org.apache.tomcat ...
- Spring Boot开启Druid数据库监控功能
Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JDBC兼容的数据库,包括Oracle.MySQL.Derby.PostgreSQL.SQL Server.H2等.D ...
- Spring Boot 中 Druid 的监控页面配置
Druid的性能相比HikariCp等其他数据库连接池有一定的差距,但是数据库的相关属性的监控,别的连接池可能还追不上,如图: 今天写一下 Spring Boot 中监控页面的配置,我是直接将seat ...
- spring boot +mybatis+druid 多数据源配置
因为我的工程需要在两个数据库中操作数据,所以要配置两个数据库,我这里没有数据源没有什么主从之分,只是配合多数据源必须要指定一个主数据源,所以我就把 操作相对要对的那个数据库设置为主数据(dataBas ...
- (16)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】
在上一节使用是配置文件的方式进行使用druid,这里在扩散下使用编程式进行使用Druid,在上一节我们新建了一个类:DruidConfiguration我在这个类进行编码: package com.k ...
- Spring Boot [使用 Druid 数据库连接池]
导读 最近一段时间比较忙,以至于很久没有更新Spring Boot系列文章,恰好最近用到Druid, 就将Spring Boot 使用 Druid作为数据源做一个简单的介绍. Druid介绍: Dru ...
随机推荐
- 解释HTTP中Get和Post。它们有什么区别,哪个使用时更加安全?
Get和Post都是浏览器向网页服务器提交数据的方法. Get把要提交的数据编码在url中,比如/workinfo.jsp/mianshiti?key1=value1&key2=value2中 ...
- PHP 封装POD 类
使用POD的过程 //1.造DSN:驱动名:dbname=数据库名;host=服务器地址 $dsn = "mysql:dbname=mydb;host=localhost"; // ...
- Redis常见报错之 Redis::CommandError (MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk)
在Redis运行过程中,报错信息如下: Redis::CommandError (MISCONF Redis is configured to save RDB snapshots, but it i ...
- 微信小程序开发工具下载以及安装教程
微信公众平台上登录你的微信小程序账号 登录进入小程序开发-工具-下载,再根据你的系统选择相对应的版本地址进行下载. 以管理员身份运行下载,点击下一步,如图所示: 下一步,就会出现许可证协议 ...
- shell 部分语法
语法: variable_name=${variable_name:-xxxx} 如果variable 已经有值,则不被新值覆盖,否则将新值赋给variable split命令切割文件
- Java Web开发中路径问题小结(getRequestUrl getContextUrl getServletUrl)
看以博客感觉不错,分享一下http://www.cnblogs.com/tianguook/archive/2012/08/31/2665755.html (1) Web开发中路径的几个基本概念 假设 ...
- mariadb面试
[mariadb主从架构的工作原理] 主节点写入数据以后,保存到二进制文件中,从节点生成IO线程和sql线程,IO线程请求读取二进制文件:主节点生成的dump线程,将数据发送到中继日志中,sql线程读 ...
- vue-router懒加载
require.ensure(dependencies:String [],callback:function(require),errorCallback:function(error),chunk ...
- 数据库高级数据库学习--上机练习7(Transact-SQL 函数定义和调用)
上机练习7 在Transact SQL中,有一类特殊的自定义函数,其返回值为一张表,该类自定义函数被称作内嵌(联)表值函数,其基本语句格式如下: CREATE FUNCTION函数名称[( {@参数名 ...
- Introduction to pointers in C
The basic purpose of developing a C programming tutorial for this website – CircuitsToday – is to ma ...