W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
您可以在類路徑上同時使用Spring AMQP和Spring Cloud Contract Stub Runner,并設(shè)置屬性stubrunner.amqp.enabled=true
。請記住用@AutoConfigureStubRunner
注釋測試類。
如果您已經(jīng)在類路徑上具有Stream and Integration,則需要通過設(shè)置
stubrunner.stream.enabled=false
和stubrunner.integration.enabled=false
屬性來顯式禁用它們。
假設(shè)您具有以下Maven存儲庫,其中包含spring-cloud-contract-amqp-test
應(yīng)用程序的已部署存根。
└── .m2 └── repository └── com └── example └── spring-cloud-contract-amqp-test ├── 0.4.0-SNAPSHOT │ ├── spring-cloud-contract-amqp-test-0.4.0-SNAPSHOT.pom │ ├── spring-cloud-contract-amqp-test-0.4.0-SNAPSHOT-stubs.jar │ └── maven-metadata-local.xml └── maven-metadata-local.xml
進一步假設(shè)存根包含以下結(jié)構(gòu):
├── META-INF │ └── MANIFEST.MF └── contracts └── shouldProduceValidPersonData.groovy
考慮以下合同:
Contract.make { // Human readable description description 'Should produce valid person data' // Label by means of which the output message can be triggered label 'contract-test.person.created.event' // input to the contract input { // the contract will be triggered by a method triggeredBy('createPerson()') } // output message of the contract outputMessage { // destination to which the output message will be sent sentTo 'contract-test.exchange' headers { header('contentType': 'application/json') header('__TypeId__': 'org.springframework.cloud.contract.stubrunner.messaging.amqp.Person') } // the body of the output message body([ id : $(consumer(9), producer(regex("[0-9]+"))), name: "me" ]) } }
現(xiàn)在考慮以下Spring配置:
stubrunner: repositoryRoot: classpath:m2repo/repository/ ids: org.springframework.cloud.contract.verifier.stubs.amqp:spring-cloud-contract-amqp-test:0.4.0-SNAPSHOT:stubs stubs-mode: remote amqp: enabled: true server: port: 0
要使用上述合同觸發(fā)消息,請使用StubTrigger
界面,如下所示:
stubTrigger.trigger("contract-test.person.created.event")
該消息的目的地為contract-test.exchange
,因此Spring AMQP存根運行器集成查找與該交換有關(guān)的綁定。
@Bean public Binding binding() { return BindingBuilder.bind(new Queue("test.queue")) .to(new DirectExchange("contract-test.exchange")).with("#"); }
綁定定義綁定隊列test.queue
。結(jié)果,以下偵聽器定義將與合同消息匹配并調(diào)用。
@Bean public SimpleMessageListenerContainer simpleMessageListenerContainer( ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames("test.queue"); container.setMessageListener(listenerAdapter); return container; }
此外,以下帶注釋的偵聽器將匹配并被調(diào)用:
@RabbitListener(bindings = @QueueBinding(value = @Queue("test.queue"), exchange = @Exchange(value = "contract-test.exchange", ignoreDeclarationExceptions = "true"))) public void handlePerson(Person person) { this.person = person; }
該消息被直接移交給與匹配的SimpleMessageListenerContainer
相關(guān)聯(lián)的MessageListener
的onMessage
方法。
為了避免Spring AMQP在我們的測試期間嘗試連接到正在運行的代理,請配置模擬ConnectionFactory
。
要禁用模擬的ConnectionFactory,請設(shè)置以下屬性:stubrunner.amqp.mockConnection=false
stubrunner: amqp: mockConnection: false
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: