申请试用
HOT
登录
注册
 
《Saga分布式事务解决方案与实践》-姜宁
林中仙
/
发布于
/
2399
人观看
  • 微服务事务⼀一致性问题?
  • 业界Saga的解决⽅方案
  • ServiceComb Saga的演进
  • 后续的开发计划
展开查看详情

1 .《Saga分布式事务解决⽅方案与实践》 演讲者/姜宁

2 . 关于我 • 华为开源能⼒力力中⼼心 • ServiceComb项⽬目负责⼈人 • Apache Member , IPMC, 多个Apache项⽬目 • RedHat, IONA, Travelsky

3 . 议题 • 微服务事务⼀一致性问题? • 业界Saga的解决⽅方案 • ServiceComb Saga的演进 • 后续的开发计划

4 . 微服务架构 • 微服务架构将⼀一个应⽤用分成多个相互独⽴立的服务。 • 好处是各个服务能够持续独⽴立的开发和部署。 • 难题是服务的数据需要采⽤用什什么样的⽅方式来进⾏行行存储?

5 .多个微服务使⽤用同⼀一数据库 A B C commit rollback

6 . ⼀一个典型的微服务架构应⽤用 HTML Customer Browser Front UI Service Customer Database REST Order EdgeService Service REST Order Database Mobile Device Inventory Service Inventory Database http://microservices.io/patterns/data/database-per-service.html http://blog.christianposta.com/microservices/the-hardest-part-about-microservices-data/

7 . 两阶段提交 2PC Transaction RM 1 RM 2 • 提供强⼀一致保障 Manager prepare() • 准备阶段完成资源操作 OK prepare() • 如果准备过程中出现问题,可以回滚 OK • 提交阶段不不允许出错 commit() OK • 资源层⾯面提供保障业务侵⼊入性低 commit() OK • 协议成本⾼高,并且存在全局锁的问题

8 . ACID 与 BASE • ACID (刚性事务) • BASE (柔性事务) • 原⼦子性 (Atomicity) • 基本可⽤用(Basically Available) • ⼀一致性 (Consistency) • 柔性状态 (Soft state) • 隔离性 ( Isolation) • 最终⼀一致性 (Eventually Consistent) • 持久性 (Durability) https://queue.acm.org/detail.cfm?id=1394128

9 . TCC • Try: 尝试执⾏行行业务 try confirm cancel • 完成所有业务检查,预留留必须的业务资源 • Confirm:确认执⾏行行业务 • 真正执⾏行行业务,不不做业务检查 业务服务 • Cancel:取消执⾏行行业务 • 释放Try阶段预留留的业务资源

10 .通过领域建模来解决

11 .微服务事务⼀一致性建议 • 内刚 • 微服务内:聚合通过数据库事务保证强⼀一致 • 外柔 • 微服务间:最终⼀一致

12 . Saga简介 • 1987年年Hector & Kenneth 发表论⽂文 Sagas • Saga = Long Live Transaction (LLT) • LLT = T1 + T2 + T3 + ... + Tn • 每个本地事务Tx 有对应的补偿 Cx T1 T2 T3 ... Tn T1 T2 T3 ... Tn T1 T2 T3 C3 C2 C1 C1 C2 C3 ... Cn 正常情况 异常情况 https://www.cs.cornell.edu/andru/cs711/2002fa/reading/sagas.pdf

13 . 业界Saga的研究应⽤用情况 Caitie McCaffrey Chris Richardson Distributed Sagas Microservice saga pattern https://github.com/aphyr/dist-sagas/blob/master/sagas.pdf http://microservices.io/patterns/data/saga.html

14 . Saga Started request StartSaga Flight Started Flight Ended Saga Hotel Started Hotel Ended Car Started Car Ended Payment Started Payment Ended Saga Ended EndSaga

15 . Saga Started request StartSaga Flight Started Flight Ended Hotel Started Saga Hotel Aborted Flight Compensated Saga Ended Transaction Rollback EndSaga

16 . 对服务的要求 • 幂等 T = T T … T time saga car rental T T'

17 . 对服务的要求 • 幂等 T = T T … T • 可交换补偿 T C = T C T time saga car rental time saga car rental T T T' T' C 保留留所有事务数据!

18 . ACID 与 Saga • ACID • Saga只提供ACD保证 • 原⼦子性 (Atomicity) • 原⼦子性(通过Saga协调器器实现) • ⼀一致性 (Consistency) • ⼀一致性 (本地事务 + Saga log) • 隔离性 ( Isolation) • 隔离性 (Saga不不保证) • 持久性 (Durability) • 持久性 (Saga log)

19 . 缺乏隔离性带来的问题 • 两个Saga事务同时操作⼀一个资源会出现数据语义不不⼀一致的的情况。 • 两个Saga事务同时操作⼀一个订单 ,彼此操作会覆盖对⽅方(更更新丢失) • 两个Saga事务同时访问扣款账号,⽆无法看到退款 (脏读取问题) • 在⼀一个Saga事务内,数据被其他事务修改前后的读取值不不⼀一致(模糊 读取问题) http://microservices.io/microservices/general/2018/03/22/microxchg-sagas.html

20 . 如何应对隔离性问题 • 隔离的本质是控制并发,防⽌止并发事务操作相同资源⽽而引起结果错乱 • 在应⽤用层⾯面加⼊入逻辑锁的逻辑。 • Session层⾯面隔离来保证串串⾏行行化操作。 • 业务层⾯面采⽤用预先冻结资⾦金金的⽅方式隔离此部分资⾦金金。 • 业务操作过程中通过及时读取当前状态的⽅方式获取更更新。

21 . Saga的实现⽅方式 • 集中式的实现⽅方式 (Orchestration-based coordination) • 集中式协调器器负责服务调⽤用以及事务协调 • 分布式的实现⽅方式 (Choreography-based coordination) • 通过事件驱动的⽅方式来进⾏行行事务协调 http://microservices.io/patterns/data/saga.html

22 . 集中式Saga实现 //transaction invocation action Order from("direct:creditReservation") Service Customer Service .saga() .propagation(SagaPropagation.SUPPORTS) OrderService .option("CreditId", body()) // mark the current body as needed in the compensating action .compensation("direct:creditRefund") CustomerService create() .bean(creditService, "reserveCredit") reserveCredit() .log("Credit ${header.amount} reserved. Custom Id used is $ creditLimit {body}"); create() CreateOrder Saga // called only creditReserved() if the saga is cancelled from("direct:creditRefund") creditReservate … option from .transform(header("CreditId")) // retrieve the CreditId headers .bean(creditService, "refundCredit") Order .log("Credit for Custom Id ${body} refunded"); state total … http://microservices.io/patterns/data/saga.html https://github.com/apache/camel/blob/master/camel-core/src/main/docs/eips/saga-eip.adoc

23 . 分布式Saga实现 Order created Create Order Order Credit Reserved Customer Service Service create() OR reserveCredit() Credit Limit Exceeded approve()/reject() Customer Order creditLimit state creditReservations total … … http://microservices.io/patterns/data/saga.html http://www.axonframework.org/docs/2.0/sagas.html

24 . Service Center Java Chassis 让云原⽣生应⽤用开发更更简单 Saga 代码: https://github.com/apache?q=incubator-servicecomb ⽹网站:http://servicecomb.incubator.apache.org/ 华为云: https://www.huaweicloud.com/product/cse.html

25 . ServiceComb Saga演进 1 2 3 Saga Log Service A Saga Started Saga T1 Started Omega Caller Saga Coordinator Transaction Viewer Saga ID: x T1 Ended params Saga { T2 Started T1:[a,b], config Alpha { Service B Alpha C1:[c,d], T1: name, path, Alpha ... C1:name, path, } ... } Omega Dynamic Service Aggregator Config Registry 集中式的Saga协调器器 分布式Saga协调器器

26 . 集中式Saga协调器器 1 2 3 Saga Log Saga Started T1 Started Saga Caller Saga Coordinator Transaction Viewer Saga ID: x T1 Ended params Saga { T2 Started T1:[a,b], config { C1:[c,d], T1: name, path, ... C1:name, path, } ... } Dynamic Service Aggregator Config Registry

27 . 集中式的Saga – 基于图形 Saga Execution Saga Saga Log Component Saga Started TaskRunner T1 Started Caller FromJsonFormat requests { T1 Ended T1:[a,b], C1:[c,d], T2 Started ... GraphBuilder TaskConsumer } request graph events SagaTask SagaRequest Transaction Compensation EventRepo ToJsonFormat Transport Service Registry 1 2 3

28 . 集中式Saga – 基于Actor模型 Saga Execution Saga Saga Log Component Actors Saga Started T1 Started Caller FromJsonFormat requests { T1 Ended T1:[a,b], C1:[c,d], T2 Started ... ActorBuilder } events SagaTask SagaRequest Transaction Compensation EventRepo ToJsonFormat Transport Service Registry 1 2 3

29 . 集中式Saga实现过程中问题 • 好处 • 集中式的服务调⽤用易易于监控和协调 • 坏处 • 通过JSon描述Saga事务执⾏行行灵活性不不⾼高 • 业务描述与代码相分离,需要依赖UI⼯工具的帮助 • 如何解决⾃自动获取Saga事务定义的问题?

0 点赞
0 收藏
1下载
相关文档
确认
3秒后跳转登录页面
去登陆