druid springboot
Posted mr_raptor
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了druid springboot相关的知识,希望对你有一定的参考价值。
一、加入druid依赖
[html] view plain copy- <!--druid -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.27</version>
- </dependency>
- # 初始化大小,最小,最大
- 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
- package com.chhliu.springboot.jpa;
- 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
- package com.chhliu.springboot.jpa;
- 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="127.0.0.1"),// 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;
- package com.chhliu.springboot.jpa;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.web.servlet.ServletComponentScan;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
- @SpringBootApplication
- @EnableEurekaClient
- @ServletComponentScan // 注意要加上@ServletComponentScan注解,否则Servlet无法生效
- public class SpringbootJpaDruidApplication
- public static void main(String[] args)
- SpringApplication.run(SpringbootJpaDruidApplication.class, args);
1、在浏览器中输入http://localhost:7602/druid/login.html登陆
2、登陆进去后,监控页面如下:
经过测试发现,无论我们怎么操作,SQL监控一直都显示不出任何的内容,当前使用的spring boot版本为1.4.x版本
七、不使用spring boot自动配置功能,手动初始化DataSource
[java] view plain copy- package com.chhliu.springboot.jpa;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- SpringBoot使用Druid数据库加密链接完整方案