网站建设方案设计书,网站建设用图,wordpress启动页,做动态二维码的网站SpringBoot使用druid 一、前言二、配置1、pom依赖2、配置文件yml3、配置类 一、前言
Java程序很大一部分要操作数据库#xff0c;为了提高性能操作数据库的时候#xff0c;又不得不使用数据库连接池。
Druid 是阿里巴巴开源平台上一个数据库连接池实现#xff0c;结合了 C… SpringBoot使用druid 一、前言二、配置1、pom依赖2、配置文件yml3、配置类 一、前言
Java程序很大一部分要操作数据库为了提高性能操作数据库的时候又不得不使用数据库连接池。
Druid 是阿里巴巴开源平台上一个数据库连接池实现结合了 C3P0、DBCP 等 DB 池的优点同时加入了日志监控。
Druid 可以很好的监控 DB 池连接和 SQL 的执行情况天生就是针对监控而生的 DB 连接池。
Druid已经在阿里巴巴部署了超过600个应用经过一年多生产环境大规模部署的严苛考验。
Spring Boot 2.0 以上默认使用 Hikari 数据源可以说 Hikari 与 Driud 都是当前 Java Web 上最优秀的数据源我们来重点介绍 Spring Boot 如何集成 Druid 数据源如何实现数据库监控。 二、配置
1、pom依赖 !--引入Druid数据源--dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.1.12/version/dependency!--日志依赖--dependencygroupIdlog4j/groupIdartifactIdlog4j/artifactIdversion1.2.17/version/dependency2、配置文件yml
#数据库配置
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/library?serverTimezoneGMT%2B8useUnicodetruecharacterEncodingutf8characterSetResultsutf8useSSLfalseallowMultiQueriestrueusername: rootpassword: 123456# 连接池类型druidtype: com.alibaba.druid.pool.DruidDataSource#Spring Boot 默认是不注入这些属性值的需要自己绑定#druid 数据源专有配置# 配置Druid的其他参数以下配置必须增加一个配置文件才能有效# 初始化大小最小最大initialSize: 5minIdle: 5maxActive: 20# 获取连接等待超时的时间maxWait: 60000# 配置间隔多久才进行一次检测检测需要关闭的空闲连接单位是毫秒timeBetweenEvictionRunsMillis: 60000# 配置一个连接在池中最小生存的时间单位是毫秒minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true# 配置监控统计拦截的filters去掉后监控界面sql无法统计wall用于防火墙filters: stat, wall# 打开PSCache并且指定每个连接上PSCache的大小maxPoolPreparedStatementPerConnectionSize: 20# 通过connectProperties属性来打开mergeSql功能慢SQL记录connectionProperties: druid.stat.mergeSqltrue;druid.stat.slowSqlMillis500# 合并多个DruidDataSource的监控数据useGlobalDataSourceStat: true
3、配置类
第一段 由于DruidDataSource需要使用上述的配置在添加到容器中就不能使用springboot自动生成这时需要我们自己添加 DruidDataSource 组件到容器中并绑定属性
第二段 Druid 数据源具有监控的功能并提供了一个 web 界面方便用户查看类似安装 路由器 时人家也提供了一个默认的 web 页面。
所以第一步需要设置 Druid 的后台管理页面比如 登录账号、密码 等配置后台管理
这里只是注册了一个servlet同时表明/druid/* 这个请求会走到这个servlet而druid内置了这个请求的接收同时需要给这个请求添加用户密码等参数
第三段 配置过滤请求需要统计哪些sql的信息
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;/*** 表示一个配置文件*/
Configuration
public class DruidConfig {/*** 加入到Spring容器中并扫描spring.datasource前缀的配置** return*/BeanConfigurationProperties(prefix spring.datasource)public DataSource druid() {return new DruidDataSource();}//配置 Druid 监控管理后台的Servlet//内置 Servlet 容器时没有web.xml文件所以使用 Spring Boot 的注册 Servlet 方式Beanpublic ServletRegistrationBean a() {ServletRegistrationBeanStatViewServlet bean new ServletRegistrationBean(new StatViewServlet(), /druid/*);MapString, String initParameters new HashMap();initParameters.put(loginUsername, admin);initParameters.put(loginPassword, admin);bean.setInitParameters(initParameters);return bean;}//配置 Druid 监控 之 web 监控的 filter//WebStatFilter用于配置Web和Druid数据源之间的管理关联监控统计Beanpublic FilterRegistrationBean webStatFilter() {FilterRegistrationBean bean new FilterRegistrationBean();bean.setFilter(new WebStatFilter());//exclusions设置哪些请求进行过滤排除掉从而不进行统计MapString, String initParams new HashMap();initParams.put(exclusions, *.js,*.css,/druid/*,/jdbc/*);bean.setInitParameters(initParams);///* 表示过滤所有请求bean.setUrlPatterns(Arrays.asList(/*));return bean;}
}配置完毕后我们可以选择访问 http://localhost:8080/druid/login.html