
1. ShardingSphere-ProxyShardingSphere-Proxy的核心定位是作为一个透明化的数据库代理。 它对应用程序完全透明您可以直接将其当作普通的 MySQL 或 PostgreSQL 数据库来使用。 任何兼容 MySQL/PostgreSQL 协议的客户端如 MySQL Command Client, Workbench, Navicat 等都可以直接连接并操作它这对 DBA 和非Java技术栈的开发者非常友好。2.准备网络环境本机 192.168.147.1虚拟机 192.168.147.130数据库版本5.6.51使用时要考虑数据库及shardingsphere-proxy镜像版本兼容性测试连接数据库时需要关闭本机的防火墙创建所要使用的目录及文件config-sharding.yaml文件databaseName: db dataSources: ds_0: url: jdbc:mysql://192.168.147.1:3306/db?serverTimezoneUTCuseSSLfalsecharacterEncodingutf8 username: root password: *Root12* connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 rules: - !SHARDING tables: t_record: actualDataNodes: ds_0.t_record_$-{2023..2026} tableStrategy: standard: shardingColumn: created_at shardingAlgorithmName: t_record_year_interval keyGenerateStrategy: # 主键生成策略 column: id keyGeneratorName: snowflake shardingAlgorithms: t_record_year_interval: type: INTERVAL props: datetime-pattern: yyyy-MM-dd HH:mm:ss datetime-lower: 2023-01-01 00:00:00 datetime-upper: 2026-12-31 23:59:59 sharding-suffix-pattern: yyyy datetime-interval-amount: 1 datetime-interval-unit: Years keyGenerators: snowflake: type: SNOWFLAKEserver.yamlmode: type: Standalone repository: type: File props: path: ./metadata rules: - !AUTHORITY users: - root%:*Root12* - sharding%:root provider: type: NATIVE props: sql-show: true连接数据库驱动3. docker 启动docker run -d --name shardingsphere-proxy --network host -v /home/shardingsphere/conf:/opt/shardingsphere-proxy/conf -v /home/shardingsphere/ext-lib:/opt/shardingsphere-proxy/ext-lib apache/shardingsphere-proxy:5.1.24.测试分表1.根据分表规则进行物理建表1.使用命令行中进行连接分表的数据库进行测试。可以看到物理表中已经存在分表但是在连接分表之后的数据库中只是出现t_record。然后通过在分表的数据库进行增加数据在物理表中查到数据进行测试。