===^^===
《榴芒客服系统》是我们工作室开发的在线客服系统,欢迎下载试用:
《榴芒客服系统》
https://blog.csdn.net/look4liming/article/details/164755808
安装ShardingSphere代理服务器:
下载apache-shardingsphere-5.5.0-shardingsphere-proxy-bin.tar.gz,解压缩;
解压后的文件夹再复制一份,这样一份作为源,一份作为目标;
修改conf/database-shardingsphere.yaml和global.yaml,修改后的文件在本教程的同一目录下,请参考;
迁移数据还需要安装zookeeper,其它的都不要动,配置文件需要修改,修改后的配置文件也在本教程的同目录下。
====================================
启动ShardingSphere代理:
start.bat 8810
====================================
连接ShardingSphere代理:
psql -h localhost -p 8890 -U root -d sharding_db
mysql -u root -p -h 127.0.0.1 -P 8899
====================================
1、在源库代理上执行建表及插入数据操作:
create table t_user(
id int primary key,
username varchar(64) null
);
insert into t_user(id, username) values(1, 'jack');
insert into t_user(id, username) values(2, 'rose');
insert into t_user(id, username) values(3, 'mike');
insert into t_user(id, username) values(4, 'bob');
2、在目标库代理上注册目标存储单元:
register storage unit target_ds_0 (
url="jdbc:postgresql://39.106.151.66:5432/demo_ds_2_0",
user="pgyht",
password="pgyht"
);
register storage unit target_ds_1 (
url="jdbc:postgresql://39.106.151.66:5432/demo_ds_2_1",
user="pgyht",
password="pgyht"
);
register storage unit target_ds_2 (
url="jdbc:postgresql://39.106.151.66:5432/demo_ds_2_2",
user="pgyht",
password="pgyht"
);
3、在目标库代理上创建读写分离规则:
create readwrite_splitting rule rw_ds_0 (
write_storage_unit=target_ds_0,
read_storage_units(target_ds_0),
type(name="random")
);
create readwrite_splitting rule rw_ds_1 (
write_storage_unit=target_ds_1,
read_storage_units(target_ds_1),
type(name="random")
);
create readwrite_splitting rule rw_ds_2 (
write_storage_unit=target_ds_2,
read_storage_units(target_ds_2),
type(name="random")
);
4、在目标库代理上创建分库分表规则:
create sharding table rule t_user(
storage_units(rw_ds_0 rw_ds_1, rw_ds_2),
sharding_column=id,
type(name="hash_mod", properties("sharding-count"="3")),
key_generate_strategy(column=id,type(name="snowflake"))
);
5、在目标代理上注册源存储单元:
unregister migration source storage unit source_ds_0;
unregister migration source storage unit source_ds_1;
register migration source storage unit source_ds_0(
url="jdbc:prostgresql://39.106.151.66:5432/demo_ds_0",
user="pgyht",
password="pgyht";
);
register migration source storage unit source_ds_1(
url="jdbc:prostgresql://39.106.151.66:5432/demo_ds_1",
user="pgyht",
password="pgyht";
);
6、在目标库代理上执行扩容:
migrate table source_ds_0.t_user_0 into sharding_db.t_user;
7、在目标库代理上查看任务列表:
show migration list;
8、在目标库代理上查看任务详情:
show migration status {jobId};
9、在目标库代理上查看数据一致性:
show migration check status {jobId};
10、在目标库代理上查看数据一致性校验算法:
show migration check algorithms;
11、提交扩容操作:
commit migration {jobId};
===================================
elasticsearch-7.6.2
apache-zookeeper-3.9.2
apache-shardingsphere-5.5.0-shardingsphere-proxy-bin
===================================
ShardingSphere问题总结:
1、不做全局唯一性检查(包括主键冲突检查),除非两条主键一样的数据落在了同一个数据节点上。
2、Shardingsphere连接ZooKeeper,会把本地的配置文件同步到ZooKeeper上,如果不清掉ZooKeeper上的相关配置信息,修改本地配置文件将会造成部分修改无法生效(消耗了一下午的时间才定位到该问题)。
3、被作为散列键的字段,不允许修改,修改的话会报错。
4、分库字段和分表字段不能是同一个字段,否则会出现问题,比如框架不建表。