SpringBoot uses Druid connection pool

Druid Spring Boot Starter

Druid Spring Boot Starter is used to help you easily integrate Druid database connection pool and monitoring in Spring Boot project.

How to use

  1. Add druid-spring-boot-starter dependencies to Spring Book projects

    Maven

    <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>druid-spring-boot-starter</artifactId>
       <version>1.1.5</version>
    </dependency>

    Gradle

    compile 'com.alibaba:druid-spring-boot-starter:1.1.5'
    
  2. Add configuration
    xml
    spring.datasource.url=
    spring.datasource.username=
    spring.datasource.password=
    #... Other configurations (optional, not necessary, use embedded database, the above three can also be omitted from filling in)

Configuration properties

The name of the Druid Spring Boot Starter configuration property fully complies with Druid. You can configure the Druid database connection pool and monitoring through the Spring Boot configuration file, and use default values if not.

  • JDBC configuration
spring.datasource.druid.url= # Or spring.datasource.url= 
spring.datasource.druid.username= # Or spring.datasource.username=
spring.datasource.druid.password= # Or spring.datasource.password=
spring.datasource.druid.driver-class-name= #Or spring.datasource.driver-class-name=
  • Connection pool configuration
spring.datasource.druid.initial-size=
spring.datasource.druid.max-active=
spring.datasource.druid.min-idle=
spring.datasource.druid.max-wait=
spring.datasource.druid.pool-prepared-statements=
spring.datasource.druid.max-pool-prepared-statement-per-connection-size= 
spring.datasource.druid.max-open-prepared-statements= #Equivalent to the above
spring.datasource.druid.validation-query=
spring.datasource.druid.validation-query-timeout=
spring.datasource.druid.test-on-borrow=
spring.datasource.druid.test-on-return=
spring.datasource.druid.test-while-idle=
spring.datasource.druid.time-between-eviction-runs-millis=
spring.datasource.druid.min-evictable-idle-time-millis=
spring.datasource.druid.max-evictable-idle-time-millis=
spring.datasource.druid.filters= #Configure multiple English comma separators
....//more
  • Monitoring configuration
# WebStatFilter configuration, refer to Druid Wiki for instructions, configure _configure WebStatFilter
spring.datasource.druid.web-stat-filter.enabled= #Whether StatFilter default value true is enabled
spring.datasource.druid.web-stat-filter.url-pattern=
spring.datasource.druid.web-stat-filter.exclusions=
spring.datasource.druid.web-stat-filter.session-stat-enable=
spring.datasource.druid.web-stat-filter.session-stat-max-count=
spring.datasource.druid.web-stat-filter.principal-session-name=
spring.datasource.druid.web-stat-filter.principal-cookie-name=
spring.datasource.druid.web-stat-filter.profile-enable=

# StatViewServlet configuration, refer to Druid Wiki for instructions, configure the _StatViewServlet configuration
spring.datasource.druid.stat-view-servlet.enabled= #Whether the StatViewServlet default value true is enabled
spring.datasource.druid.stat-view-servlet.url-pattern=
spring.datasource.druid.stat-view-servlet.reset-enable=
spring.datasource.druid.stat-view-servlet.login-username=
spring.datasource.druid.stat-view-servlet.login-password=
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=

# Spring monitoring configuration, please refer to Druid Github Wiki, configuration _Druid and Spring associated monitoring configuration
spring.datasource.druid.aop-patterns= # Spring monitors AOP entry points, such as x.y.z.service. *, and configures multiple comma delimitations in English
# If the class that spring.datasource.druid.aop-patterns want to proxy does not define an interface, set spring.aop.proxy-target-class=true

Druid Spring Boot Starter is not limited to supporting the above configuration properties. DruidDataSource Configurable properties that provide setter methods inside will be supported. You can consult WIKI documents or configure them through IDE input prompts. Configuration file format you can choose. properties or. yml, the effect is the same, in the case of more configuration recommended to use. yml.

How to configure multiple data sources

  1. Add configuration
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=

# Druid Data Source Configuration, inherits spring.datasource. * Configuration, and overrides the same
...
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=5
...

# Druid Data Source 1 configuration, inherits spring.datasource.druid. * configuration, and overrides the same
...
spring.datasource.druid.one.max-active=10
spring.datasource.druid.one.max-wait=10000
...

# Druid Data Source 2 configuration, inherits spring.datasource.druid. * configuration, and overrides the same
...
spring.datasource.druid.two.max-active=20
spring.datasource.druid.two.max-wait=20000
...
  1. create data source
@Primary
@Bean
@ConfigurationProperties("spring.datasource.druid.one")
public DataSource dataSourceOne(){
    return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.two")
public DataSource dataSourceTwo(){
    return DruidDataSourceBuilder.create().build();
}

How to configure Filter

You can enable the corresponding built-in filters by spring.datasource.druid.filters=stat,wall,log4j... but these filters are all default configurations. If the default configuration does not meet your needs, you can abandon this method and configure Filter through configuration files. Here is an example.

# Configure StatFilter 
spring.datasource.druid.filter.stat.db-type=h2
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000

# Configure WallFilter 
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.db-type=h2
spring.datasource.druid.filter.wall.config.delete-allow=false
spring.datasource.druid.filter.wall.config.drop-table-allow=false

# Other Filter configurations are no longer demonstrated

Configuration support is currently provided for the following Filters, either in the documentation or in accordance with the IDE prompt (spring.datasource.druid.filter. *).
- StatFilter
- WallFilter
- ConfigFilter
- EncodingConvertFilter
- Slf4jLogFilter
- Log4jFilter
- Log4j2Filter
- CommonsLogFilter

To make the custom Filter configuration work, you need to set the enabled of the corresponding Filter to true. Druid Spring Boot Starter will enable StatFilter by default. You can also set its enabled to false to disable it.

IDE prompt support

Reference resources

Druid Spring Boot Starter

Keywords: Spring Druid Database Session

Added by wit77 on Sun, 19 May 2019 03:58:46 +0300