本章以實時OALP引擎Clickhouse(簡稱ck)為例, 以其面向場景, 架構設計, 細節實現等方面來介紹, 深度瞭解其如何成為了OLAP引擎中的性能之王. ...
領域驅動設計之銀行轉賬:Wow框架實戰
銀行賬戶轉賬案例是一個經典的領域驅動設計(DDD)應用場景。接下來我們通過一個簡單的銀行賬戶轉賬案例,來瞭解如何使用 Wow 進行領域驅動設計以及服務開發。
銀行轉賬流程
- 準備轉賬(Prepare): 用戶發起轉賬請求,觸發 Prepare 步驟。這個步驟會向源賬戶發送準備轉賬的請求。
- 校驗餘額(CheckBalance): 源賬戶在收到準備轉賬請求後,會執行校驗餘額的操作,確保賬戶有足夠的餘額進行轉賬。
- 鎖定金額(LockAmount): 如果餘額足夠,源賬戶會鎖定轉賬金額,防止其他操作干擾。
- 入賬(Entry): 接著,轉賬流程進入到目標賬戶,執行入賬操作。
- 確認轉賬(Confirm): 如果入賬成功,確認轉賬;否則,執行解鎖金額操作。
- 成功路徑(Success): 如果一切順利,完成轉賬流程。
- 失敗路徑(Fail): 如果入賬失敗,執行解鎖金額操作,並處理失敗情況。
運行案例
- 運行 TransferExampleServer.java
- 查看 Swagger-UI : http://localhost:8080/swagger-ui.html
- 執行 API 測試:Transfer.http
自動生成 API 端點
運行之後,訪問 Swagger-UI : http://localhost:8080/swagger-ui.html 。
該 RESTful API 端點是由 Wow 自動生成的,無需手動編寫。
模塊劃分
模塊 | 說明 |
---|---|
example-transfer-api | API 層,定義聚合命令(Command)、領域事件(Domain Event)以及查詢視圖模型(Query View Model),這個模塊充當了各個模塊之間通信的“發佈語言”。 |
example-transfer-domain | 領域層,包含聚合根和業務約束的實現。聚合根:領域模型的入口點,負責協調領域對象的操作。業務約束:包括驗證規則、領域事件的處理等。 |
example-transfer-server | 宿主服務,應用程式的啟動點。負責整合其他模塊,並提供應用程式的入口。涉及配置依賴項、連接資料庫、啟動 API 服務 |
領域建模
狀態聚合根(AccountState
)與命令聚合根(Account
)分離設計保證了在執行命令過程中,不會修改狀態聚合根的狀態。
狀態聚合根(AccountState
)建模
public class AccountState implements Identifier {
private final String id;
private String name;
/**
* 餘額
*/
private long balanceAmount = 0L;
/**
* 已鎖定金額
*/
private long lockedAmount = 0L;
/**
* 賬號已凍結標記
*/
private boolean frozen = false;
@JsonCreator
public AccountState(@JsonProperty("id") String id) {
this.id = id;
}
@NotNull
@Override
public String getId() {
return id;
}
public String getName() {
return name;
}
public long getBalanceAmount() {
return balanceAmount;
}
public long getLockedAmount() {
return lockedAmount;
}
public boolean isFrozen() {
return frozen;
}
void onSourcing(AccountCreated accountCreated) {
this.name = accountCreated.name();
this.balanceAmount = accountCreated.balance();
}
void onSourcing(AmountLocked amountLocked) {
balanceAmount = balanceAmount - amountLocked.amount();
lockedAmount = lockedAmount + amountLocked.amount();
}
void onSourcing(AmountEntered amountEntered) {
balanceAmount = balanceAmount + amountEntered.amount();
}
void onSourcing(Confirmed confirmed) {
lockedAmount = lockedAmount - confirmed.amount();
}
void onSourcing(AmountUnlocked amountUnlocked) {
lockedAmount = lockedAmount - amountUnlocked.amount();
balanceAmount = balanceAmount + amountUnlocked.amount();
}
void onSourcing(AccountFrozen accountFrozen) {
this.frozen = true;
}
}
命令聚合根(Account
)建模
@StaticTenantId
@AggregateRoot
public class Account {
private final AccountState state;
public Account(AccountState state) {
this.state = state;
}
AccountCreated onCommand(CreateAccount createAccount) {
return new AccountCreated(createAccount.name(), createAccount.balance());
}
@OnCommand(returns = {AmountLocked.class, Prepared.class})
List<?> onCommand(Prepare prepare) {
checkBalance(prepare.amount());
return List.of(new AmountLocked(prepare.amount()), new Prepared(prepare.to(), prepare.amount()));
}
private void checkBalance(long amount) {
if (state.isFrozen()) {
throw new IllegalStateException("賬號已凍結無法轉賬.");
}
if (state.getBalanceAmount() < amount) {
throw new IllegalStateException("賬號餘額不足.");
}
}
Object onCommand(Entry entry) {
if (state.isFrozen()) {
return new EntryFailed(entry.sourceId(), entry.amount());
}
return new AmountEntered(entry.sourceId(), entry.amount());
}
Confirmed onCommand(Confirm confirm) {
return new Confirmed(confirm.amount());
}
AmountUnlocked onCommand(UnlockAmount unlockAmount) {
return new AmountUnlocked(unlockAmount.amount());
}
AccountFrozen onCommand(FreezeAccount freezeAccount) {
return new AccountFrozen(freezeAccount.reason());
}
}
轉賬流程管理器(TransferSaga
)
轉賬流程管理器(TransferSaga
)負責協調處理轉賬的事件,並生成相應的命令。
onEvent(Prepared)
: 訂閱轉賬已準備就緒事件(Prepared
),並生成入賬命令(Entry
)。onEvent(AmountEntered)
: 訂閱轉賬已入賬事件(AmountEntered
),並生成確認轉賬命令(Confirm
)。onEvent(EntryFailed)
: 訂閱轉賬入賬失敗事件(EntryFailed
),並生成解鎖金額命令(UnlockAmount
)。
@StatelessSaga
public class TransferSaga {
Entry onEvent(Prepared prepared, AggregateId aggregateId) {
return new Entry(prepared.to(), aggregateId.getId(), prepared.amount());
}
Confirm onEvent(AmountEntered amountEntered) {
return new Confirm(amountEntered.sourceId(), amountEntered.amount());
}
UnlockAmount onEvent(EntryFailed entryFailed) {
return new UnlockAmount(entryFailed.sourceId(), entryFailed.amount());
}
}
單元測試
藉助 Wow 單元測試套件,可以輕鬆的編寫聚合根和 Saga 的單元測試。從而提升代碼覆蓋率,保證代碼質量。
使用
aggregateVerifier
進行聚合根單元測試,可以有效的減少單元測試的編寫工作量。
Account
聚合根單元測試
internal class AccountKTest {
@Test
fun createAccount() {
aggregateVerifier<Account, AccountState>()
.given()
.`when`(CreateAccount("name", 100))
.expectEventType(AccountCreated::class.java)
.expectState {
assertThat(it.name, equalTo("name"))
assertThat(it.balanceAmount, equalTo(100))
}
.verify()
}
@Test
fun prepare() {
aggregateVerifier<Account, AccountState>()
.given(AccountCreated("name", 100))
.`when`(Prepare("name", 100))
.expectEventType(AmountLocked::class.java, Prepared::class.java)
.expectState {
assertThat(it.name, equalTo("name"))
assertThat(it.balanceAmount, equalTo(0))
}
.verify()
}
@Test
fun entry() {
val aggregateId = GlobalIdGenerator.generateAsString()
aggregateVerifier<Account, AccountState>(aggregateId)
.given(AccountCreated("name", 100))
.`when`(Entry(aggregateId, "sourceId", 100))
.expectEventType(AmountEntered::class.java)
.expectState {
assertThat(it.name, equalTo("name"))
assertThat(it.balanceAmount, equalTo(200))
}
.verify()
}
@Test
fun entryGivenFrozen() {
val aggregateId = GlobalIdGenerator.generateAsString()
aggregateVerifier<Account, AccountState>(aggregateId)
.given(AccountCreated("name", 100), AccountFrozen(""))
.`when`(Entry(aggregateId, "sourceId", 100))
.expectEventType(EntryFailed::class.java)
.expectState {
assertThat(it.name, equalTo("name"))
assertThat(it.balanceAmount, equalTo(100))
assertThat(it.isFrozen, equalTo(true))
}
.verify()
}
@Test
fun confirm() {
val aggregateId = GlobalIdGenerator.generateAsString()
aggregateVerifier<Account, AccountState>(aggregateId)
.given(AccountCreated("name", 100), AmountLocked(100))
.`when`(Confirm(aggregateId, 100))
.expectEventType(Confirmed::class.java)
.expectState {
assertThat(it.name, equalTo("name"))
assertThat(it.balanceAmount, equalTo(0))
assertThat(it.lockedAmount, equalTo(0))
assertThat(it.isFrozen, equalTo(false))
}
.verify()
}
@Test
fun unlockAmount() {
val aggregateId = GlobalIdGenerator.generateAsString()
aggregateVerifier<Account, AccountState>(aggregateId)
.given(AccountCreated("name", 100), AmountLocked(100))
.`when`(UnlockAmount(aggregateId, 100))
.expectEventType(AmountUnlocked::class.java)
.expectState {
assertThat(it.name, equalTo("name"))
assertThat(it.balanceAmount, equalTo(100))
assertThat(it.lockedAmount, equalTo(0))
assertThat(it.isFrozen, equalTo(false))
}
.verify()
}
@Test
fun freezeAccount() {
val aggregateId = GlobalIdGenerator.generateAsString()
aggregateVerifier<Account, AccountState>(aggregateId)
.given(AccountCreated("name", 100))
.`when`(FreezeAccount(""))
.expectEventType(AccountFrozen::class.java)
.expectState {
assertThat(it.name, equalTo("name"))
assertThat(it.balanceAmount, equalTo(100))
assertThat(it.lockedAmount, equalTo(0))
assertThat(it.isFrozen, equalTo(true))
}
.verify()
}
}
使用
sagaVerifier
進行 Saga 單元測試,可以有效的減少單元測試的編寫工作量。
TransferSaga
單元測試
internal class TransferSagaTest {
@Test
fun onPrepared() {
val event = Prepared("to", 1)
sagaVerifier<TransferSaga>()
.`when`(event)
.expectCommandBody<Entry> {
assertThat(it.id, equalTo(event.to))
assertThat(it.amount, equalTo(event.amount))
}
.verify()
}
@Test
fun onAmountEntered() {
val event = AmountEntered("sourceId", 1)
sagaVerifier<TransferSaga>()
.`when`(event)
.expectCommandBody<Confirm> {
assertThat(it.id, equalTo(event.sourceId))
assertThat(it.amount, equalTo(event.amount))
}
.verify()
}
@Test
fun onEntryFailed() {
val event = EntryFailed("sourceId", 1)
sagaVerifier<TransferSaga>()
.`when`(event)
.expectCommandBody<UnlockAmount> {
assertThat(it.id, equalTo(event.sourceId))
assertThat(it.amount, equalTo(event.amount))
}
.verify()
}
}
作者:Ahoo Wang (阿虎)
Github: https://github.com/Ahoo-Wang/
SmartSql(高性能、高生產力,超輕量級的ORM!): https://github.com/Ahoo-Wang/SmartSql
SmartCode(不只是代碼生成器!): https://github.com/Ahoo-Wang/SmartCode
CoSky 高性能、低成本微服務治理平臺 : https://github.com/Ahoo-Wang/CoSky
CosId 通用、靈活、高性能的分散式 ID 生成器 : https://github.com/Ahoo-Wang/CosId
Wow 基於 DDD、EventSourcing 的現代響應式 CQRS 架構微服務開發框架: https://github.com/Ahoo-Wang/Wow
CoSec 基於 RBAC 和策略的多租戶響應式安全框架: https://github.com/Ahoo-Wang/CoSec
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。