sharding分表应用笔记(一)——分表数据源配置

发布时间 2023-11-07 19:59:58作者: pjgyr

sharding分表应用笔记(一)——分表数据源配置

1 前言

应用背景:物理数据源只有一个;对于部分数据量大的表实行按月分表处理,其他的表仍然保持原先的模式不变。本篇记录sharding分表的逻辑数据源配置。

环境:spring

2 配置

2.1 相关依赖

<!-- without spring -->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
    <version>4.1.1</version>
</dependency>

<!-- for spring namespace -->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-namespace</artifactId>
    <version>4.1.1</version>
</dependency>

出于稳定性考虑,选用了4.x版本

因为项目使用spring,所以采用spring命名空间的配置方式。如果是spring-boot项目可以引入以下依赖,使用spring-boot配置文件的方式进行配置。其他方式的具体配置方法可参考官方文档。

<!-- for spring boot -->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>${sharding-sphere.version}</version>
</dependency>

2.2 命名空间配置

2.2.1 引入sharding命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://shardingsphere.apache.org/schema/shardingsphere/sharding
                        http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>

引入的相关新内容主要是

xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
xsi:schemaLocation="http://shardingsphere.apache.org/schema/shardingsphere/sharding
http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd"

2.2.2 物理数据源配置

这个是比较常规的数据库配置。

<!-- 数据源(使用连接池) -->
<bean id = "dataSource" class = "com.alibaba.druid.poolDruidDataSource" destroy-method = "close" >
    <!-- 数据库基本信息配置 -->
    <property name = "url" value = "${jdbc.url}" />
    <property name = "username" value = "${jdbc.username}" />
    <property name = "password" value = "${jdbc.password}" />
    <property name = "driverClassName" value = "${jdbc.driverClassName}" />
    <property name = "filters" value = "config,wall" />
    <!-- 最大并发连接数 -->
    <property name = "maxActive" value = "${jdbc.maxActive}" />
    <!-- 初始化连接数量 -->
    <property name = "initialSize" value = "${jdbc.initialSize}" />
    <!-- 配置获取连接等待超时的时间 -->
    <property name = "maxWait" value = "${jdbc.maxWait}" />
    <!-- 最小空闲连接数 -->
    <property name = "minIdle" value = "${jdbc.minIdle}" />
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name = "timeBetweenEvictionRunsMillis" value ="60000" />
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name = "minEvictableIdleTimeMillis" value ="300000" />
    <property name = "validationQuery" value = "select 1 from dual" />
    <property name = "testWhileIdle" value = "true" />
    <property name = "testOnBorrow" value = "false" />
    <property name = "testOnReturn" value = "false" />
    <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${jdbc.publicKey}" />
</bean>

项目有环境分包,所以有些参数动态获取,这里不进行展示

2.2.3 分表数据源配置

分表数据源在物理数据源的基础上进一步包装一层Sharding的数据源配置,实现一个物理数据源分为两个逻辑数据源。

<!-- 分表配置 -->
<sharding:data-source id="shardingDataSource">
    <sharding:sharding-rule data-source-names="dataSource" default-data-source-name="dataSource">
        <sharding:table-rules>
            <sharding:table-rule logic-table="t_table" actual-data-nodes="dataSource.t_table_2023_0$->{7..9}" table-strategy-ref="byCreateTimeTableStrategy" />
        </sharding:table-rules>
    </sharding:sharding-rule>
    <sharding:props>
        <prop key="sql.show">true</prop>
    </sharding:props>
</sharding:data-source>

(?)代表可缺省;(+)代表可重复配置

<sharding:data-source />

名称 类型 说明
id 属性 Spring Bean Id
sharding-rule 标签 数据分片配置规则
props (?) 标签 属性配置

<sharding:sharding-rule />

名称 类型 说明
data-source-names 属性 数据源Bean列表,本次只有单个数据源;如果有多个Bean以逗号分隔
default-data-source-name (?) 属性 未配置分片规则的表将通过默认数据源定位
table-rules 标签 表分片规则配置对象

<sharding:table-rules />

名称 类型 说明
table-rule (+) 标签 表分片规则配置对象

<sharding:table-rule />

名称 类型 说明
logic-table 属性 逻辑表名称
actual-data-nodes (?) 属性 由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况。以代码中为例,使用了inline表达式,代表数据节点是从2023_07到2023_09三个月的三张月份表
table-strategy-ref (?) 属性 表分片策略,对应sharding:xxx-strategy中的策略Id,缺省表示使用<sharding:sharding-rule />配置的默认表分片策略

<sharding:props />

名称 类型 说明
sql.show (?) 属性 是否开启SQL显示,默认值: false

其他未涉及的配置项请参考官方文档

3 外部链接

ShardingSphere-4.1.1中文说明文档:https://shardingsphere.apache.org/document/4.1.1/cn/overview/