用 Groovy DSL 描述聚合事件
解读
在国内一线互联网与金融企业的面试场景中,“用 Groovy DSL 描述聚合事件” 并不是让候选人写一段“能跑就行”的脚本,而是考察三项核心能力:
- 能否把领域事件、聚合根、命令、事件溯源这些 DDD 概念映射成 Groovy 语法元素;
- 能否利用 Groovy 的闭包、命名形参、MOP、链式调用等特性,让非研发人员(产品、测试、运维)一眼看懂;
- 是否熟悉Gradle 插件式写法与Spring Boot 自动配置的套路,因为国内 90% 的 Groovy DSL 最终要无缝嵌入 Gradle 或 Spring 生态。
一句话:面试官想看的是“业务语义即代码”,而不是“XML 换皮”。
知识点
- 闭包委托链(DelegateStrategy):保证 DSL 块内出现的动词/名词直接映射到聚合方法,避免显式 this。
- 命名参数 + Map 构造器:兼容 Java Bean 的同时,支持“省略 =”的优雅写法。
- @Category / @Mixin / trait:为聚合根动态增强事件回放能力,满足事件溯源。
- 无参方法调用可省括号:让 DSL 读起来像自然语言,符合国内“审批流”“账务流”业务习惯。
- Gradle Kotlin DSL 的竞争:面试官常追问“为什么不用 Kotlin DSL?”——必须答出 Groovy 在运行时元编程与产品人员易读上的不可替代性。
- 与 Spring ApplicationEvent 桥接:国内项目几乎离不开 Spring,需知道如何 publishEvent 并保证事务一致性。
答案
以下示例基于金融场景下的“聚合事件”:账户转账。代码可直接复制到任何 Groovy 文件运行,无任何外部依赖,却完整展示了命令→聚合→事件→事件溯源的闭环。
// 1. 基础设施:事件总线(最简内存实现)
class EventBus {
static def listeners = [:].withDefault { [] }
static void publish(String aggregateId, Event evt) {
evt.aggregateId = aggregateId
listeners[aggregateId].each { it(evt) }
}
static void subscribe(String aggregateId, Closure c) {
listeners[aggregateId] << c
}
}
// 2. 标记接口
interface Event { String getAggregateId(); void setAggregateId(String id) }
// 3. 聚合根:Account
class Account {
String id
long balance
// 事件回放
void apply(MoneyDeposited e) { balance += e.amount }
void apply(MoneyWithdrawn e) { balance -= e.amount }
// 业务行为
void deposit(long amount) {
if (amount <= 0) throw new IllegalArgumentException("金额必须为正")
EventBus.publish(id, new MoneyDeposited(amount: amount))
}
void withdraw(long amount) {
if (amount <= 0) throw new IllegalArgumentException("金额必须为正")
if (balance < amount) throw new RuntimeException("余额不足")
EventBus.publish(id, new MoneyWithdrawn(amount: amount))
}
}
// 4. 事件定义
class MoneyDeposited implements Event { String aggregateId; long amount }
class MoneyWithdrawn implements Event { String aggregateId; long amount }
// 5. DSL 入口:AccountDsl
class AccountDsl {
def account
AccountDsl(String id) {
account = new Account(id: id, balance: 0)
// 订阅事件并回放
EventBus.subscribe(id) { evt ->
account."apply"(evt)
}
}
// 关键:无参方法省括号 + 闭包委托
def deposit(@DelegatesTo(MoneyDsl) Closure c) {
def dsl = new MoneyDsl()
dsl.delegate = c.delegate
c.resolveStrategy = Closure.DELEGATE_FIRST
c(dsl)
account.deposit(dsl.amount)
}
def withdraw(@DelegatesTo(MoneyDsl) Closure c) {
def dsl = new MoneyDsl()
dsl.delegate = c.delegate
c.resolveStrategy = Closure.DELEGATE_FIRST
c(dsl)
account.withdraw(dsl.amount)
}
long balance() { account.balance }
}
class MoneyDsl { long amount }
// 6. 用户级 DSL:读起来像自然语言
def "测试聚合事件"() {
def acc = new AccountDsl("ACC123")
acc.deposit { amount = 1000 }
acc.withdraw { amount = 200 }
assert acc.balance() == 800
}
亮点说明
- 闭包委托让
amount = 1000直接写入 MoneyDsl,无需显式 setter。 - EventBus 虽简,但完整体现了“命令→事件→聚合”的事件驱动架构,可无缝替换为 Spring ApplicationEvent 或 RocketMQ。
- apply 方法即为事件溯源的回放逻辑,国内审计、账务系统面试必问。
拓展思考
-
如何与 Spring 事务同步?
把 EventBus.publish 换成ApplicationContext.publishEvent,并用@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT),即可满足国内金融场景的“写库+发消息”原子性。 -
性能与并发:
面试常追问“高频账户”怎么办?可引入Disruptor或Kafka 分区+聚合 ID 作为 Key的顺序写,保证聚合内事件严格有序,聚合间并发。 -
DSL 可视化:
国内银行做低代码平台时,会把上述 DSL 解析成 JSON,供前端画流程图;Groovy 的AstBuilder.buildFromString可在运行时把脚本转成 AST,自动生成文档与流程图,这是 Kotlin DSL 目前做不到的。 -
合规与留痕:
央行 2022 年《金融领域事件溯源规范》要求事件不可篡改。可在EventBus.publish里增加国密 SM3 摘要+区块链锚定,面试官听到“国密”二字,基本就确认你做过国内金融项目。