1、业务场景:用户购物下单。
2、表
CREATE TABLE tb_user (
login_name VARCHAR (32) comment '登录名',
user_id BIGINT comment '用户标识',
TYPE INT comment '用户类型 1 商家,2买家',
passwd VARCHAR (128) comment '密码',
primary key (user_id)
) ;
alter table tb_user comment '用户表';
CREATE TABLE tb_seller_order (
seller_user_id BIGINT comment '商家标识',
buyer_user_id BIGINT comment '用户标识',
order_id BIGINT comment '订单标识',
price BIGINT comment '价格',
STATUS INT comment '状态',
primary key (order_id)
);
alter table tb_seller_order comment '商家订单表';
CREATE TABLE tb_order_detail(
seller_user_id BIGINT comment '商家标识',
order_detail_id BIGINT comment '订单明细标识',
order_id BIGINT comment '订单标识',
goods_id BIGINT comment '标识',
goods_name VARCHAR(32) comment '商品名称',
cnt INT comment '数量',
unit_price INT comment '单价',
primary key (order_detail_id)
);
alter table tb_order_detail comment '订单明细';
CREATE TABLE tb_buyer_order (
buyer_user_id BIGINT comment '用户标识',
seller_user_id BIGINT comment '商家标识',
order_id BIGINT comment '订单标识',
price BIGINT comment '价格',
STATUS INT comment '状态',
primary key (order_id)
);
alter table tb_buyer_order comment '买家订单';
3、服务器分配:
mysql:
主 192.168.109.128 备192.168.109.130
主 192.168.109.131
主 192.168.109.132
mycat:
4、表主键生成策略使用mycat全局序列生成(mycat 全局序列号:http://blog.csdn.net/convict_eva/article/details/51917499)
ORDER_ID_SQUE=dn_master
DETAIL_ID_SQUE=dn_master
在dn_master结点执行:
insert into MYCAT_SEQUENCE(NAME,current_value,increment)
values('USER_ID_SQUE',1,100),('ORDER_ID_SQUE',1,100),('DETAIL_ID_SQUE',1,100);
重启mycat测试:
SELECT NEXT VALUE FOR MYCATSEQ_USER_ID_SQUE;
SELECT NEXT VALUE FOR MYCATSEQ_ORDER_ID_SQUE;
SELECT NEXT VALUE FOR MYCATSEQ_DETAIL_ID_SQUE;
5、配置分片规则
在mycat conf 目录下的rule.xml 是分片规则的配置文件。
5.1 配置function 标签,找到function 标签name="mod-long" 的function 配置,修改总结点数为3(和使用的mysql 结点数一致)。
<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">3</property>
</function>
<tableRule name="tb_user_mod-long">
</rule>
</tableRule>
<!-- 卖家订单分片规则 -->
<tableRule name="seller_order_mod-long">
<rule>
<columns>seller_user_id</columns>
要求:把订单分片,商家要查询购买在订单,用户要查询自己的订单。
表设计:用户,商家订单,订单明细,买方订单
分片规则:
“用户表”---user_id 取模,“商家订单”----seller_user_id 取模
“订单明细”----“商家订单”ER 分片,“买家订单”----buyer_user_id 取模2、表
CREATE TABLE tb_user (
login_name VARCHAR (32) comment '登录名',
user_id BIGINT comment '用户标识',
TYPE INT comment '用户类型 1 商家,2买家',
passwd VARCHAR (128) comment '密码',
primary key (user_id)
) ;
alter table tb_user comment '用户表';
CREATE TABLE tb_seller_order (
seller_user_id BIGINT comment '商家标识',
buyer_user_id BIGINT comment '用户标识',
order_id BIGINT comment '订单标识',
price BIGINT comment '价格',
STATUS INT comment '状态',
primary key (order_id)
);
alter table tb_seller_order comment '商家订单表';
CREATE TABLE tb_order_detail(
seller_user_id BIGINT comment '商家标识',
order_detail_id BIGINT comment '订单明细标识',
order_id BIGINT comment '订单标识',
goods_id BIGINT comment '标识',
goods_name VARCHAR(32) comment '商品名称',
cnt INT comment '数量',
unit_price INT comment '单价',
primary key (order_detail_id)
);
alter table tb_order_detail comment '订单明细';
CREATE TABLE tb_buyer_order (
buyer_user_id BIGINT comment '用户标识',
seller_user_id BIGINT comment '商家标识',
order_id BIGINT comment '订单标识',
price BIGINT comment '价格',
STATUS INT comment '状态',
primary key (order_id)
);
alter table tb_buyer_order comment '买家订单';
3、服务器分配:
mysql:
主 192.168.109.128 备192.168.109.130
主 192.168.109.131
主 192.168.109.132
mycat:
192.168.109.129
4、表主键生成策略使用mycat全局序列生成(mycat 全局序列号:http://blog.csdn.net/convict_eva/article/details/51917499)
添加mycat全局序列号:
USER_ID_SQUE=dn_masterORDER_ID_SQUE=dn_master
DETAIL_ID_SQUE=dn_master
在dn_master结点执行:
insert into MYCAT_SEQUENCE(NAME,current_value,increment)
values('USER_ID_SQUE',1,100),('ORDER_ID_SQUE',1,100),('DETAIL_ID_SQUE',1,100);
重启mycat测试:
SELECT NEXT VALUE FOR MYCATSEQ_USER_ID_SQUE;
SELECT NEXT VALUE FOR MYCATSEQ_ORDER_ID_SQUE;
SELECT NEXT VALUE FOR MYCATSEQ_DETAIL_ID_SQUE;
5、配置分片规则
在mycat conf 目录下的rule.xml 是分片规则的配置文件。
5.1 配置function 标签,找到function 标签name="mod-long" 的function 配置,修改总结点数为3(和使用的mysql 结点数一致)。
<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">3</property>
</function>
5.2 配置表分片(name 要全局唯一),rule.xml 配置添加如下配置
<!-- user分片规则 --><tableRule name="tb_user_mod-long">
<rule>
<!-- 分片使用的字段 -->
<columns>user_id</columns>
<!-- 分片使用的方法,对应function 名称 -->
<algorithm>mod-long</algorithm></rule>
</tableRule>
<!-- 卖家订单分片规则 -->
<tableRule name="seller_order_mod-long">
<rule>
<columns>seller_user_id</columns>