利用ShardingSphere-JDBC实现分库分表--配置中心的实现

本文介绍使用ShardingSphere-JDBC结合Zookeeper作为配置中心,简化大规模分库分表配置管理的方法。通过具体配置示例,解决配置文件膨胀问题,并提供排除依赖冲突的Gradle配置方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在之前的文章中我详细描述了如何利用ShardingSphere-JDBC进行分库分表,同时也实现了简单的精确分库算法接口,详情见下面的链接:

利用ShardingSphere-JDBC实现分库分表

但是观察一下配置文件,我现在只有两张表的情况下就已经用了60行来做配置,如果说我在一个真实的系统中,那么配置文件的规模将是非常可观的,这个时候配置中心的作用就很重要了。ShardingSphere支持主流的zookeeper和etcd等服务发现组件,作为最常用的,我用zookeeper来实现。

下面是关于zookeeper部分的配置:

# 名字随便起一个
spring.shardingsphere.orchestration.name=spring_boot_ds_sharding
# 覆盖配置中心的配置,以本地为准
spring.shardingsphere.orchestration.overwrite=true
spring.shardingsphere.orchestration.registry.type=zookeeper
# 名字随便起一个,这是我们这个集群的名称,其他的集群可以用这个也可以用自己单独的,作为资源隔离
spring.shardingsphere.orchestration.registry.namespace=shardingsphere
spring.shardingsphere.orchestration.registry.server-lists=127.0.0.1:2181

服务启动以后就会自动的将这些配置上传到配置好的配置中心去,未来只需要修改配置中心就可以。

需要注意一点,官方文档中没有讲明白gradle或者maven的配置,如果按照文档上讲得直接把相关的starter配置进去,启动会报错,这是因为jar包冲突导致的,我这里给出gradle的配置,maven的可以参考修改:

compile('org.apache.shardingsphere:sharding-jdbc-orchestration-spring-boot-starter:4.0.0-RC2')
{
    exclude group: 'org.apache.curator', module: 'curator-framework'
}
compile('org.apache.shardingsphere:sharding-orchestration-reg-zookeeper-curator:4.0.0-RC2')
{
    exclude group: 'org.apache.curator', module: 'curator-framework'
    exclude group: 'org.apache.curator', module: 'curator-recipes'
    exclude group: 'org.apache.curator', module: 'curator-client'
}
compile group: 'org.apache.curator', name: 'curator-framework', version: '2.10.0'
compile group: 'org.apache.curator', name: 'curator-recipes', version: '2.10.0'

我们可以在zkCli上看看上传上去的配置信息:

配置中心

至此我们就完成了配置中心的配置工作。在以后的篇幅中,我还会提及数据脱敏等其他内容,要是有时间的话,我还希望能够深入源码内部了解一下这个组件。

转载于:https://www.cnblogs.com/wingsless/p/11432501.html

### 使用 ShardingSphere-JDBCJava实现分表 ShardingSphere-JDBC 是一个强大的分布式数据库中间件,能够帮助开发者轻松实现分库分表功能。为了在 Java 应用程序中利用 ShardingSphere-JDBC 进行分表操作,以下是详细的设置过程和代码示例。 #### 1. 添加依赖项 首先,在项目的 `pom.xml` 文件中添加必要的 Maven 依赖: ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>5.0.0</version> </dependency> <!-- 数据源驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> ``` #### 2. 配置数据源 接着,在 `application.yml` 或者 `application.properties` 文件里定义多个数据源以及分片策略的相关参数: ```yaml spring: shardingsphere: datasource: names: ds_0,ds_1 ds_0: 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_1: 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 rules: sharding: tables: t_order: actual-data-nodes: ds_${0..1}.t_order${0..1} table-strategy: standard: sharding-column: order_id precise-algorithm-class-name: com.example.demo.algorithm.ModuloTableShardingAlgorithm ``` 上述配置指定了两个物理数据库实例 (`ds_0`, `ds_1`) 和逻辑上的订单表 (`t_order`). 同时还设置了基于 `order_id` 的取模算法来决定具体的数据分布[^1]. #### 3. 编写自定义分片算法类 创建一个新的 Java 类用于定义具体的分片规则: ```java package com.example.demo.algorithm; import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm; import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue; import java.util.Collection; import java.util.Properties; public class ModuloTableShardingAlgorithm implements PreciseShardingAlgorithm<Long> { @Override public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Long> shardingValue) { for (String each : availableTargetNames) { if (each.endsWith(shardingValue.getValue() % 2 + "")) { return each; } } throw new UnsupportedOperationException(); } @Override public Properties getProps() { return null; } } ``` 这段代码实现了简单的取模运算作为分片依据,即当 `order_id` 对应的哈希值除以指定数量的结果等于某个特定数值时,则该记录会被分配至相应的子表内. #### 4. 测试验证 最后一步是在项目中编写单元测试或其他形式的应用逻辑去检验整个流程是否正常工作。确保所有的 CRUD 操作都能够正确地映射到对应的分片位置上。 通过以上步骤就可以成功地使用 ShardingSphere-JDBC 来完成基本的分表处理了。当然实际应用场景可能会更加复杂一些,可能涉及到更多的业务场景和技术细节调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值