In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to introduce druid data sources in springboot, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The first step, the introduction of dependency mysql mysql-connector-java runtime com.alibaba druid 1.1.5, the second step, Configuration application.properties File # data Source configuration spring.datasource.url=jdbc:mysql://192.168.0.131:3306/hongone?useAffectedRows=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver#druid connection Pool configuration Information for spring.datasource.type=com.alibaba.druid.pool.DruidDataSource# connection Pool # initialization size Minimum and maximum spring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20# configuration get the time for connection waiting for timeout spring.datasource.maxWait=30000# configuration how often to detect the 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 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=false# open PSCache And specify the size of the PSCache on each connection spring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20# configure the filters intercepted by monitoring statistics. After removal, the monitoring interface sql cannot count. 'wall' is used for firewall spring.datasource.filters=stat,wall# to turn on the mergeSql function through the connectProperties attribute. Slow SQL recording spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=2000
Note: the configuration here is customized according to the needs of the project, such as timeout connection time, slow sql query time, etc.
Third, the data source configuration file package com.hone.system.utils.druid;import com.alibaba.druid.pool.DruidDataSource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import javax.sql.DataSource;import java.sql.SQLException;@Configurationpublic class DruidDBConfig {private Logger logger = LoggerFactory.getLogger (DruidDBConfig.class) @ Value ("${spring.datasource.url}") private String dbUrl; @ Value ("${spring.datasource.username}") private String username; @ Value ("${spring.datasource.password}") private String password; @ Value ("${spring.datasource.driver-class-name}") private String driverClassName; @ Value ("${spring.datasource.initialSize}") private int initialSize @ Value ("${spring.datasource.minIdle}") private int minIdle; @ Value ("${spring.datasource.maxActive}") private int maxActive; @ Value ("${spring.datasource.maxWait}") private int maxWait; @ Value ("${spring.datasource.timeBetweenEvictionRunsMillis}") private int timeBetweenEvictionRunsMillis; @ Value ("${spring.datasource.minEvictableIdleTimeMillis}") private int minEvictableIdleTimeMillis; @ Value ("${spring.datasource.validationQuery}") private String validationQuery @ Value ("${spring.datasource.testWhileIdle}") private boolean testWhileIdle; @ Value ("${spring.datasource.testOnBorrow}") private boolean testOnBorrow; @ Value ("${spring.datasource.testOnReturn}") private boolean testOnReturn; @ Value ("${spring.datasource.poolPreparedStatements}") private boolean poolPreparedStatements; @ Value ("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}") private int maxPoolPreparedStatementPerConnectionSize; @ Value ("${spring.datasource.filters}") private String filters @ Value ("{spring.datasource.connectionProperties}") private String connectionProperties; / * * @ Bean declares that the DataSource object is managed by the Spring container; * @ Primary means that the DataSource defined here will overwrite DataSource from other sources. * StatFilter, which is used to count monitoring information. The alias of StatFilter is stat. * Statistics SQL information and merge statistics. MergeStat is the abbreviation of MergeStatFilter. * turn on the mergeSql function through the DataSource property or * connectProperties property * the standard StatFilter property slowSqlMillis is used to configure SQL slowness * * @ return * / @ Bean @ Primary public DataSource dataSource () {DruidDataSource datasource = new DruidDataSource (); datasource.setUrl (dbUrl); datasource.setDriverClassName (driverClassName); datasource.setUsername (username) Datasource.setPassword (password); / / configuration datasource.setInitialSize (initialSize); datasource.setMinIdle (minIdle); datasource.setMaxActive (maxActive); datasource.setMaxWait (maxWait); datasource.setTimeBetweenEvictionRunsMillis (timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis (minEvictableIdleTimeMillis); datasource.setValidationQuery (validationQuery); datasource.setTestWhileIdle (testWhileIdle); datasource.setTestOnBorrow (testOnBorrow); datasource.setTestOnReturn (testOnReturn) Datasource.setPoolPreparedStatements (poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize (maxPoolPreparedStatementPerConnectionSize); try {/ * set StatFilter for statistical monitoring information. * the alias of StatFilter is stat * * / datasource.setFilters (filters);} catch (SQLException e) {logger.error ("druid configuration initialization filter: {0}", e);} datasource.setConnectionProperties (connectionProperties); return datasource;}} step 4, configure the monitoring display page Servletimport com.alibaba.druid.support.http.StatViewServlet by annotating Import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet;/** * StatViewServlet is used to display the statistics of Druid. * the html page that provides the display of monitoring information * the JSON API that provides monitoring information * * the home page of the built-in monitoring page is / druid/index.html * * loginUsername, loginPassword login user name and password * / / * @ Webservlet * there are two attributes that can be used to indicate the access path of Servlet, namely value and urlPatterns. Both value and urlPatterns are arrays. * means that we can map a Servlet to multiple access paths, but value and urlPatterns cannot be used at the same time. * * / @ WebServlet (urlPatterns = {"/ druid/*"}, initParams = {@ WebInitParam (name = "loginUsername", value = "admin"), @ WebInitParam (name = "loginPassword", value = "admin"), @ WebInitParam (name = "resetEnable") Value = "false")}) public class DruidStatViewServlet extends StatViewServlet {} step 5, configure the collection Filterimport com.alibaba.druid.support.http.WebStatFilter through annotations Import javax.servlet.annotation.WebFilter;import javax.servlet.annotation.WebInitParam;/** * WebStatFilter is used to collect data of web-jdbc associated monitoring. * attribute filterName declares the name of the filter. Optional * attribute urlPatterns specifies the URL mode to be filtered, or it can be declared using attribute value. (specify that the URL mode to be filtered is a required attribute) * / @ WebFilter (urlPatterns = "/ *", initParams = {@ WebInitParam (name = "exclusions", value = "* .js,*.gif,*.jpg,*.png,*.css,*.ico") / druid/* ")}) step 6 of public class DruidStatFilter extends WebStatFilter {}, start class modification @ SpringBootApplication@MapperScan (" com.hone.dao ") / / enable mapper scan @ ServletComponentScan / / enable servlet scan public class HongoneApplication {.}
Finally, start the project for testing and type http://localhost:8080/druid/index.html in the browser
(note: if your project has an access prefix, ask the access address for the http://localhost:8080/ access prefix / druid/index.html)
The above is how to introduce druid data sources into springboot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.