当前位置:   article > 正文

SpringBoot 之 Spring Boot 集成 Druid_druid querytimeout默认值

druid querytimeout默认值

目录

 

一、Druid 的介绍

二、添加依赖

三、配置

四、添加 druid 的支持类

五、监控

1.配置 servlet

2.配置 filter

3.其他参数配置

4.测试:

5.sql 监控配置

6.spring 监控配置


一、Druid 的介绍

 
 
 
https://github.com/alibaba/druid/wiki/%E5%90%84%E7%A7%8D%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9
E%E6%8E%A5%E6%B1%A0%E5%AF%B9%E6%AF%94
 
中文文档: https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
 

二、添加依赖

 
          <dependency>
              <groupId>com.alibaba</groupId>
              <artifactId>druid</artifactId>
              <version>1.0.26</version>
          </dependency>
 

三、配置

 
spring.datasource.url=jdbc:mysql://localhost/spring_boot_demo?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
注意: 关于 spring.datasource.type 的说明
旧版本不支持这个属性,1.3.x 开始支持,但是 1.4.0 不支持,1.4.1 重新支持。
 
 

四、添加 druid 的支持类

  1. package com.roncoo.education.util.configuration;
  2. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  4. import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.boot.jdbc.DatabaseDriver;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import com.alibaba.druid.pool.DruidDataSource;
  10. @Configuration
  11. public class DruidConfiguration {
  12. @ConditionalOnClass(DruidDataSource.class)
  13. @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.alibaba.druid.pool.DruidDataSource", matchIfMissing = true)
  14. static class Druid extends DruidConfiguration {
  15. @Bean
  16. @ConfigurationProperties("spring.datasource.druid")
  17. public DruidDataSource dataSource(DataSourceProperties properties) {
  18. DruidDataSource druidDataSource = (DruidDataSource) properties.initializeDataSourceBuilder().type(DruidDataSource.class).build();
  19. DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(properties.determineUrl());
  20. String validationQuery = databaseDriver.getValidationQuery();
  21. if (validationQuery != null) {
  22. druidDataSource.setValidationQuery(validationQuery);
  23. }
  24. return druidDataSource;
  25. }
  26. }
  27. }

五、监控

1.配置 servlet

 
  1. package com.roncoo.education.util.servlet;
  2. import javax.servlet.annotation.WebInitParam;
  3. import javax.servlet.annotation.WebServlet;
  4. import com.alibaba.druid.support.http.StatViewServlet;
  5. /**
  6. *
  7. * @author
  8. * 访问http://localhost:8080/druid会有druid的管理工具
  9. */
  10. @WebServlet(urlPatterns = { "/druid/*" }, initParams = { @WebInitParam(name = "loginUsername", value = "roncoo"), @WebInitParam(name = "loginPassword", value = "roncoo") })
  11. public class DruidStatViewServlet extends StatViewServlet {
  12. private static final long serialVersionUID = 1L;
  13. }

2.配置 filter

 
  1. package com.roncoo.education.util.filter;
  2. import javax.servlet.annotation.WebFilter;
  3. import javax.servlet.annotation.WebInitParam;
  4. import com.alibaba.druid.support.http.WebStatFilter;
  5. /**
  6. * @author
  7. */
  8. @WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*", initParams = { @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*") })
  9. public class DruidWebStatFilter extends WebStatFilter {
  10. }

3.其他参数配置

  1. #初始化连接大小
  2. spring.datasource.druid.initial-size=8 #最小空闲连接数
  3. spring.datasource.druid.min-idle=5 #最大连接数
  4. spring.datasource.druid.max-active=10
  5. #查询超时时间
  6. spring.datasource.druid.query-timeout=6000
  7. #事务查询超时时间
  8. spring.datasource.druid.transaction-query-timeout=6000
  9. #关闭空闲连接超时时间
  10. spring.datasource.druid.remove-abandoned-timeout=1800

4.测试:

 
http://localhost:8080/druid/index.html
 

5.sql 监控配置

 
#filter 类名 :stat,config,encoding,logging
 
spring.datasource.druid.filters= stat
 
 

6.spring 监控配置

 
  1. package com.roncoo.education;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.web.servlet.ServletComponentScan;
  5. import org.springframework.context.annotation.ImportResource;
  6. @ServletComponentScan
  7. @SpringBootApplication
  8. @ImportResource(locations = { "classpath:druid-bean.xml" })
  9. public class SpringBootDemo291Application {
  10. public static void main(String[] args) {
  11. SpringApplication.run(SpringBootDemo291Application.class, args);
  12. }
  13. }

druid-bean.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop.xsd">
  9. <!-- 配置_Druid和Spring关联监控配置 -->
  10. <bean id="druid-stat-interceptor"
  11. class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean>
  12. <!-- 方法名正则匹配拦截配置 -->
  13. <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
  14. scope="prototype">
  15. <property name="patterns">
  16. <list>
  17. <value>com.roncoo.education.mapper.*</value>
  18. </list>
  19. </property>
  20. </bean>
  21. <aop:config proxy-target-class="true">
  22. <aop:advisor advice-ref="druid-stat-interceptor"
  23. pointcut-ref="druid-stat-pointcut" />
  24. </aop:config>
  25. </beans>

 

 
 
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号