go-zero 成长之路—微服务电商实战系列(三、表结构篇)

该系列源码已开源:micro-shop

1. 概述

前两篇文章分别介绍了本系列文章的背景以及根据业务职能对商城系统做了服务的拆分,其中每个服务又可分为如下三类:

  • api服务 - BFF层,对外提供HTTP接口
  • rpc服务 - 内部依赖的微服务,实现单一的业务功能

如果没看过前两篇文章可通过如下传送门查看

前两篇文章比较偏理论知识多一些,大家看着也比较累。但是,正所谓磨刀不误砍柴工,在真实开发场景中,我们一般会花大量的时间在需求的理解和业务逻辑上进行梳理和整理,以便开发时避免返工的可能,甚至重构,所以前期投入搭大量的时间是值得的。当我们需求明确,业务清晰,开发时就会事半功倍,效率也会大大的提升。

2. 表结构定义

不同的微服务间需要做数据的隔离,每个微服务独占数据库资源,通过RPC调用来获取数据依赖,整体架构如下图所示:

在这里插入图片描述

通过以上对API的定义我们大致了解了需要哪些数据字段,下面开始进行数据表的设计,建表语句放在每个服务的model目录下的sql文件中,该文件会不断更新,主要涉及的库和表定义如下:

用户表主要保存用户信息,在user库中后续可能还会扩展比如用户积分,用户等级等功能:

-- auto-generated definition
create table user
(
    id           bigint unsigned auto_increment comment '用户ID'
        primary key,
    userIdentity varchar(255) default ''                null comment '用户唯一标识',
    userName     varchar(50)  default ''                not null comment '用户名',
    passWord     varchar(50)  default ''                not null comment '用户密码,MD5加密',
    userNick     varchar(100) default ''                null comment '用户昵称',
    userFace     varchar(255) default ''                null comment '用户头像地址',
    UserSex      tinyint(1)   default 0                 null comment '用户性别:0男,1女,2保密',
    userEmail    varchar(255) default ''                null comment '用户邮箱',
    userPhone    varchar(20)  default ''                not null comment '手机号',
    loginAddress varchar(255) default ''                null comment '用户登录IP地址',
    create_time  timestamp    default CURRENT_TIMESTAMP not null comment '创建时间',
    update_time  timestamp    default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    constraint userName
        unique (userName)
)
    comment '用户表' charset = utf8mb4;

create index userPhone
    on user (userPhone);

商品库中主要涉及商品表和商品分类表:

-- auto-generated definition
create table product
(
    id             bigint unsigned auto_increment comment '商品id'
        primary key,
    cate_id        smallint(6) unsigned default 0    not null comment '类别Id',
    name           varchar(100)         default ''   not null comment '商品名称',
    property       varchar(255)         default ''   not null comment '商品属性',
    coverPic       varchar(255)         default ''   not null comment '商品封面',
    images         varchar(1024)        default ''   not null comment '图片地址,逗号分隔',
    detail         varchar(1024)        default ''   not null comment '商品详情',
    marketPrice    decimal(20, 2)       default 0.00 not null comment '市场价格,单位-元保留两位小数',
    discountPrice  decimal(20, 2)       default 0.00 not null comment '折扣价格, 单位元-保留两位小数',
    stock          int                  default 0    not null comment '库存数量',
    status         int(6)               default 1    not null comment '商品状态.1-在售 2-下架 3-删除',
    createTimeTamp int                               null comment '创建时间',
    updateTimeTamp int                               null comment '更新时间'
)
    comment '商品表' charset = utf8mb4;

create index cate_id
    on product (cate_id);

create index name
    on product (name);

-- auto-generated definition
create table category
(
    id             smallint(6) unsigned auto_increment comment '分类id'
        primary key,
    parent_id      smallint    default 0                 not null comment '父类别id当id=0时说明是根节点,一级类别',
    name           varchar(50) default ''                not null comment '类别名称',
    status         tinyint     default 1                 not null comment '类别状态1-正常,2-已废弃',
    createTimeTamp int                               null comment '创建时间',
    updateTimeTamp int                               null comment '更新时间'
)
    comment '商品类别表' charset = utf8mb4;

购物车表:

-- auto-generated definition
create table cart
(
    id         bigint unsigned auto_increment comment '购物车id'
        primary key,
    userId     bigint unsigned default 0 not null comment '用户id',
    productId  bigint unsigned default 0 not null comment '商品id',
    buyCount   int             default 0 not null comment '数量',
    checked    int             default 0 not null comment '是否选中,1=选中,0=未选',
    createTime int                       null comment '创建时间',
    updateTime int                       null comment '更新时间'
)
    comment '购物车表' charset = utf8mb4;

create index ix_product_id
    on cart (productId);

create index ix_user_id
    on cart (userId);

订单库中主要涉及到订单表和订单地址表:

-- auto-generated definition
create table orders
(
    id          varchar(64)     default ''   not null comment '订单id'
        primary key,
    userId      bigint unsigned default 0    not null comment '用户id',
    addressId   bigint          default 0    not null comment '收货信息表id',
    price       decimal(20, 2)  default 0.00 null comment '实际付款金额,单位是元,保留两位小数',
    priceType   tinyint         default 1    not null comment '支付类型,1-在线支付',
    sendPrice   int(10)         default 0    not null comment '运费,单位是元',
    orderStatus smallint        default 10   not null comment '订单状态:0-已取消-10-未付款,20-已付款,30-待发货 40-待收货,50-交易成功,60-交易关闭',
    payTime     int                          null comment '支付时间',
    sendTime    int                          null comment '发货时间',
    endTime     int                          null comment '交易完成时间',
    closeTime   int                          null comment '交易关闭时间',
    createTime  int                          null comment '创建时间',
    updateTime  int                          null comment '更新时间'
)
    comment '订单表' charset = utf8mb4;

create index ix_userid
    on orders (userId);
    
    
-- auto-generated definition
create table order_record
(
    id          bigint unsigned auto_increment comment '订单子表id'
        primary key,
    orderId     varchar(64)     default ''   not null comment '订单id',
    userId      bigint unsigned default 0    not null comment '用户id',
    productId   bigint unsigned default 0    not null comment '商品id',
    productName varchar(100)    default ''   not null comment '商品名称',
    productImg  varchar(500)    default ''   not null comment '商品图片地址',
    unitPrice   decimal(20, 2)  default 0.00 not null comment '生成订单时的商品单价,单位是元,保留两位小数',
    buyCount    int(10)         default 0    not null comment '商品数量',
    totalPrice  decimal(20, 2)  default 0.00 not null comment '商品总价,单位是元,保留两位小数',
    createTime  int                          null comment '创建时间',
    updateTime  int                          null comment '更新时间'
)
    comment '订单商品表' charset = utf8mb4;

create index ix_order_id
    on order_record (orderId);

create index ix_product_id
    on order_record (productId);

create index ix_user_id
    on order_record (userId);


-- auto-generated definition
create table order_address
(
    id         bigint unsigned auto_increment comment '收货信息表id'
        primary key,
    orderId    varchar(64)     default '' not null comment '订单id',
    useriId    bigint unsigned default 0  not null comment '用户id',
    name       varchar(20)     default '' not null comment '收货姓名',
    phone      varchar(20)     default '' not null comment '收货固定电话',
    mobile     varchar(20)     default '' not null comment '收货移动电话',
    province   varchar(20)     default '' not null comment '省份',
    city       varchar(20)     default '' not null comment '城市',
    district   varchar(20)     default '' not null comment '区/县',
    address    varchar(200)    default '' not null comment '详细地址',
    createTime int                        null comment '创建时间',
    updateTime int                        null comment '更新时间'
)
    comment '收货信息表' charset = utf8mb4;

create index ix_order_id
    on order_address (orderId);

create index ix_userid
    on order_address (useriId);

支付信息表:

-- auto-generated definition
create table pay_info
(
    id           bigint unsigned auto_increment comment '支付信息表id'
        primary key,
    orderId      varchar(64)     default '' not null comment '订单id',
    userId       bigint unsigned default 0  not null comment '用户id',
    payType      tinyint         default 0  not null comment '支付平台:1-支付宝,2-微信',
    serialNumber varchar(200)    default '' not null comment '支付流水号',
    serialStatus varchar(20)     default '' not null comment '支付状态',
    createTime   int                        null comment '创建时间',
    updateTime   int                        null comment '更新时间'
)
    comment '支付信息表' charset = utf8mb4;

create index ix_order_id
    on pay_info (orderId);

create index ix_user_id
    on pay_info (userId);

3. 结束语

本篇文章介绍定义了整个项目主要涉及的库和表,我们采用了微服务的架构,服务间数据做了隔离,每个服务独享了数据库。

这里只说了 用户user 表、商品product表、分类category

后续的功能表结构,在后续功能中会贴出,敬请期待。

下一篇内容预览:

  • 如何定义API,并根据定义好的api文件通过 goctl 生成服务代码
  • 如何在BFF服务中调用RPC服务,把整个调用链路打通

另外,如果你感兴趣,非常欢迎你加入,我们一起来完成这个项目,为社区献出自己的一份力。

希望本篇文章对你有所帮助,谢谢。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码一行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值