SpringBoot使⽤Druid和监控配置⽅法Spring Boot默认的数据源是:at.jdbc.pool.DataSource
Druid是Java语⾔中最好的数据库连接池,并且能够提供强⼤的监控和扩展功能。
下⾯来说明如何在 Spring Boot 中配置使⽤Druid
(1)添加Maven依赖 (或jar包)\
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
2)、配置数据源相关信息
# 数据库访问配置
# 主数据源,默认的
pe=com.alibaba.druid.pool.DruidDataSource
spring.datasource.sql.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
stWhileIdle=true
stOnBorrow=false
stOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的⼤⼩
druid连接池配置详解
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界⾯sql⽆法统计,‘wall‘⽤于防⽕墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
tionProperties=Sql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true
(3) 配置监控统计功能
配置Servlet
如下是在SpringBoot项⽬中基于注解的配置,如果是l配置,按规则配置即可。DruidStatViewServlet:
import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* druid数据源状态监控.
* Created by winner_0715 on 2017/4/19.
*/
@WebServlet(urlPatterns = "/druid/*",
initParams = {
// IP⽩名单 (没有配置或者为空,则允许所有访问)
@WebInitParam(name = "allow", value = "192.168.1.72,127.0.0.1"),
/
/ IP⿊名单 (存在共同时,deny优先于allow)
@WebInitParam(name = "deny", value = "192.168.1.73"),
// ⽤户名
@WebInitParam(name = "loginUsername", value = "admin"),
// 密码
@WebInitParam(name = "loginPassword", value = "123456"),
// 禁⽤HTML页⾯上的“Reset All”功能
@WebInitParam(name = "resetEnable", value = "false")
}
)
public class DruidStatViewServlet extends StatViewServlet {
private static final long serialVersionUID = 1L;
}
DruidStatFilter:
import com.alibaba.druid.support.http.WebStatFilter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
/**
* druid过滤器.
* Created by winner_0715 on 2017/4/19.
*/
@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
initParams = {
// 忽略资源
@WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")
}
)
public class DruidStatFilter extends WebStatFilter {
}
最后在启动类上加上注解:@ServletComponentScan使得spring能够扫描到我们⾃⼰编写的servlet和filter。
注意不要忘记在 SpringBootSampleApplication.java 上添加 @ServletComponentScan 注解,不然就是404了。
(4)配置监控系统⽅式⼆:
以上配置的监控⽅式是使⽤了原⽣的servlet,filter⽅式,[email protected]�⾏处理的,你会发现我们的servlet,filter根本没有任何的编码。
在这⾥我们将使⽤另外⼀种⽅式进⾏处理:使⽤代码注册Servlet:
DruidConfiguration :
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import t.annotation.Bean;
import t.annotation.Configuration;
/**
* 这样的⽅式不需要添加注解:@ServletComponentScan
* Created by winner_0715 on 2017/4/19.
*/
@Configuration
public class DruidConfiguration {
/**
* 注册⼀个StatViewServlet
*
* @return
*/
@Bean
public ServletRegistrationBean DruidStatViewServle2() {
/
/org.t.embedded.ServletRegistrationBean提供类的进⾏注册.
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/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(new WebStatFilter());
//添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*");
//添加不需要忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");
return filterRegistrationBean;
}
}
以上所述是⼩编给⼤家介绍的Spring Boot使⽤Druid和监控配置⽅法,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。