dgate新特性:EventBusBridge Mock

作者: 胡键 | 来源:发表于2017-12-15 16:17 被阅读134次

由于简单同时又强大的Mock特性,dgate在我的项目中除了作为简单的API网关,它也承担着面向前端的Mock Server作用,保证前后端开发同步进行。最近,因为项目的需要,顺手给它增加了一个新的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,对应后端主动发起的推送,对于每一个推送地址,timer必填,单位为毫秒。
  • consumers,对应后端接收前端消息的消费者。
    • 若target不写,则对应的模式为:message.reply
    • 若给出target,则对应eventbus.publish

对于expected,它既可以为一个固定的值,也可以为一个闭包。当为闭包时,其返回值为mock结果。同时,对于consumers中的expected,闭包的入参为event message。


相关链接:

相关文章

网友评论

    本文标题:dgate新特性:EventBusBridge Mock

    本文链接:https://www.haomeiwen.com/subject/knjdwxtx.html