問題描述
我正在研究 DDD 和事件源中的編程.
我看到一個(gè)示例,當(dāng)調(diào)用域邏輯時(shí)(例如 Order.placeOrder()
),它會(huì)發(fā)布一個(gè)事件(例如 OrderPlaced
).并且該事件將作為事件存儲(chǔ)發(fā)送到 MQ.
領(lǐng)域邏輯(Order.placeOrder()
)應(yīng)該是一個(gè)原子API,如果使用Spring作為事務(wù)管理器,它應(yīng)該有@Transactional
注解.p>
現(xiàn)在我的問題是:
如何確保數(shù)據(jù)庫更改和事件發(fā)送在同一個(gè)事務(wù)中?即,如果在將數(shù)據(jù)提交到 DB 時(shí)出現(xiàn)任何錯(cuò)誤,則該事件不應(yīng)該發(fā)送到 MQ.
我知道有像 XA 或 2 階段提交這樣的解決方案來強(qiáng)制數(shù)據(jù)庫更新和在同一事務(wù)中發(fā)送 MQ 消息.但現(xiàn)在似乎沒有被廣泛使用.
如果還是使用Spring的
@Transactional
注解,沒有XA,是不是我們可以在事務(wù)提交成功后做一些邏輯呢?這樣做的最佳做法是什么?
以下兩個(gè)屬性必須具備才能擁有一個(gè)可靠的系統(tǒng):
- P1:已發(fā)布的域事件必須描述真正發(fā)生的變化(即確保沒有鬼事件開始四處飛舞).
- P2:對(duì)觸發(fā)域事件的數(shù)據(jù)庫進(jìn)行更改必須導(dǎo)致事件被發(fā)布(即不會(huì)丟失事件).
有以下幾種可能來實(shí)現(xiàn)這一點(diǎn),所有這些都是我自己使用過的,或者在項(xiàng)目中看到過的:
使用與您的應(yīng)用程序使用相同數(shù)據(jù)庫的消息傳遞基礎(chǔ)架構(gòu),以便可以使用單個(gè)事務(wù).當(dāng)一個(gè)非常簡單的消息傳遞基礎(chǔ)架構(gòu)就足夠了,并且團(tuán)隊(duì)決定自己構(gòu)建它時(shí),此解決方案是可行的.
使用 2 階段提交.我沒有這個(gè)不再使用的印象,但可能它很少被談?wù)摚驗(yàn)樗皇腔ㄉ诘募夹g(shù)......
使用一些巧妙的技巧來確保這兩個(gè)條件都成立.例如.用我所說的雞和蛋的解決方案:
- 始終先同步發(fā)布事件,然后持久化到數(shù)據(jù)庫.這確保了 P2 成立.
- 然后使用事件處理器檢查事件流并檢查是否可以在數(shù)據(jù)庫中找到事件.如果沒有,請(qǐng)從流中刪除該事件.這可確保 P1 成立.
解決方案 3 需要仔細(xì)設(shè)計(jì)和審查系統(tǒng)每個(gè)部分在故障行為方面所做的保證,因此它可能是最難做到的.但它也是一個(gè)非常優(yōu)雅的解決方案,一旦它起作用.
順便說一句,我不同意應(yīng)將 Spring 注釋添加到域?qū)ο笾校菓?yīng)添加到相應(yīng)的應(yīng)用程序服務(wù)中.這只是一個(gè)旁注.
I am studying on the programming in DDD and event source.
I saw one example that when a domain logic was called (e.g. Order.placeOrder()
) it would publish an event (e.g. OrderPlaced
). And the event would be sent to MQ as the event store.
The domain logic (Order.placeOrder()
) should be an atomic API, and it should have @Transactional
annotation if using Spring for the transaction manager.
And now my question is:
How to make sure the DB change and event sending are within the same transaction? i.e. If there any error when committing data into DB, the event should never send to MQ.
I know that there is solution like XA or 2 phase commit to force the DB update and sending MQ messages within the same transaction. But seems it is not widely used nowadays.
If still using Spring
@Transactional
annotation and no XA, is it possible that we do some logic after the transaction is committed successfully? What is the best practice to do that?
The following two properties must hold to have a reliable system:
- P1: Published domain events MUST describe a change that really happened (i.e. make sure no ghost events start flying around).
- P2: Changes to the DB that trigger domain events MUST result in an event being published (i.e. don't lose events).
There are the following possibilities to achieve this, all of which I've either used myself or seen being used in a project:
Use a messaging infrastructure that uses the same database as your application, so that a single transaction can be used. This solution is viable when a very simple messaging infrastructure suffices, and the team decides to build it themselves.
Use 2 phase commits. I don't have the impression that this is not used anymore, but maybe it's less talked about, because it isn't fancy technology...
Use some clever trickery to ensure both conditions hold. E.g. with what I call the chicken and egg solution:
- Always publish events synchronously first, then persist to the DB. This ensures P2 holds.
- Then use an event processor that inspects the event stream and checks whether an event can be found in the DB. If not, remove the event from the stream. This ensures P1 holds.
Solution 3 requires careful design and review of the guarantees each part of the system makes in terms of failure behavior, so it is probably the most difficult one to get right. But it is also a very elegant solution, once it works.
By the way, I don't agree that Spring annotations should be added to domain objects, but rather to the respective app services. This only as a side note.
這篇關(guān)于如何在 DDD 中管理域邏輯和事件之間的事務(wù)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!