SpringCloud Stub 禁用功能
2023-12-12 17:52 更新
如果需要禁用此功能,请设置stubrunner.stream.enabled=false
属性。
假设您具有以下Maven存储库,其中包含streamService
应用程序的已部署存根:
└── .m2 └── repository └── io └── codearte └── accurest └── stubs └── streamService ├── 0.0.1-SNAPSHOT │ ├── streamService-0.0.1-SNAPSHOT.pom │ ├── streamService-0.0.1-SNAPSHOT-stubs.jar │ └── maven-metadata-local.xml └── maven-metadata-local.xml
进一步假设存根包含以下结构:
├── META-INF │ └── MANIFEST.MF └── repository ├── accurest │ ├── bookDeleted.groovy │ ├── bookReturned1.groovy │ └── bookReturned2.groovy └── mappings
考虑以下合同(编号1):
Contract.make { label 'return_book_1' input { triggeredBy('bookReturnedTriggered()') } outputMessage { sentTo('returnBook') body('''{ "bookName" : "foo" }''') headers { header('BOOK-NAME', 'foo') } } }
现在考虑2:
Contract.make { label 'return_book_2' input { messageFrom('bookStorage') messageBody([ bookName: 'foo' ]) messageHeaders { header('sample', 'header') } } outputMessage { sentTo('returnBook') body([ bookName: 'foo' ]) headers { header('BOOK-NAME', 'foo') } } }
现在考虑以下Spring配置:
stubrunner.repositoryRoot: classpath:m2repo/repository/ stubrunner.ids: org.springframework.cloud.contract.verifier.stubs:streamService:0.0.1-SNAPSHOT:stubs stubrunner.stubs-mode: remote spring: cloud: stream: bindings: output: destination: returnBook input: destination: bookStorage server: port: 0 debug: true
这些示例适用于三种情况:
- 方案1(无输入消息)
- 方案2(由输入触发的输出)
- 方案3(输入无输出)
要通过return_book_1
标签触发消息,请使用StubTrigger
接口,如下所示:
stubFinder.trigger('return_book_1')
要收听发送到destination
为returnBook
的频道的消息的输出,请执行以下操作:
Message<?> receivedMessage = messaging.receive('returnBook')
收到的消息传递以下断言:
receivedMessage != null assertJsons(receivedMessage.payload) receivedMessage.headers.get('BOOK-NAME') == 'foo'
由于已为您设置了路线,因此您可以向bookStorage
destination
发送消息:
messaging.send(new BookReturned('foo'), [sample: 'header'], 'bookStorage')
要监听发送到returnBook
的消息的输出,请执行以下操作:
Message<?> receivedMessage = messaging.receive('returnBook')
收到的消息传递以下断言:
receivedMessage != null assertJsons(receivedMessage.payload) receivedMessage.headers.get('BOOK-NAME') == 'foo'
由于已为您设置了路由,因此您可以向output
目标发送消息:
messaging.send(new BookReturned('foo'), [sample: 'header'], 'delete')
以上内容是否对您有帮助:
更多建议: