ShardingSphere-Proxy5搭建使用

Apache ShardingSphere下的一个产品,定位为透明化的数据库代理端,与mycat类似,所有的分片都由其完成。

ShardingSphere-Proxy5下载安装

官网提供三种安装方式,这里主要记录两种

二进制包安装

  1. 官网下载二进制包apache-shardingsphere-5.2.0-shardingsphere-proxy-bin.tar.gz
  2. 下载MySQL驱动mysql-connector-java-8.0.22.jar(根据所使用的mysql下载对应版本)
  3. 将MySQl驱动放至shardingsphere-proxy解压目录中的ext-lib目录
  4. 修改配置conf/server.yaml
    server.yaml是与shardingsphere-proxy服务相关的配置,这里主要配置下权限
rules: - !AUTHORITY   users: # 配置连接shardingsphere-proxy的用户     - root@127.0.0.1:root     - sharding@:sharding   provider: # 授权模式     type: ALL_PERMITTED # 不用授权,获取所有权限
  1. 启动shardingsphere-proxy
# windows# 指定端口号和配置文件目录, 默认端口为3307bin/start.bat ${proxy_port} ${proxy_conf_directory}

![[Pasted image 20220922095217.png]]
出现以上信息代表部署成功
6. 连接测试
连接方式与mysql差不多,可以用mysql命令行连接,也可以用navicat连接。

Docker 方式安装

# 拉取镜像docker pull apache/shardingsphere-proxy# 启动临时容器docker run -d --name tmp --entrypoint=bash apache/shardingsphere-proxy:5.2.0# 配置文件拷贝docker cp tmp:/opt/shardingsphere-proxy/conf /mnt/data/shardingsphere-proxy/#删除临时容器docker rm tmp# 注意镜像的扩张依赖需要放在ext-lib目录,不能直接覆盖lib目录docker run --name shardingsphere-proxy -d -v /mnt/data/shardingsphere-proxy/conf:/opt/shardingsphere-proxy/conf -v /mnt/data/shardingsphere-proxy/ext-lib:/opt/shardingsphere-proxy/ext-lib -e PORT=3307 -p 33307:3307 apache/shardingsphere-proxy:5.2.0docker run --name shardingsphere-proxy -d -v /mnt/data/shardingsphere-proxy/conf:/opt/shardingsphere-proxy/conf -e PORT=3307 -p 33307:3307 apache/shardingsphere-proxy:5.2.0

ShardingSphere-Proxy5分库分表创建数据源

修改config-sharding.yaml文件添加数据源配置

databaseName: sharding_db ##逻辑库。配置多个逻辑库时需要创建其他的配置文件,并且配置文件格式需为config-sharding***.yamldataSources: ## 数据源,连接真实物理库,注意物理库必须有相应的库存在,负责proxy无法启动。ds_0:url: jdbc:mysql://127.0.0.1:13307/demo_ds_0?serverTimezone=UTC&useSSL=falseusername: rootpassword: sundayconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50minPoolSize: 1ds_1:url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=falseusername: rootpassword: sundayconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50minPoolSize: 1

配置分片规则

继续修改config-sharding.yaml文件添加数据分片规则

databaseName: sharding_dbdataSources: ## 数据源,连接真实物理库,注意物理库必须有相应的库存在,负责proxy无法启动。ds_0:url: jdbc:mysql://127.0.0.1:13307/demo_ds_0?serverTimezone=UTC&useSSL=falseusername: rootpassword: sundayconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50minPoolSize: 1ds_1:url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=falseusername: rootpassword: sundayconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50minPoolSize: 1## 分片规则配置rules:- !SHARDINGtables:t_order: # 分片表actualDataNodes: ds_${0..1}.t_order${0..1}databaseStrategy: # 分库规则standard: # 标准类型分片,目前官方有四种分片类型shardingColumn: user_idshardingAlgorithmName: alg_mod # 算法名称tableStrategy: # 分表规则standard:shardingColumn: order_noshardingAlgorithmName: alg_hash_mod # 算法名称,具体使用哪一种算法下面会根据算法名称配置keyGenerateStrategy: # 主键生成规则column: idkeyGeneratorName: snowflake# bindingTables: # 绑定表。对于相同分片算法的表,设置绑定,避免相互关联时产生笛卡尔关联# broadcastTables: # 广播表keyGenerators: # 主键生成规则配置snowflake:type: SNOWFLAKEshardingAlgorithms: # 分片算法配置,根据上面的算法名称配置算法的类型和算法接收的参数alg_mod:type: MODprops:sharding-count: 2alg_hash_mod:type: HASH_MODprops:sharding-count: 2

配置完成后重启proxy。

连接proxy创建分片表

配置分片表后,并没有生成相应的分片表,需要连接上sharding-proxy,在proxy中执行建表语句,在创建逻辑表时分片表会被proxy自动按照配置的规则进行创建。

CREATE TABLE `t_order` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`order_no` varchar(30) DEFAULT NULL,`user_id` bigint(20) DEFAULT NULL,`amount` decimal(10,2) DEFAULT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=779468255126355969 DEFAULT CHARSET=utf8mb4;

插入测试数据

INSERT INTO `sharding_db`.`t_order`(`id`, `order_no`, `user_id`, `amount`) VALUES (779468213359476737, '22', 22, 22.00);INSERT INTO `sharding_db`.`t_order`(`id`, `order_no`, `user_id`, `amount`) VALUES (779468285585391617, '44', 44, 44.00);INSERT INTO `sharding_db`.`t_order`(`id`, `order_no`, `user_id`, `amount`) VALUES (779468168534949888, '11', 11, 11.00);INSERT INTO `sharding_db`.`t_order`(`id`, `order_no`, `user_id`, `amount`) VALUES (779468255126355968, '33', 33, 33.00);

插入后,观察物理库的表数据存储情况。