springboot实现动态分库-逻辑schema分库

1 篇文章 0 订阅
1 篇文章 0 订阅

前段时间公司要开发一套企业级的Saas系统,为了方便部署和迁移因此每个企业的数据库是独立的,数据库之间没有关联。库以及表动态创建的,只有用户注册后才会动态创建。

该功能实现起来比较简单,因为数据库是独立的,因此不需要考虑事务问题、跨库查询等分库分表的常见问题。但是因为因为云端部署多个数据库成本很高并且会造资源浪费,因此采用的是逻辑分库的方式及创建多个schema的方式进行分库。
在这里插入图片描述

我们目前使用的开发架构是springboot+mybatis,但是mybatis无法满足动态schema的需求,因此改用Springboot+SpringJDBC的方式。

主要的依赖如下:

    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>3.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

JdbcTemplate本身并不支持动态设置schema的功能,因此需要重新改造JdbcTemplate使其支持逻辑schema的动态设置。
在这里只展示部分代码,如果想查看全部代码可以下载查看。https://download.csdn.net/download/u013623958/11225293。

@Override
@Nullable
public <T> T execute(StatementCallback<T> action, String schema) throws DataAccessException {
    Assert.notNull(action, "Callback object must not be null");

    Connection con = DataSourceUtils.getConnection(obtainDataSource());
    try {
        con.setCatalog(schema);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    try {
        con.setCatalog(schema);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    Statement stmt = null;
    try {
        stmt = con.createStatement();
        applyStatementSettings(stmt);
        T result = action.doInStatement(stmt);
        handleWarnings(stmt);
        return result;
    }
    catch (SQLException ex) {
        // Release Connection early, to avoid potential connection pool deadlock
        // in the case when the exception translator hasn't been initialized yet.
        String sql = getSql(action);
        JdbcUtils.closeStatement(stmt);
        stmt = null;
        DataSourceUtils.releaseConnection(con, getDataSource());
        con = null;
        throw translateException("StatementCallback", sql, ex);
    }
    finally {
        JdbcUtils.closeStatement(stmt);
        DataSourceUtils.releaseConnection(con, getDataSource());
    }
}

接下来就可以根据自身业务动态设置schema了

List<Map<String,Object>>  list1= db1JdbcTemplate.queryForList("select * from user","demo_ds_0");
List<Map<String,Object>>  list2= db1JdbcTemplate.queryForList("select * from db_info","demo_ds_1");
其中demo_ds_0、demo_ds_0是逻辑分库。

在这里插入图片描述

通过Springboot将MyJdbcTemplate注入。

@Bean(name = "db1JdbcTemplate")
public MyJdbcTemplate Db1JdbcTemplate(@Qualifier("db1DataSource")DataSource dataSource) {
    return new MyJdbcTemplate(dataSource);
}

@Primary
@Bean(name = "db1DataSourceProperties")
@Qualifier("db1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSourceProperties db1DataSourceProperties() {
    return new DataSourceProperties();
}

@Primary
@Bean(name = "db1DataSource")
@Qualifier("db1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource db1DataSource() {

    return db1DataSourceProperties().initializeDataSourceBuilder().build();
}

同时SpringJDBC支持多数据源,这样一来业务基本满足。

https://download.csdn.net/download/u013623958/11225293。

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值