mirror of
https://github.com/drtrang/druid-spring-boot.git
synced 2026-10-31 22:13:46 +00:00
1.1.2
This commit is contained in:
+1
-1
@@ -7,4 +7,4 @@ cache:
|
||||
directories:
|
||||
- $HOME/.m2/repository
|
||||
after_success:
|
||||
- cd druid-spring-boot-samples && mvn clean -Pcoveralls cobertura:cobertura coveralls:report
|
||||
- cd druid-spring-boot-example && mvn clean -Pcoveralls cobertura:cobertura coveralls:report
|
||||
@@ -14,16 +14,14 @@ Druid Spring Boot Starter 将帮助你在 Spring Boot 中使用 Druid。
|
||||
<dependency>
|
||||
<groupId>com.github.drtrang</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.1.2</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
## NEW !
|
||||
1. 新增全配置说明 [druid.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-samples/src/main/resources/druid.yml)
|
||||
1. 新增 `statement-sql-format-option` 的代码提示
|
||||
1. 改进多数据源的声明方式,由 Starter 自动发现配置并注册到 Spring Context,详情请查看 [Druid 多数据源支持.md](https://github.com/drtrang/druid-spring-boot/tree/master/docs/Druid%20%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E6%94%AF%E6%8C%81.md)。
|
||||
1. 新增全配置说明 [druid.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/druid.yml)
|
||||
2. 新增 `druid-spring-boot-actuator-starter`
|
||||
3. 新增 ConfigFilter 的自动配置,替换 Druid 默认的 `connectionProperties` 方式
|
||||
4. 完美支持多数据源 [ISSUE #2](https://github.com/drtrang/druid-spring-boot/issues/2)
|
||||
|
||||
|
||||
## 配置
|
||||
@@ -45,6 +43,10 @@ Druid Spring Boot Starter 会将以 `spring.datasource.druid` 为前缀的配置
|
||||
```yaml
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: org.h2.Driver
|
||||
url: jdbc:h2:file:./samples
|
||||
username: root
|
||||
password: 123456
|
||||
druid:
|
||||
initial-size: 1
|
||||
min-idle: 1
|
||||
@@ -91,13 +93,24 @@ spring:
|
||||
```
|
||||
|
||||
### 多数据源
|
||||
1.0.2 版本新增多数据源支持,使用方式请查看 [Druid 多数据源支持.md](https://github.com/drtrang/druid-spring-boot/tree/master/docs/Druid%20%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E6%94%AF%E6%8C%81.md)。
|
||||
1.1.2 版本改进了多数据源的声明方式,由 Starter 自动发现配置并注册到 Spring Context,详情请查看 [Druid 多数据源支持.md](https://github.com/drtrang/druid-spring-boot/tree/master/docs/Druid%20%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E6%94%AF%E6%8C%81.md)。
|
||||
|
||||
```yaml
|
||||
spring:
|
||||
datasource:
|
||||
druid:
|
||||
data-sources:
|
||||
first:
|
||||
url: jdbc:h2:file:./first
|
||||
second:
|
||||
url: jdbc:h2:file:./second
|
||||
```
|
||||
|
||||
### 配置示例
|
||||
[application.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-samples/src/main/resources/application.yml)
|
||||
[application.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/application.yml)
|
||||
|
||||
### 全配置说明
|
||||
[druid.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-samples/src/main/resources/druid.yml)
|
||||
[druid.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/druid.yml)
|
||||
|
||||
|
||||
## 自动提示
|
||||
@@ -114,7 +127,7 @@ Druid Spring Boot Starter 基于 `spring-boot-configuration-processor` 模块,
|
||||
|
||||
|
||||
## 演示
|
||||
[druid-spring-boot-samples](https://github.com/drtrang/druid-spring-boot/tree/master/druid-spring-boot-samples) 演示了 Druid Spring Boot Starter 的使用方式,可以作为参考。
|
||||
[druid-spring-boot-example](https://github.com/drtrang/druid-spring-boot/tree/master/druid-spring-boot-example) 演示了 Druid Spring Boot Starter 的使用方式,可以作为参考。
|
||||
|
||||
|
||||
## Change Log
|
||||
|
||||
+43
-37
@@ -1,20 +1,33 @@
|
||||
# Druid 多数据源支持
|
||||
|
||||
1.0.2 版本新增支持多数据源的 **DruidDataSource2**,开启该特性需要手动声明数据源,并指定注入的前缀 `spring.datasource.druid.${name}`,其中 `${name}` 可根据实际情况自定义。
|
||||
1.0.2 版本新增了支持多数据源的 **DruidDataSource2**,可以将 `spring.datasource` 和 `spring.datasource.druid` 开头的配置注入到数据源,解决各数据源之间的相同配置问题。
|
||||
1.1.2 版本改进了多数据源的注册方式,由 Starter 自动发现配置并注册到 Spring Context,用户只需在配置文件中声明即可。
|
||||
|
||||
|
||||
## 使用方式
|
||||
|
||||
1. 声明各个 DataSource,类型为 `com.github.trang.druid.autoconfigure.datasource.DruidDataSource2`。
|
||||
```java
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.datasource.druid.one")
|
||||
public DruidDataSource firstDataSource() {
|
||||
return new DruidDataSource2();
|
||||
}
|
||||
1. 编辑配置文件,Starter 会将 `spring.datasource.druid.data-sources` 开头的的属性注入到 Map<String, DruidDataSource2> 中,从而构建多数据源。
|
||||
```yaml
|
||||
spring:
|
||||
autoconfigure:
|
||||
exclude:
|
||||
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
|
||||
datasource:
|
||||
driver-class-name: org.h2.Driver
|
||||
username: root
|
||||
password: 123456
|
||||
druid:
|
||||
# 多数据源的标识,若该属性存在则为多数据源环境
|
||||
data-sources:
|
||||
## master 数据源的配置,以下为 master 数据源独有的配置
|
||||
master:
|
||||
url: jdbc:h2:file:./master
|
||||
## slave 数据源的配置,以下为 slave 数据源独有的配置
|
||||
slave:
|
||||
url: jdbc:h2:file:./slave
|
||||
```
|
||||
|
||||
2. 手动排除 `org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration`,以下方式任选其一:
|
||||
注: 排除 `org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration` 有两种方式,以下方式任选其一即可:
|
||||
1. 在 `@SpringBootApplication` 注解中排除
|
||||
```java
|
||||
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
|
||||
@@ -29,46 +42,39 @@
|
||||
|
||||
## 原理
|
||||
|
||||
DruidDataSourceSupport 类存在的目的是为了注入 `spring.datasource.druid` 的配置。
|
||||
基于 Spring4 的特性,DruidDataSource2 在继承 DruidDataSourceSupport 的同时,也会继承这些配置,由此解决多数据源场景下相同配置重复定义的问题。
|
||||
DruidDataSource2Support 类存在的目的是为了注入 `spring.datasource` 和 `spring.datasource.druid` 的配置。
|
||||
基于 Spring4 的特性,DruidDataSource2 在继承 DruidDataSource2Support 的同时,也会继承这些配置,由此解决多数据源场景下相同配置重复定义的问题。
|
||||
|
||||
也就是说,以下两种方式是等价的:
|
||||
```yaml
|
||||
spring:
|
||||
datasource:
|
||||
druid:
|
||||
min-evictable-idle-time-millis: 1800000
|
||||
max-evictable-idle-time-millis: 25200000
|
||||
one:
|
||||
name: one
|
||||
two:
|
||||
name: two
|
||||
---
|
||||
spring:
|
||||
datasource:
|
||||
druid:
|
||||
one:
|
||||
name: one
|
||||
min-evictable-idle-time-millis: 1800000
|
||||
max-evictable-idle-time-millis: 25200000
|
||||
two:
|
||||
name: two
|
||||
min-evictable-idle-time-millis: 1800000
|
||||
max-evictable-idle-time-millis: 25200000
|
||||
driver-class-name: org.h2.Driver
|
||||
username: root
|
||||
password: 123456
|
||||
data-sources:
|
||||
master:
|
||||
url: jdbc:h2:file:./master
|
||||
slave:
|
||||
url: jdbc:h2:file:./slave
|
||||
```
|
||||
|
||||
若子数据源有相同的配置时,则会覆盖掉父数据源的值:
|
||||
```yaml
|
||||
spring:
|
||||
datasource:
|
||||
druid:
|
||||
max-active: 20
|
||||
one:
|
||||
name: one
|
||||
max-active: 50
|
||||
two:
|
||||
name: two
|
||||
master:
|
||||
driver-class-name: org.h2.Driver
|
||||
username: root
|
||||
password: 123456
|
||||
url: jdbc:h2:file:./master
|
||||
slave:
|
||||
driver-class-name: org.h2.Driver
|
||||
username: root
|
||||
password: 123456
|
||||
url: jdbc:h2:file:./slave
|
||||
```
|
||||
|
||||
## 演示
|
||||
将 [druid-spring-boot-samples](https://github.com/drtrang/druid-spring-boot/tree/master/druid-spring-boot-samples) 中 [application.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-samples/src/main/resources/application.yml) 的 `spring.profiles.active` 配置改为 `dynamic` 即可。
|
||||
将 [druid-spring-boot-example](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/application.yml) 的 `spring.profiles.active` 配置改为 `dynamic` 即可。
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.drtrang</groupId>
|
||||
<artifactId>druid-spring-boot-actuator</artifactId>
|
||||
<version>1.1.2-SNAPSHOT</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>druid-spring-boot-actuator-autoconfigure</artifactId>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.drtrang</groupId>
|
||||
<artifactId>druid-spring-boot-actuator</artifactId>
|
||||
<version>1.1.2-SNAPSHOT</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>druid-spring-boot-actuator-starter</artifactId>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.drtrang</groupId>
|
||||
<artifactId>druid-spring-boot-parent</artifactId>
|
||||
<version>1.1.2-SNAPSHOT</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>druid-spring-boot-actuator</artifactId>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Druid Spring Boot Samples
|
||||
# Druid Spring Boot Example
|
||||
|
||||
Druid Spring Boot Samples 项目演示了 Druid Spring Boot Starter 的使用方式。将项目导入 IDE 后,直接运行 `com.github.trang.druid.DruidSpringBootStarterApplication#main()` 即可,无需依赖其它环境。
|
||||
Druid Spring Boot Example 项目演示了 Druid Spring Boot Starter 的使用方式。将项目导入 IDE 后,直接运行 `com.github.trang.druid.DruidSpringBootStarterApplication#main()` 即可,无需依赖其它环境。
|
||||
|
||||
项目默认使用单数据源,将 [application.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-samples/src/main/resources/application.yml) 的 `spring.profiles.active` 配置改为 `dynamic` 即可切换为多数据源演示。
|
||||
项目默认使用单数据源,将 [application.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/application.yml) 的 `spring.profiles.active` 配置改为 `dynamic` 即可切换为多数据源演示。
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.drtrang</groupId>
|
||||
<artifactId>druid-spring-boot-example</artifactId>
|
||||
<version>1.1.2-SNAPSHOT</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>druid-spring-boot-mybatis-example</artifactId>
|
||||
|
||||
+7
-7
@@ -154,10 +154,10 @@ spring:
|
||||
druid:
|
||||
### 子数据源也可以自定义配置,与 `spring.datasource` 相同的属性会覆盖 `spring.datasource` 的配置
|
||||
### 不同的属性只会注入到当前数据源,不会对其它数据源造成影响
|
||||
### 最终该数据源的配置为 `spring.datasource` + `spring.datasource.druid`
|
||||
order-master:
|
||||
name: orderMaster
|
||||
### 会覆盖 `spring.datasource.url`,但是因为二者的值相同,所以可以不写
|
||||
url: jdbc:h2:file:./samples
|
||||
order-slave:
|
||||
name: orderSlave
|
||||
### 最终该数据源的配置为 `spring.datasource.*` + `spring.datasource.druid.*` + `spring.datasource.druid.${name}.*`
|
||||
data-sources:
|
||||
order-master:
|
||||
### 会覆盖 `spring.datasource.url`
|
||||
url: jdbc:h2:file:./order_master
|
||||
order-slave:
|
||||
url: jdbc:h2:file:./order_slave
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.drtrang</groupId>
|
||||
<artifactId>druid-spring-boot-parent</artifactId>
|
||||
<version>1.1.2-SNAPSHOT</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>druid-spring-boot-example</artifactId>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.drtrang</groupId>
|
||||
<artifactId>druid-spring-boot</artifactId>
|
||||
<version>1.1.2-SNAPSHOT</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>druid-spring-boot-autoconfigure</artifactId>
|
||||
|
||||
+6
-2
@@ -154,8 +154,12 @@ public class DruidAutoConfiguration implements BeanFactoryAware, EnvironmentAwar
|
||||
.getBeanDefinition();
|
||||
// 注册 BeanDefinition
|
||||
beanFactory.registerBeanDefinition(dataSourceName, beanDefinition);
|
||||
// 注册 Alias
|
||||
beanFactory.registerAlias(dataSourceName, dataSourceName + "DataSource");
|
||||
String lowerCaseName = dataSourceName.toLowerCase();
|
||||
// 当 beanName 不是以 datasource 结尾时,增加别名
|
||||
if (!lowerCaseName.endsWith("datasource") && !lowerCaseName.endsWith("data-source")) {
|
||||
// 注册 Alias
|
||||
beanFactory.registerAlias(dataSourceName, dataSourceName + "DataSource");
|
||||
}
|
||||
// 将 'spring.datasource.druid.data-sources.$dataSourceName' 的配置绑定到 Bean
|
||||
DruidDataSource2 dataSource = beanFactory.getBean(dataSourceName, DruidDataSource2.class);
|
||||
PropertySourcesBinder propertySourcesBinder = new PropertySourcesBinder(environment);
|
||||
|
||||
+2
-3
@@ -1,6 +1,6 @@
|
||||
package com.github.trang.druid.autoconfigure.properties;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.github.trang.druid.autoconfigure.datasource.DruidDataSource2;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@@ -41,8 +41,7 @@ public class DruidDataSourceProperties {
|
||||
DruidWebStatProperties webStat = new DruidWebStatProperties();
|
||||
@NestedConfigurationProperty
|
||||
DruidAopStatProperties aopStat = new DruidAopStatProperties();
|
||||
@NestedConfigurationProperty
|
||||
Map<String, DruidDataSource> dataSources = new HashMap<>(16);
|
||||
Map<String, DruidDataSource2> dataSources = new HashMap<>(16);
|
||||
|
||||
@Getter@Setter
|
||||
public static class DruidConfigFilterProperties {
|
||||
|
||||
+11
-28
@@ -3,88 +3,71 @@
|
||||
{
|
||||
"name": "spring.datasource.druid.stat.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether or not druid stat-filter are enabled.",
|
||||
"description": "是否开启 StatFilter.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.wall.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether or not druid wall-filter are enabled.",
|
||||
"description": "是否开启 WallFilter.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.config.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether or not druid config-filter are enabled.",
|
||||
"description": "是否开启 ConfigFilter.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.slf4j.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether or not druid slf4j-filter are enabled.",
|
||||
"description": "是否开启 Slf4jLogFilter.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.log4j.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether or not druid log4j-filter are enabled.",
|
||||
"description": "是否开启 Log4jFilter.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.log4j2.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether or not druid log4j2-filter are enabled.",
|
||||
"description": "是否开启 Log4j2Filter.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.commons-log.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether or not druid commons-log-filter are enabled.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.aop-stat.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether or not druid aop-stat are enabled.",
|
||||
"sourceType": "com.github.trang.druid.autoconfigure.properties.DruidDataSourceProperties$DruidAopStatProperties",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.web-stat.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether or not druid web-stat are enabled.",
|
||||
"sourceType": "com.github.trang.druid.autoconfigure.properties.DruidDataSourceProperties$DruidWebStatProperties",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.stat-view-servlet.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether or not druid stat-view-servlet are enabled.",
|
||||
"sourceType": "com.github.trang.druid.autoconfigure.properties.DruidDataSourceProperties$DruidStatViewServletProperties",
|
||||
"description": "是否开启 CommonsLogFilter.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.slf4j.statement-sql-format-option.upp-case",
|
||||
"type": "java.lang.Boolean",
|
||||
"sourceType": "com.alibaba.druid.sql.SQLUtils$FormatOption",
|
||||
"description": "是否开启关键字大写.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.slf4j.statement-sql-format-option.pretty-format",
|
||||
"type": "java.lang.Boolean",
|
||||
"sourceType": "com.alibaba.druid.sql.SQLUtils$FormatOption",
|
||||
"description": "是否开启 SQL 格式化.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.slf4j.statement-sql-format-option.desensitize",
|
||||
"type": "java.lang.Boolean",
|
||||
"sourceType": "com.alibaba.druid.sql.SQLUtils$FormatOption",
|
||||
"description": "是否开启不敏感表名.",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"name": "spring.datasource.druid.slf4j.statement-sql-format-option.parameterized",
|
||||
"type": "java.lang.Boolean",
|
||||
"sourceType": "com.alibaba.druid.sql.SQLUtils$FormatOption",
|
||||
"description": "是否开启 SQL 参数化.",
|
||||
"defaultValue": false
|
||||
}
|
||||
],
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.drtrang</groupId>
|
||||
<artifactId>druid-spring-boot</artifactId>
|
||||
<version>1.1.2-SNAPSHOT</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.drtrang</groupId>
|
||||
<artifactId>druid-spring-boot-parent</artifactId>
|
||||
<version>1.1.2-SNAPSHOT</version>
|
||||
<version>1.1.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>druid-spring-boot</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user