- 订单重复下单问题
- insert前查一下
- Version
更新数据时需要把版本号座位请求更新的参数带回给订单更新服务
update order set number = 666 ,version = version+1 wherer version=#{}?
- 读写分离 & 分库分表
用多个MySql实例承担大量读写请求。 读写比例一般在9:1左右
引出的问题 --- 数据不一致
如果需要立即查询到业务
- 更新和查询的步骤需要放到一个事务中,同一个事务查询会被路由到主库中,避免了主从不一致的问题.
- 查询的步骤指定主库前缀,强制走主库.
如何规划分库分表
- 数据量太大导致的慢查询
- 应对高并发
预估数据量提前拆分库和表
分片键
从什么维度查询,比如用户想看我的订单,从用户的uid维度查询
- 引出一个问题.
商家想查询订单时该怎么做?
比如开了很多家分店,想看每家分店的数据。
怎么办? 数据同步到其他系统中,构建一个以店铺ID为分片键的只读库.
常见分片方式
时间范围
哈希分片
查表发
读写分离实现的三种方式
- 纯手工指定。 定义多个DS,需要的时候指定数据源
- ShardiingJDBC指定
- 代理层Sharding-Proxy MyCat等
原理: 将自己伪装成一个MySql实例节点,代理请求,然后发送给数据库实例
Sharding-JDBC 配置
spring:
shardingsphere:
datasource:
names: ds-master
ds-master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.65.223:3306/tl_mall_order?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true
initialSize: 5
minIdle: 10
maxActive: 30
validationQuery: SELECT 1 FROM DUAL
username: tlmall
password: tlmall123
rules:
sharding:
tables:
oms_order:
actual-data-nodes: ds-master.oms_order_$->{0..31}
table-strategy:
complex:
--- 分片键是id和member_id
sharding-columns: id,member_id
sharding-algorithm-name: oms_order_table_alg
oms_order_item:
actual-data-nodes: ds-master.oms_order_item_$->{0..31}
table-strategy:
complex:
sharding-columns: order_id
sharding-algorithm-name: oms_order_item_table_alg
--- 分库分表算法
sharding-algorithms:
oms_order_table_alg:
type: CLASS_BASED
props:
algorithmClassName: com.tuling.tulingmall.ordercurr.sharding.OmsOrderShardingAlgorithm
strategy: COMPLEX
oms_order_item_table_alg:
type: CLASS_BASED
props:
algorithmClassName: com.tuling.tulingmall.ordercurr.sharding.OmsOrderItemShardingAlgorithm
strategy: COMPLEX
binding-tables:
- oms_order,oms_order_item
--- 广播表
broadcast-tables:
- oms_company_address
- oms_order_operate_history
- oms_order_return_apply
- oms_order_return_reason
- oms_order_setting
props:
sql-show: true
分布式事务模型,一致性算法扩展
2pc 3pc 本地消息表 消息最终一致性 tcc xa mq事务消息 尽最大努力通知
数据一致性算法
Paxos,Quorrum 机制,ZAB一致性协议 Zxid事务年代m 奔溃恢复,原子广播,Raft算法,Gossip协议种子传播