【Video & Communication Platform】
http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
https://gitee.com/happyangellxq520/spring-boot
à Spring Boot communication platform
http://412887952-qq-com.iteye.com/blog/2321532
Origin of need:
In my impression, Druid was integrated in spring boot in a very early blog. At that time, there were more things to configure. With the maturity and stability of the technology, there is now a simpler integration method. This blog is to tell you. How to integrate gracefully.
Outline of this section:
2. Configuration properties
3. How to configure multiple data sources
4. How to configure Filter
Next, look at the specific content:
1. How to use
1.1 Add druid-spring-boot-starter dependency to Spring Boot project
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.3</version> </dependency>
1.2 Add configuration
spring.datasource.url= spring.datasource.username= spring.datasource.password= # ...Other configuration (optional, not required, if the embedded database is used, the above three items can also be omitted)
2. Configuration properties
Druid Spring Boot Starter configuration properties are named exactly as Druid . You can configure Druid database connection pooling and monitoring through the Spring Boot configuration file , or use default values if not configured.
2.1 JDBC Configuration
spring.datasource.druid.url= # 或spring.datasource.url= spring.datasource.druid.username= # 或spring.datasource.username= spring.datasource.druid.password= # 或spring.datasource.password= spring.datasource.druid.driver-class-name= #或 spring.datasource.driver-class-name=
2.2 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 comma separated ....//more
2.3 Monitoring configuration
# WebStatFilter configuration, please refer to Druid Wiki, configuration_configure WebStatFilter spring.datasource.druid.web-stat-filter.enabled= #Whether to enable StatFilter default value true 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, please refer to Druid Wiki for instructions, configure _StatViewServlet configuration spring.datasource.druid.stat-view-servlet.enabled= #Whether to enable StatViewServlet default value true 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 for instructions, configure _Druid and Spring related monitoring configuration spring.datasource.druid.aop-patterns= # Spring monitoring AOP entry points, such as xyzservice.*, configure multiple comma-separated # If the class to be proxyed by spring.datasource.druid.aop-patterns does not define an interface, please set spring.aop.proxy-target-class=true
Druid Spring Boot Starter is not limited to providing support for the above configuration properties, and all configurable properties that provide setter methods in DruidDataSource will be supported. You can refer to the WIKI documentation or input prompts through the IDE to configure. You can choose .properties or .yml for the format of the configuration file. The effect is the same. It is recommended to use .yml when there are many configurations .
3. How to configure multiple data sources
3.1 Add configuration
spring.datasource.url= spring.datasource.username= spring.datasource.password= # Druid data source configuration, inherit spring.datasource.* configuration, overwrite the same ... spring.datasource.druid.initial-size=5 spring.datasource.druid.max-active=5 ... # Druid data source 1 configuration, inherit spring.datasource.druid.* configuration, overwrite the same ... spring.datasource.druid.one.max-active=10 spring.datasource.druid.one.max-wait=10000 ... # Druid data source 2 configuration, inherit spring.datasource.druid.* configuration, overwrite the same ... spring.datasource.druid.two.max-active=20 spring.datasource.druid.two.max-wait=20000 ...
3.2 Create a 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(); }
Fourth, how to configure Filter
You can enable the corresponding built-in Filters in the following ways , but these Filters are all default configurations. If the default configuration does not meet your needs, you can abandon this method and configure the Filter through the configuration file . The following is an example. spring.datasource.druid.filters=stat,wall,log4j ...
# 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
Currently, configuration support is provided for the following Filters , please refer to the documentation or configure according to the IDE prompt ( spring.datasource.druid.filter.*
).
- StatFilter
- WallFilter
- ConfigFilter
- EncodingConvertFilter
- Slf4jLogFilter
- Log4jFilter
- Log4j2Filter
- CommonsLogFilter
To make the custom Filter configuration take effect, you need to set the corresponding Filter to , Druid Spring Boot Starter will enable StatFilter by default , you can also set it to disable it. enabled
true
enabled
false
This article is reproduced: https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter/