springboot

springboot动态数据源怎么配置

小亿
103
2023-11-22 19:06:28
栏目: 编程语言

在Spring Boot中配置动态数据源需要以下步骤:

  1. 在pom.xml文件中添加以下依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>2.9.0</version>
</dependency>
  1. 创建DataSourceConfig类,用于配置数据源:
@Configuration
public class DataSourceConfig {
    
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }
    
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    public DataSource slaveDataSource() {
        return DataSourceBuilder.create().build();
    }
    
    @Bean
    @Primary
    public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("master", masterDataSource);
        targetDataSources.put("slave", slaveDataSource);
        
        DynamicDataSource dataSource = new DynamicDataSource();
        dataSource.setTargetDataSources(targetDataSources);
        dataSource.setDefaultTargetDataSource(masterDataSource);
        
        return dataSource;
    }
}
  1. 在application.properties文件中配置数据源的相关属性:
# 主数据源配置
spring.datasource.master.url = jdbc:mysql://localhost:3306/master_database
spring.datasource.master.username = root
spring.datasource.master.password = root

# 从数据源配置
spring.datasource.slave.url = jdbc:mysql://localhost:3306/slave_database
spring.datasource.slave.username = root
spring.datasource.slave.password = root
  1. 创建DynamicDataSource类,用于动态切换数据源:
public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContextHolder.getDataSource();
    }

}
  1. 创建DataSourceContextHolder类,用于保存当前线程使用的数据源:
public class DataSourceContextHolder {

    private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();

    public static void setDataSource(String dataSource) {
        CONTEXT_HOLDER.set(dataSource);
    }

    public static String getDataSource() {
        return CONTEXT_HOLDER.get();
    }

    public static void clearDataSource() {
        CONTEXT_HOLDER.remove();
    }

}
  1. 创建AopDataSourceAspect类,用于切换数据源:
@Aspect
@Component
public class AopDataSourceAspect {

    @Before("@annotation(com.example.dynamicdatasource.annotation.DataSource)")
    public void switchDataSource(JoinPoint point) {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        DataSource dataSource = method.getAnnotation(DataSource.class);
        if (dataSource == null) {
            DataSourceContextHolder.setDataSource("master");
        } else {
            DataSourceContextHolder.setDataSource(dataSource.value());
        }
    }

    @After("@annotation(com.example.dynamicdatasource.annotation.DataSource)")
    public void restoreDataSource(JoinPoint point) {
        DataSourceContextHolder.clearDataSource();
    }

}
  1. 在需要切换数据源的方法上添加@DataSource注解,如:
@DataSource("slave")
public List<User> getUsers() {
    // 查询从数据源
}

通过以上配置,就可以在Spring Boot中实现动态数据源的切换。

0
看了该问题的人还看了