Saga的实现模式——进化(Saga implementation patterns – variations)
在之前的几个博客中,我主要讲了两个saga的实现模式:
- 基于command的控制者模式
- 基于事件的观察者模式
当然,这些都不是实现saga的唯一方式。我们甚至可以将这些结合起来。
发布者——收集者
回顾我们的麦当劳的例子,我们可以稍稍改进一下我们的方案。我们可以通过一个command去启动一个saga,然后这个saga就会发布消息。然后saga就等待回复事件(忽略顺序):
![]()
这里有一个优点,那就是我们的saga只有一个入口。因此我们不必担心我们的saga被其他消息触发。
当你在麦当劳下一个订单的时候,总是有一个服务员把托盘放到柜台上。当工作台完成了薯条,三明治等等食物的制作,他们从来不到柜台看看:“我是否还需要一个新的托盘?”因为托盘总是已经被准备好在那里了,就像我们的saga在业务开始之前总是已经开始了的。
在这里没有任何阻止产生额外saga的方法——但是其实是有的。
报告者
saga可以扮演另一个不做任何决定而仅仅报告状态的角色:
![]()
This is a situation where a saga might never actually complete, and goes on forever. Its role is to communicate status of a longer-running process in the back-end, not for coordination purposes, but for reporting purposes.
The reason we might want a saga for this case is we still don’t know what order we’ve received messages from downstream systems we don’t own. As we learn about downstream events, we can evaluate them based on our knowledge of the overall business process. An order can’t go backwards from “shipped” to “verified”, so receiving these out of order doesn’t change the fact that the order shipped! Note: this doesn’t necessarily imply the status is in the saga entity itself. It still could be separate.
Keeping it as a saga lets us handle messages in any order and keep some centralized logic around interpreting these messages.
Sagas/process managers are pretty flexible in how we compose the pieces together. I often get questions around “why can’t I design a saga like a workflow?” And the answer is that sagas are meant to handle cases where I don’t have a directed workflow, where I live in a world where messages can arrive out of order.
It’s a much easier world to scale – but we need to accept the complexity it brings on the other end.
There is another clear downside here – we have shared state amongst multiple messages and handlers, which can potentially lead to scaling problems. Next time, we’ll look at scaling sagas.
有一种saga可能永远无法完成并且会永远运行下去。它的职责是和后台运行的长时间运行的事务保持通讯,主要为了报告状态而不是为了协调业务。
我们可能需要这种saga的原因是我们仍然不知道从不属于我们的上游系统中得到消息的顺序。当我们处理上游的事件的时候,我们可以通过我们对整个业务过程的理解来判断。就好比一个订单不可能从“已发货”变成“已审核”,所以尽管收到的事件的顺序不一样也不可能改变货物已经发货的事实。注意:这并不意味着状态必须被包含在saga中,它也是可以被分出去的。
saga让我们能够忽略事件到来的顺序,并且能够通过一定的逻辑解读这些消息。
sagas/process manager能够让我们更灵活地把这些消息组织起来。我经常被人问道:“为什么我们不能把saga设计成一个工作流呢?”答案就是saga是用来处理一些不能控制的工作流情况的,因为在现实世界里消息的到来可能会不按照顺序。
Saga的实现模式——进化(Saga implementation patterns – variations)的更多相关文章
- Saga的实现模式——观察者(Saga implementation patterns – Observer)
https://lostechies.com/jimmybogard/2013/03/11/saga-implementation-patterns-observer/ 侵删. NServiceBus ...
- Saga的实现模式——控制者(Saga implementation patterns – Controller)
https://lostechies.com/jimmybogard/2013/03/14/saga-implementation-patterns-controller/ 之前的文章中我们介绍了观察 ...
- 《Language Implementation Patterns》之 增强解析模式
上一章节讲述了基本的语言解析模式,LL(k)足以应付大多数的任务,但是对一些复杂的语言仍然显得不足,已付出更多的复杂度.和运行时效率为代价,我们可以得到能力更强的Parser. Pattern 5 : ...
- 局部二值模式(Local Binary Patterns)纹理灰度与旋转不变性
Multiresolution Gray Scale and Rotation Invariant Texture Classification with Local Binary Patterns, ...
- .Net Core自实现CLR异步编程模式(Asynchronous programming patterns)
最近在看一个线程框架,对.Net的异步编程模型很感兴趣,所以在这里实现CLR定义的异步编程模型,在CLR里有三种异步模式如下,如果不了解的可以详细看MSDN 文档Asynchronous progra ...
- Javascript编程模式(JavaScript Programming Patterns)Part 2.(高级篇)
模块编程模式的启示(Revealing Module Pattern) 客户端对象(Custom Objects) 懒函数定义(Lazy Function Definition) Christian ...
- Javascript编程模式(JavaScript Programming Patterns)Part 1.(初级篇)
JavaScript 为网站添加状态,这些状态可能是校验或者更复杂的行为像拖拽终止功能或者是异步的请求webserver (aka Ajax). 在过去的那些年里, JavaScript librar ...
- 《Language Implementation Patterns》之 解释器
前面讲述了如何验证语句,这章讲述如何构建一个解释器来执行语句,解释器有两种,高级解释器直接执行语句源码或AST这样的中间结构,低级解释器执行执行字节码(更接近机器指令的形式). 高级解释器比较适合DS ...
- 《Language Implementation Patterns》之 强类型规则
语句的语义取决于其语法结构和相关符号:前者说明了了要"做什么",后者说明了操作"什么对象".所以即使语法结构正确的,如果被操作的对象不合法,语句也是不合法的.语 ...
随机推荐
- rpmdb open failed解决方案
1.前提条件:安装软件包的时候,被我手动终止了(可能出错原因)[root@dhcp yum.repos.d]# yum clean allrpmdb: Thread/process 4541/1406 ...
- HDU2157 How many ways??---(邻接矩阵,图论,矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=2157 How many ways?? Time Limit: 2000/1000 MS (Java/Others ...
- HDU1267 下沙的沙子有几粒? 基础DP
题目链接 题意:给定m个H和n个D(1<=n,m<=20),问这些字母构成的序列中,对于任意位置,从左开始数H的累积个数总是不比D的累计数少的排列有多少种. 题解:二维DP,画一个正方形, ...
- HDU 2105 The Center of Gravity (数学)
题目链接 Problem Description Everyone know the story that how Newton discovered the Universal Gravitatio ...
- 求LCA最近公共祖先的在线倍增算法模板_C++
倍增求 LCA 是在线的,而且比 ST 好写多了,理解起来比 ST 和 Tarjan 都容易,于是就自行脑补吧,代码写得容易看懂 关键理解 f[i][j] 表示 i 号节点的第 2j 个父亲,也就是往 ...
- Jasper语音助理
1. 介绍 Jasper是一款基于树莓派的开源语音控制助理, 使用Python语言开发. Jasper工作原理主要是设备被动监听麦克风, 当收到唤醒关键字时进入主动监听模式, 此时收到语音指令后进行语 ...
- Linux下的Backlight子系统(二)【转】
转自:http://blog.csdn.net/weiqing1981127/article/details/8515847 版权所有,转载必须说明转自 http://my.csdn.net/weiq ...
- JS 判断某变量是否为某数组中的一个值 的几种方法
1.正则表达式 js 中判断某个元素是否存在于某个 js 数组中,相当于 PHP 语言中的 in_array 函数. }; 用法如下: var arr=new Array([‘b’,2,‘a‘,4]) ...
- boost::function的简单实现
前言 boost::function和boost:bind是一对强大的利器.相信用过的童鞋多少有些体会. 虽然平时在用boost::function,但是用的时候心中总会一些不安,因为不知道它是怎么实 ...
- php编译中遇到种种error解决办法
http://my.oschina.net/maczhao/blog/365176 编译PHP5.5 make 时出现错误 make: *** [ext/fileinfo/libmagic/appre ...