自学内容网 自学内容网

Spring Boot 动态多数据源配置

1.实现AbstractRoutingDataSource,维护动态数据源集合

public class DynamicDataSource extends AbstractRoutingDataSource {

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

}

spring:
  datasource:
    ds1:
      jdbcUrl: jdbc:mysql://192.168.31.102:3306/ds1
      username: root
      password: root
    ds2:
      jdbcUrl: jdbc:mysql://192.168.31.102:3306/ds2
      username: root
      password: root
logging:
  level:
    root: debug

2.设置datasource,并初始默认。

@Configuration
public class DataSourceConfig {
    //定义数据源1
    @Bean(name = "ds1")
    @ConfigurationProperties(prefix = "spring.datasource.ds1")
    public DataSource ds1() {
        return DataSourceBuilder.create().build();
    }
    //定义数据源2
    @Bean(name = "ds2")
    @ConfigurationProperties(prefix = "spring.datasource.ds2")
    public DataSource ds2() {
        return DataSourceBuilder.create().build();
    }
    //定义动态数据源
    @Bean(name = "dataSource")
    public DataSource dynamicDataSource(@Qualifier("ds1") DataSource ds1,
                                         @Qualifier("ds2") DataSource ds2) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("ds1", ds1);
        targetDataSources.put("ds2", ds2);
        //实例化DynamicDataSource,设置数据源Map
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(targetDataSources);
        //设置默认数据源,未匹配上则使用默认数据源
        dynamicDataSource.setDefaultTargetDataSource(ds1);
        return dynamicDataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource ds){
        return new JdbcTemplate(ds);
    }
}

3.封装ThreadLocal线程,存储datasource

public class DataSourceContextHolder {

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

    public static void setDataSource(String dataSourceName) {
        contextHolder.set(dataSourceName);
    }

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

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

}

4.使用案列

@Service
public class UserSerivce {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void insertDs1(User user){
        DataSourceContextHolder.setDataSource("ds1");
        String sql = "INSERT INTO user(username, password, nickname) VALUES (?, ?, ?)";
        jdbcTemplate.update(sql,  user.getUsername(), user.getPassword(), user.getNickname());
    }

    public void insertDs2(User user){
        DataSourceContextHolder.setDataSource("ds2");
        String sql = "INSERT INTO user(username, password, nickname) VALUES (?, ?, ?)";
        jdbcTemplate.update(sql,  user.getUsername(), user.getPassword(), user.getNickname());
    }
}


原文地址:https://blog.csdn.net/qq_34249468/article/details/140485113

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!