ShardingSphere-Proxy 分库分表 简单示例

本文提供了一个使用ShardingSphere-Proxy进行分库分表的简单示例。通过设置MySQL环境和配置ShardingSphere-Proxy 5.0.0 alpha,将订单表水平拆分为2个库,每库16张表。然后,介绍了如何在SpringBoot Mybatis应用中进行相关配置,包括数据库连接配置和测试类代码。

ShardingSphere-Proxy 分库分表 简单示例


简要说明

    对一张简单的订单表数据表进行水平分库分表,拆分2个库,每个库16张表并在新结构在演示常见的增删改查操作

环境配置

设置MySQL

    使用docker设置mysql

# 启动两个mysql
docker run --name mysql11 -p 3311:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% -d mysql:latest
docker run --name mysql12 -p 3312:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% -d mysql:latest

# 在11上创建数据库demo_ds_0
docker exec -ti mysql11 mysql -u root -p
create database demo_ds_0;

# 在12上创建数据库demo_ds_1
docker exec -ti mysql11 mysql -u root -p
create database demo_ds_1;
ShardingSphere-Proxy 5.0.0 alpha 设置

    docker一直不能设置成功,有点奇怪,这里就直接下载使用了

    下面需要配置两个文件:server.yaml、config-sharding.yaml,示例如下(配置都有默认示例说明的,相应进行修改即可)

    server.yaml

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

######################################################################################################
# 
# If you want to configure governance, authorization and proxy properties, please refer to this file.
# 
######################################################################################################
#
#governance:
#  name: governance_ds
#  registryCenter:
#    type: ZooKeeper
#    serverLists: localhost:2181
#    props:
#      retryIntervalMilliseconds: 500
#      timeToLiveSeconds: 60
#      maxRetries: 3
#      operationTimeoutMilliseconds: 500
#  overwrite: false

authentication:
  users:
    root:
      password: root
    sharding:
      password: sharding
      authorizedSchemas: sharding_db

props:
  max-connections-size-per-query: 1
  acceptor-size: 16  # The default value is available processors count * 2.
  executor-size: 16  # Infinite by default.
  proxy-frontend-flush-threshold: 128  # The default value is 128.
    # LOCAL: Proxy will run with LOCAL transaction.
    # XA: Proxy will run with XA transaction.
    # BASE: Proxy will run with B.A.S.E transaction.
  proxy-transaction-type: LOCAL
  proxy-opentracing-enabled
<think>好的,用户的问题是关于如何在ShardingSphere-Proxy 5中实现自定义分表算法的。我需要先回顾一下之前的引用内容,看看有没有相关的信息。引用[2]提到了需要实现StandardShardingAlgorithm接口,并且要重写doSharding方法。引用[3]可能提供了分库分表的基本使用方式,但用户需要的是自定义算法,所以重点应该在引用[2]的内容。 首先,我应该确认用户使用的ShardingSphere版本是否支持自定义分片算法。根据引用中的信息,用户提到的是5.x版本,所以需要确保步骤适用于该版本。然后,用户可能需要具体的实现步骤,包括接口实现、配置方法以及示例代码。 接下来,我需要考虑用户可能的背景。他们可能已经了解分库分表的基本概念,现在需要扩展或自定义分片规则。因此,回答需要详细说明实现步骤,并提供一个简单示例,比如按用户ID取模分片。同时,需要提醒用户注意配置文件的正确性,以及如何部署和测试自定义算法。 可能的问题点包括:如何正确实现StandardShardingAlgorithm接口,如何处理精确分片和范围分片的情况,以及如何将自定义类打包并配置到ShardingSphere-Proxy中。此外,还要检查是否需要将实现类所在的JAR包放入proxy的lib目录下,确保类加载正确。 最后,生成的相关问题应该围绕用户可能遇到的后续问题,比如算法优化、调试技巧或集成配置中的常见错误。这有助于用户进一步探索和解决问题。</think>### ShardingSphere-Proxy 5 自定义分表算法实现步骤 #### 一、核心接口实现 需实现`StandardShardingAlgorithm`接口,重点重写两个方法: $$doSharding(Collection<String> availableTargetNames, RangeShardingValue<Comparable<?>> shardingValue)$$ $$doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Comparable<?>> shardingValue)$$ 引用[2]明确指出该接口是实现标准分片算法的关键[^2] #### 二、示例:用户ID取模分表 ```java public class UserIdModSharding implements StandardShardingAlgorithm<Long> { private Properties props; // 精确分片(=, IN) public String doSharding(Collection<String> tables, PreciseShardingValue<Long> shardingValue) { long userId = shardingValue.getValue(); return "user_" + (userId % 4); // 分4张表 } // 范围分片(BETWEEN) public Collection<String> doSharding(Collection<String> tables, RangeShardingValue<Long> shardingValue) { return tables; // 返回所有表进行全路由 } public Properties getProps() { return props; } public void init(Properties props) { this.props = props; } } ``` #### 三、配置文件配置 ```yaml rules: - !SHARDING tables: user: actualDataNodes: ds_${0..1}.user_${0..3} tableStrategy: standard: shardingColumn: user_id shardingAlgorithmName: user_mod shardingAlgorithms: user_mod: type: CLASS_BASED props: strategy: STANDARD algorithmClassName: com.example.algorithm.UserIdModSharding ``` #### 四、部署注意事项 1. 将编译后的JAR包放入`/ext-lib`目录 2. 重启Proxy服务后通过`SHOW SHARDING ALGORITHMS;`验证算法注册 3. 使用引用[4]的连接命令测试分片效果[^4]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值