springBoot多数据源
时间: 2023-08-29 17:12:42 浏览: 144
在Spring Boot中,实现多数据源有多种方式,以下是其中两种:
1. 使用Spring Boot提供的AbstractRoutingDataSource
在Spring Boot中,可以通过继承AbstractRoutingDataSource类来实现多数据源。该类是Spring提供的一个抽象类,实现了DataSource接口,可以根据不同的数据源进行切换。具体实现步骤如下:
(1)定义不同的数据源配置类,每个数据源配置类都需要继承org.springframework.boot.autoconfigure.jdbc.DataSourceProperties,例如:
```java
@Configuration
@ConfigurationProperties(prefix = "spring.datasource.primary")
public class PrimaryDataSourceProperties extends DataSourceProperties {
}
```
(2)定义数据源配置类的DataSource Bean,例如:
```java
@Configuration
public class PrimaryDataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
```
(3)定义AbstractRoutingDataSource Bean,该Bean会根据线程上下文中的数据源key切换不同的数据源,例如:
```java
@Configuration
public class DynamicDataSourceConfig {
@Bean
@Primary
public DataSource dataSource(PrimaryDataSourceProperties primaryDataSourceProperties,
SecondaryDataSourceProperties secondaryDataSourceProperties) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put("primary", primaryDataSourceProperties.initializeDataSourceBuilder().build());
targetDataSources.put("secondary", secondaryDataSourceProperties.initializeDataSourceBuilder().build());
AbstractRoutingDataSource dataSource = new AbstractRoutingDataSource() {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSourceKey();
}
};
dataSource.setTargetDataSources(targetDataSources);
dataSource.setDefaultTargetDataSource(targetDataSources.get("primary"));
return dataSource;
}
}
```
(4)定义DataSourceContextHolder,用于在不同的线程中设置数据源key,例如:
```java
public class DataSourceContextHolder {
private static final ThreadLocal<String> dataSourceKey = new ThreadLocal<>();
public static String getDataSourceKey() {
return dataSourceKey.get();
}
public static void setDataSourceKey(String key) {
dataSourceKey.set(key);
}
public static void clearDataSourceKey() {
dataSourceKey.remove();
}
}
```
(5)定义切面,用于在每次调用DAO方法之前设置数据源key,例如:
```java
@Aspect
@Component
public class DataSourceAspect {
@Pointcut("execution(* com.example.demo.dao..*.*(..))")
public void dataSourceAspect() {
}
@Before("dataSourceAspect()")
public void setDataSource(JoinPoint joinPoint) {
if (joinPoint.getSignature().getDeclaringTypeName().contains("Primary")) {
DataSourceContextHolder.setDataSourceKey("primary");
} else {
DataSourceContextHolder.setDataSourceKey("secondary");
}
}
}
```
2. 使用Spring Boot提供的@Qualifier注解
在Spring Boot中,可以使用@Qualifier注解来指定不同的数据源。具体实现步骤如下:
(1)定义不同的DataSource Bean,例如:
```java
@Configuration
public class PrimaryDataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.primary")
@Primary
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
@Configuration
public class SecondaryDataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
```
(2)在DAO层中使用@Qualifier注解来指定使用哪个数据源,例如:
```java
@Repository
public class UserDaoImpl implements UserDao {
private final JdbcTemplate jdbcTemplate;
@Autowired
public UserDaoImpl(@Qualifier("primaryDataSource") DataSource primaryDataSource) {
this.jdbcTemplate = new JdbcTemplate(primaryDataSource);
}
// ...
}
```
以上是两种实现多数据源的方式,具体选择哪种方式取决于具体的业务场景和需求。
阅读全文