1

dgate新特性:EventBusBridge Mock

 1 year ago
source link: https://blog.dteam.top/posts/2017-12/dgate%E6%96%B0%E7%89%B9%E6%80%A7eventbusbridge-mock.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

dgate新特性:EventBusBridge Mock

胡键 Posted at — Dec 15, 2017 阅读 98

由于简单同时又强大的 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。


相关链接:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK