Zookeeper和Mycat
- 使用ZK对mycat 的配置进行管理
- 使用ZK修改配置,Mycat配置自动更新
- 使用Mycat-web监控必须基于ZK之上
- 通过监控ZK节点情况就可以知道Mycat每个节点的运行情况
集成Zookeeper和Mycat
第一步:启动ZK集群
第二步:修改myid.properties
loadZk=truezkURL=192.168.225.129:2181clusterId=mycat-cluster-1myid=mycat_fz_01clusterNodes=mycat_fz_01,mycat_fz_02,mycat_fz_04#server booster ; booster install on db same server,will reset all minCon to 1type=serverboosterDataHosts=dn2,dn3
loadZk:默认值false。代表mycat集群是否使用ZK
zkURL:zk集群的地址
clusterId:mycat集群名字
myid:当前的mycat服务器名称
clusterNodes:把所有集群中的所有mycat服务器罗列进行以逗号隔开,比如:
clusterNodes=mycat_fz_01,mycat_fz_02,mycat_fz_03
第三步:使用Mycat脚本初始化mycat在ZK中的节点数据。
第四步:启动mycat
ps:
1 修改完配置文件如果没有热配置时则需要手动重启mycat
2 采用zk管理mycat配置文件时,第一次会把所有配置文件初始化为原来的配置。所以需要重新配置集群信息。
Mycat监控
Mycat-eye启动后的2个服务端口:
8082:web服务端口
8999:jmx服务端口
修改mycat-web项目下/WEB-INF/classes/mycat.properties中zookeeper属性。
需要手动初始化的项:
- mycat配置管理。增加mycat服务器配置
- Mycat-mv管理。配置JMX服务项。
- MySQL配置管理。配置docker mysql地址和db名称
Mycat数据迁移(扩容)
数据样例:
insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom001', 10010, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom002', 10000, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom003', 10010, 3);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom004', 10010, 4);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom005', 10010, 5);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom006', 10000, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom007', 10000, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom008', 10000, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom009', 10010, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom010', 10010, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom011', 10010, 3);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom012', 10010, 4);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom013', 10000, 5);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom014', 10000, 5);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom015', 10010, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom016', 10000, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom017', 10010, 3);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom018', 10000, 3);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom019', 10010, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom021', 10000, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom022', 10010, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom023', 10000, 5);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom024', 10000, 4);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom025', 10000, 5);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Tom026', 10010, 3);
迁移需求:
当前数据分布在dn1,dn2,迁移后要分布在dn1,dn2,dn3
迁移步骤(以一致性hash分片规则为例):
1.生成一个新的schema文件,文件名newSchema.xml。(新的分片定义)
2.生成一个新的rule文件,文件名newRule.xml。(新的分片规则)
3.修改migrateTables.properties文件。写明要迁移的schema和表,多张表用逗号隔开。
4.停止mycat(也可以不停止mycat)。
5.执行dataMigrate.sh开始迁移(扩容)。
修改newRule.xml成为rule.xml,修改newSchema.xml为schema.xml。重启mycat。
Mycat+Spring
官方的一个例子地址:
springMVC+ibatis+FreeMarker连接mycat示例: