springboot druid 多数据源 示例
时间: 2023-05-23 10:02:44 浏览: 123
非常感谢您的提问。下面是 Spring Boot 和 Druid 多数据源的示例代码:
您需要添加以下 Maven 依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
然后,在 `application.yml` 中配置数据源:
```yaml
spring:
datasource:
# 主数据源
druid:
url: jdbc:mysql://localhost:3306/main?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 1 from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 第一个从数据源
druid1:
url: jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 1 from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 第二个从数据源
druid2:
url: jdbc:mysql://localhost:3306/other_db?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 1 from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
```
然后,在 Java 代码中配置数据源:
```java
@Configuration
public class DataSourceConfig {
@Bean
@Primary
@ConfigurationProperties("spring.datasource.druid")
public DataSource mainDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid1")
public DataSource dataSource1() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid2")
public DataSource dataSource2() {
return DruidDataSourceBuilder.create().build();
}
@Bean
public DataSource dynamicDataSource(@Qualifier("mainDataSource") DataSource mainDataSource,
@Qualifier("dataSource1") DataSource dataSource1,
@Qualifier("dataSource2") DataSource dataSource2) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MAIN, mainDataSource);
targetDataSources.put(DataSourceType.DATA_SOURCE_1, dataSource1);
targetDataSources.put(DataSourceType.DATA_SOURCE_2, dataSource2);
DynamicDataSource dynamicDataSource = new DynamicDataSource();
dynamicDataSource.setTargetDataSources(targetDataSources);
dynamicDataSource.setDefaultTargetDataSource(mainDataSource);
return dynamicDataSource;
}
}
```
其中,`DynamicDataSource` 是一个自定义的 `javax.sql.DataSource` 实现类,用于支持数据源的动态切换。最后,您就可以在代码中使用 `@Qualifier(DataSourceType.MAIN)`、`@Qualifier(DataSourceType.DATA_SOURCE_1)`、`@Qualifier(DataSourceType.DATA_SOURCE_2)` 等注解来区分使用不同的数据源了。
希望这个示例能够帮到您!
阅读全文