由于簡單同時又強大的Mock特性,dgate在我的項目中除了作為簡單的API網(wǎng)關(guān),它也承擔(dān)著面向前端的Mock Server作用,保證前后端開發(fā)同步進(jìn)行。最近,因為項目的需要,順手給它增加了一個新的Mock特性:EventBusBridge。
使用很簡單,DSL的例子如下:
apiGateway {
port = 7001
host = 'localhost'
urls { ... }
eventBusBridge {
urlPattern ='/eventbus/*'
publishers {
'target_address' {
expected = {
[timestamp: Instant.now()]
}
timer = 1000
}
}
consumers {
'consumer_address' {
target = "target_address"
expected = [test: true] // 或者 {message -> ...}
}
}
}
}
語法很簡單,與Mock HTTP幾乎一致。其中:
- publishers,對應(yīng)后端主動發(fā)起的推送,對于每一個推送地址,timer必填,單位為毫秒。
- consumers,對應(yīng)后端接收前端消息的消費者。
- 若target不寫,則對應(yīng)的模式為:message.reply
- 若給出target,則對應(yīng)eventbus.publish
對于expected,它既可以為一個固定的值,也可以為一個閉包。當(dāng)為閉包時,其返回值為mock結(jié)果。同時,對于consumers中的expected,閉包的入?yún)閑vent message。
相關(guān)鏈接: