Sharding-Proxy是ShardingSphere的第二个产品。 它定位为透明化的数据库代理端,提供封装了数据库二进制协议的服务端版本,用于完成对异构语言的支持。
目前先提供MySQL/PostgreSQL版本,它可以使用任何兼容MySQL/PostgreSQL协议的访问客户端(如:MySQL Command Client, MySQL Workbench, Navicat等)操作数据,
对DBA更加友好。
- 向应用程序完全透明,可直接当做MySQL/PostgreSQL使用。
- 适用于任何兼容MySQL/PostgreSQL协议的的客户端
定位为透明的数据库代理端
Sharding-Proxy独立应用,需要安装服务,进行分库分表或者读写分离配置,启动使用
tar zxvf apache-shardingsphere-5.0.0-beta-shardingsphere-proxy-bin.tar.gz
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://127.0.0.1:3306/edu_db_1?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
maintenanceIntervalMilliseconds: 30000
rules:
- !SHARDING
tables:
t_order:
actualDataNodes: ds_${0}.t_order_${0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_${order_id % 2}
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
bindingTables:
- t_order
defaultDatabaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: ds_${0}
defaultTableStrategy:
none:
(1)Sharding-Proxy默认端口号 3307
(1) 打开cmd窗口连接Sharding-Proxy,连接方式和连接mysql一样的
(2) 进行sql命令操作看到只有一个库
(3) 在sharding_db数据库创建表
create table if not exists t_order (
order_id bigint not null,
user_id int not null,
status varchar(5),
primary key(order_id)
);
(4) 向表添加一条记录
insert into t_order(order_id, user_id, status) values (11, 1, 'init');
(5) 回到本地3306端口实际数据库中,看到已经创建好了表和添加数据
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://127.0.0.1:3306/edu_db_1?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
maintenanceIntervalMilliseconds: 30000
ds_1:
url: jdbc:mysql://127.0.0.1:3306/edu_db_2?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
maintenanceIntervalMilliseconds: 30000
rules:
- !SHARDING
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order_${1..2}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_${order_id % 2 + 1}
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
bindingTables:
- t_order
defaultDatabaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: ds_${user_id % 2}
defaultTableStrategy:
none:
mysql -P3307 -uroot -p
(1) 创建数据库表,向表添加记录
create table if not exists t_order (
order_id bigint not null,
user_id int not null,
status varchar(5),
primary key(order_id)
);
insert into t_order(order_id, user_id, status) values (11, 1, 'init');
(2) 连接本地3306的MySql数据库服务器,表已经创建出来,表里面有数据
schemaName: readwrite_splitting_db
dataSources:
write_ds:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_master?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
maintenanceIntervalMilliseconds: 30000
read_ds_0:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_slave_0?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
maintenanceIntervalMilliseconds: 30000
read_ds_1:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_slave_1?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
maintenanceIntervalMilliseconds: 30000
rules:
- !READWRITE_SPLITTING
dataSources:
pr_ds:
writeDataSourceName: write_ds
readDataSourceNames:
- read_ds_0
- read_ds_1
(1)在主数据库和从数据库里面,都创建数据库表
(2)向表添加记录,不指定向哪个库添加
把添加数据添加到主数据库里面
(3)查询数据库表数据,不指定查询哪个库
直接执行查询从库里面的数据
Sharding-JDBC与Sharding-Proxy、视频教程