Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringBoot Learning (5)-- springboot Rapid Integration of Druid

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Introduction to Druid connection Pool

The druid connection pool opened by Alibaba is the most outstanding database connection pool with comprehensive strength at present, and it also provides the monitoring log function to analyze the implementation of SQL.

Introduction of druid connection pool

Add to pom.xml

Com.alibaba druid 1.1.21

Add to application.properties

# druidspring.datasource.type=com.alibaba.druid.pool.DruidDataSource# connection pool initialization size, minimum, maximum spring.datasource.initialSize=10spring.datasource.minIdle=10spring.datasource.maxActive=30# connection wait timeout spring.datasource.maxWait=60000# how long to detect the idle connection that needs to be closed spring.datasource.timeBetweenEvictionRunsMillis=60000# the minimum survival time of a connection in the pool spring.datasource.minEvictableIdleTimeMillis=300000# check SQL,Oracle configuration spring.datasource.validationQuery=SELECT 1 FROM DUAL, if not equipped with the validationQuery item Then the following three configurations do not use spring.datasource.validationQuery=SELECT 'x'spring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=false# to open PSCache, and specify the size of the PSCache on each connection. Configure the filters that is intercepted by monitoring statistics. After removing the monitoring interface, sql cannot count, and wall is used for firewall spring.datasource.filters=stat,wall,log4j# to turn on the mergeSql function through the connectProperties attribute. Slow SQL records the monitoring data spring.datasource.useGlobalDataSourceStat=true of spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# merging multiple DruidDataSource

Note: mysql,mybatis has been configured before, so there is no repetition here

Code actual combat

Added a DruidFilter.java to configure built-in monitoring

DruidFilter.java

Package com.example.config;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 org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration @ Configurationpublic class DruidFilter {@ Bean public ServletRegistrationBean druidStatView () {/ / specify the path to the built-in monitoring page ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean (new StatViewServlet (), "/ druid/*"); / / IP whitelist: servletRegistrationBean.addInitParameter ("allow", "127.0.0.1"); / / IP blacklist servletRegistrationBean.addInitParameter ("deny", "192.168.1.73") / / log in to view the account password of the information. / / servletRegistrationBean.addInitParameter ("loginUsername", "admin"); / / servletRegistrationBean.addInitParameter ("loginPassword", "123456"); / / whether the data can be reset. ServletRegistrationBean.addInitParameter ("resetEnable", "true"); return servletRegistrationBean;} @ Bean public FilterRegistrationBean druidWebStatFilter () {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean (new WebStatFilter ()); / / add filter rules. FilterRegistrationBean.addUrlPatterns ("/ *"); / / add formatting information that does not need to be ignored. FilterRegistrationBean.addInitParameter ("exclusions", "* .js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean;}

Note: I have already configured spring security before. If you configure the account password of druid as in other tutorials, you will not be able to jump to the built-in monitoring page after entering the account password of druid. I know there are many ways to avoid it, but that requires additional code to determine bypass. Since spring security already has account permissions, why add a separate set of account permissions to druid? If the / druid/* path is configured in spring security, it is better to share the existing account permissions. So I don't have a separate configuration.

And this is only a common configuration, if you need to know more about the detailed configuration.

Built-in monitoring page configuration details, Web associated configuration details

The effect picture is as follows

Well, it also comes with its own advertisement for Aliyun, which is really Ali's open source product.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report