In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how springboot configures DRUID data sources. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The details are as follows:
Druid is Ali's open source database connection pool.
The process of integrating druid data sources during development.
1. Modify pom.xml
Mysql mysql-connector-java com.alibaba druid 1.0.26
Add the above configuration.
two。 Edit configuration data source code
Package com.neo.conf;import javax.sql.DataSource;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 org.springframework.context.annotation.PropertySource;import com.alibaba.druid.pool.DruidDataSource;import com.alibaba.druid.support.http.StatViewServlet;import com.alibaba.druid.support.http.WebStatFilter @ Configuration@ConfigurationProperties (value = "classpath:druid.properties") public class DruidConfiguration {@ Bean (destroyMethod = "close", initMethod = "init") @ ConfigurationProperties (prefix = "spring.datasource") public DataSource druidDataSource () {DruidDataSource druidDataSource = new DruidDataSource (); return druidDataSource;} / * register a StatViewServlet * @ return * / @ Bean public ServletRegistrationBean druidStatViewServlet () {/ / org.springframework.boot.context.embedded.ServletRegistrationBean provides class registration. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean (new StatViewServlet (), "/ druid/*"); / / add initialization parameters: initParams / / whitelist: servletRegistrationBean.addInitParameter ("allow", "127.0.0.1192.168.31.77"); / / IP blacklist (if there is common, deny takes precedence over allow): if deny is met, prompt: Sorry, you are not permitted to view this page. ServletRegistrationBean.addInitParameter ("deny", "192.168.1.73"); / / login to view the account password for information. ServletRegistrationBean.addInitParameter ("loginUsername", "admin"); servletRegistrationBean.addInitParameter ("loginPassword", "123456"); / / whether the data can be reset. ServletRegistrationBean.addInitParameter ("resetEnable", "false"); return servletRegistrationBean;} / * register a: filterRegistrationBean * @ return * / @ Bean public FilterRegistrationBean druidStatFilter () {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;}
3. Write configuration file druid.properties
Below spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/mycine?useUnicode=true&characterEncoding=utf8&autoReconnect=truespring.datasource.username=rootspring.datasource.password=root#--# are the supplementary settings for connection pooling, which are applied to all the above data sources # initialization size, minimum Maximum spring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=50# configuration gets the time for connection waiting for timeout spring.datasource.maxWait=60000# configuration how often does it take to detect idle connections that need to be closed, in milliseconds spring.datasource.timeBetweenEvictionRunsMillis=60000# configure the minimum survival time of a connection in the pool, in milliseconds spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1spring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=false# opens PSCache And specify the size of the PSCache on each connection spring.datasource.poolPreparedStatements=false#spring.datasource.maxPoolPreparedStatementPerConnectionSize=20# configure the filters intercepted by monitoring statistics. After removal, the monitoring interface sql cannot count. 'wall' is used for firewalls # spring.datasource.filters=stat,wall,log4jspring.datasource.filters=stat# enables the mergeSql function through the connectProperties attribute. Slow SQL records the monitoring data of spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# merging multiple DruidDataSource # spring.datasource.useGlobalDataSourceStat=true
Thank you for reading! This is the end of the article on "how to configure DRUID data sources in springboot". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.