使用shardingsphere实现SQL server数据库分表

一、项目需求
项目中数据量比较大,单表千万级别的,需要实现分表,于是在网上搜索这方面的开源框架,最常见的就是mycat,sharding-sphere,最终我选择后者,用它来做分库分表比较容易上手。
二、简介sharding-sphere
官网地址(有中文版)
sharding-jdbc 定位为轻量级Java框架,在Java的JDBC层提供的额外服务。 它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的JDBC驱动,完全兼容JDBC和各种ORM框架。
三、项目实战
本项目使用sharding-sphere + tk.mybatis 实现分库分表
1.pom依赖

<dependency>
			<groupId>org.apache.shardingsphere</groupId>
			<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
			<version>4.1.1</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.22</version>
		</dependency>

2.创建分表
表结构一致
3.application.properties (重点)基本是在这个文件配置的

# 配置Sharding-JDBC的分片策略
# 配置数据源,给数据源起名ds1,ds2...此处可配置多数据源
spring.shardingsphere.datasource.names=ds1,ds2

# 一个实体类对应两张表,覆盖
#spring.main.allow-bean-definition-overriding=true

# 配置数据源具体内容————————包含  连接池,  驱动,             地址,   用户名,    密码
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.shardingsphere.datasource.ds1.url=jdbc:sqlserver://localhost:1433;DatabaseName=heinz;loginTimeout=30;sendStringParametersAsUnicode=false
spring.shardingsphere.datasource.ds1.username=sa
spring.shardingsphere.datasource.ds1.password=506617

spring.shardingsphere.datasource.ds2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds2.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.shardingsphere.datasource.ds2.url=jdbc:sqlserver://localhost:1433;DatabaseName=heinz;loginTimeout=</
### ShardingSphere实现读写分离与分库分表 #### 配置环境准备 为了在 ShardingSphere实现读写分离和分库分表,需先准备好相应的开发环境。确保使用的 `shardingsphere-proxy` 和 MySQL 版本兼容[^1]。 #### 数据源配置 定义数据源时,需要指定主数据库用于写操作,而多个从属数据库则负责处理读请求。通过合理分配读流量到不同的副本上可以有效提升系统的并发性能和服务可用性。 ```yaml dataSources: ds_master: # 主库配置 url: jdbc:mysql://localhost:3306/db_name?serverTimezone=UTC&useSSL=false username: root password: pwd ds_slave_0: # 副本一配置 url: jdbc:mysql://localhost:3307/db_name?serverTimezone=UTC&useSSL=false username: root password: pwd ds_slave_1: # 副本二配置 url: jdbc:mysql://localhost:3308/db_name?serverTimezone=UTC&useSSL=false username: root password: pwd ``` #### 分片策略设置 针对业务需求设计合理的分片算法是至关重要的一步。这里采用基于用户 ID 的哈希取模方式来决定具体的数据分布逻辑: ```java public class UserIDModuloShardingAlgorithm implements StandardShardingAlgorithm<Long> { @Override public String doEqualSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) { Long userId = shardingValue.getValue(); int targetIndex = Math.abs(userId.hashCode()) % availableTargetNames.size(); return new ArrayList<>(availableTargetNames).get(targetIndex); } } ``` 此方法能够保证相同用户的记录总是被存储在同一张物理表内,便于后续查询优化。 #### 启用负载均衡机制 为了让应用程序自动识别并利用上述配置完成实际的读写分离工作,在规则设定环节还需加入如下参数调整: ```properties spring.shardingsphere.rules.readwrite-splitting.data-sources.ds.type=MASTER_SLAVE spring.shardingsphere.rules.readwrite-splitting.data-sources.ds.load-balancer-name=round_robin ``` 此处选择了轮询法作为默认路由策略,即每次访问都会依次切换至下一个可选节点执行相应 SQL 请求。 #### 完整示例代码片段展示 下面给出一段完整的 Spring Boot 应用程序 YML 文件中的相关内容摘录供参考学习之用: ```yml spring: shardingsphere: datasource: names: ds_master,ds_slave_0,ds_slave_1 ds_master: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/db_name?serverTimezone=UTC&useSSL=false username: root password: pwd ds_slave_0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3307/db_name?serverTimezone=UTC&useSSL=false username: root password: pwd ds_slave_1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3308/db_name?serverTimezone=UTC&useSSL=false username: root password: pwd rules: readwrite-splitting: data-sources: ds: type: MASTER_SLAVE load-balancer-name: round_robin props: check-datasource-available: true static-data-source-groups: master-group: ds_master slave-group: ds_slave_0,ds_slave_1 sharding: tables: t_order: actual-data-nodes: ds_${0..1}.t_order_$->{0..3} # 表名模板表达式 table-strategy: standard: sharding-column: user_id sharding-algorithm-name: userid_modulo_algorithm key-generate-strategy: column: order_id key-generator-name: snowflake_algorithm default-database-strategy: standard: sharding-column: user_id sharding-algorithm-name: userid_modulo_algorithm algorithms: userid_modulo_algorithm: type: INLINE props: algorithm-expression: ds_${user_id % 2} snowflake_algorithm: type: SNOWFLAKE ``` 这段配置不仅实现了基本的读写分离功能,同时也完成了简单的水平拆分任务——按照用户ID奇偶数分别存入两个不同实例下的四张订单明细子表之中。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值