Druid is an implementation of a database connection pool on Alibaba's open source platform. It combines the advantages of DB pools such as C3P0, DBCP, PROXOOL, and also adds log monitoring, which can monitor the connections of DB pools and SQL execution well. It can be said to be a DB connection pool created for monitoring (it is said to be the best connection pool at present)
1. Dependence
For testing, use jdbcTemplate
<!-- jdbcTemplate --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- druid database connection pool--><dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.26</version></dependency><!-- mysql connector --><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope></dependency>
2. Druid configuration
druid.properties#Database settings spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/uu_core?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=truespring.datasource.username=rootspring.datasource.password=root#------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Initialize size, minimum, maximum spring.datasource.initialSize=5spring.datasource.minIdle=1spring.datasource.maxActive=50# Configure the time to get the connection waiting timeout spring.datasource.maxWait=60000# Configure how long it takes to perform a detection interval to detect idle connections that need to be closed, in milliseconds spring.datasource.timeBetweenEvictionRunsMillis=60000# Configure the minimum time to survive in the pool, in milliseconds spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falspring.datasource.testOnReturn=false# Open PSCache and specify the size of PSCache on each connection spring.datasource.poolPreparedStatements=false#spring.datasource.maxPoolPreparedStatementPerConnectionSize=20# Configure filters for monitoring statistics intercepted. After removing it, the monitoring interface SQL cannot be counted. 'wall' is used for firewall spring.datasource.filters=stat,wall,log4j# Open mergeSql function through the connectProperties property; slow SQL records spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# Merge monitoring data of multiple DruidDataSource#spring.datasource.useGlobalDataSourceStat=true
3. Instantiate Druid Datasource
package cn.aduu.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 org.springframework.context.annotation.PropertySource;import javax.sql.DataSource;/** * @author zh * @ClassName cn.aduu.config.DruidConfiguration * @Description */@Configuration@PropertySource(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 the class for registration. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //Add initialization parameters: initParams //Whitelist: servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //IP blacklist (when there is a commonality, deny takes precedence over allow) : If deny is satisfied, prompt: Sorry, you are not allowed to view this page. servletRegistrationBean.addInitParameter("deny","192.168.1.73"); //Login the account password for viewing information. servletRegistrationBean.addInitParameter("loginUsername","admin"); servletRegistrationBean.addInitParameter("loginPassword","123456"); //Is it possible to reset the data? servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; } /** * Register one: filterRegistrationBean * @return */ @Bean public FilterRegistrationBean druidStatFilter(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //Add filter rules. filterRegistrationBean.addUrlPatterns("/*"); //Add format information that does not need to be ignored. filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; }}4. Monitoring
Visit http://localhost:8080/druid and use the account password configured above.
V. Test
@RestControllerpublic class HelloController{ private static final Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired private JdbcTemplate jdbcTemplate; @RequestMapping("hello") public List<Map<String, Object>> hello() { List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT user,password FROM mysql.user", new Object[]{}); return list; }}Visit localhost:8080/hello
[ { "user": "root", "password": "*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B" }, { "user": "root", "password": "*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B" }] Summarize
The above is the SpringBoot data source introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!