這篇文章主要用實例解析SpringBoot如何配置Druid數(shù)據(jù)監(jiān)控,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
創(chuàng)新互聯(lián)公司2013年成立,先為商都等服務建站,商都等地企業(yè),進行企業(yè)商務咨詢服務。為商都企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。
druid,一個為監(jiān)控而生的數(shù)據(jù)庫連接池,提供可視化界面來查看sql執(zhí)行情況。
1、pom文件引入druid數(shù)據(jù)源
com.alibaba druid 1.1.8
2、新建一個DrruidConfig配置文件
package com.example.mybatisplus.config; 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; /** * druid數(shù)據(jù)池配置 */ @Configuration public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid(){ return new DruidDataSource(); } //配置Druid的監(jiān)控 //1、配置一個管理后臺的Servlet @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); MapinitParams = new HashMap<>(); //配置登錄的賬號密碼 initParams.put("loginUsername","admin"); initParams.put("loginPassword","123456"); // initParams.put("deny","localhost"); //拒絕誰登錄 //是否能夠重置數(shù)據(jù). // bean.addInitParameter("resetEnable","false"); bean.setInitParameters(initParams); return bean; } //2、配置一個web監(jiān)控的filter @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map initParams = new HashMap<>(); initParams.put("exclusions","*.html,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
3、yml文件配置
spring: datasource: schema: - classpath:schema.sql initialization-mode: ALWAYS username: root password: root url: jdbc:MySQL://localhost:3306/jdbc?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver # type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true # 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻 filters: stat,wall maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
看完上述內(nèi)容,是不是對用實例解析SpringBoot如何配置Druid數(shù)據(jù)監(jiān)控有進一步的了解,如果還想學習更多內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。