SpringCloud 禁用功能

2023-12-12 17:48 更新

如果需要禁用此功能,請(qǐng)?jiān)O(shè)置stubrunner.integration.enabled=false屬性。

假設(shè)您具有以下Maven存儲(chǔ)庫(kù),其中包含integrationService應(yīng)用程序的已部署存根:

└── .m2
    └── repository
        └── io
            └── codearte
                └── accurest
                    └── stubs
                        └── integrationService
                            ├── 0.0.1-SNAPSHOT
                            │   ├── integrationService-0.0.1-SNAPSHOT.pom
                            │   ├── integrationService-0.0.1-SNAPSHOT-stubs.jar
                            │   └── maven-metadata-local.xml
                            └── maven-metadata-local.xml

進(jìn)一步假設(shè)存根包含以下結(jié)構(gòu):

├── META-INF
│   └── MANIFEST.MF
└── repository
    ├── accurest
    │   ├── bookDeleted.groovy
    │   ├── bookReturned1.groovy
    │   └── bookReturned2.groovy
    └── mappings

考慮以下合同(編號(hào)1):

Contract.make {
	label 'return_book_1'
	input {
		triggeredBy('bookReturnedTriggered()')
	}
	outputMessage {
		sentTo('output')
		body('''{ "bookName" : "foo" }''')
		headers {
			header('BOOK-NAME', 'foo')
		}
	}
}

現(xiàn)在考慮2

Contract.make {
	label 'return_book_2'
	input {
		messageFrom('input')
		messageBody([
				bookName: 'foo'
		])
		messageHeaders {
			header('sample', 'header')
		}
	}
	outputMessage {
		sentTo('output')
		body([
				bookName: 'foo'
		])
		headers {
			header('BOOK-NAME', 'foo')
		}
	}
}

以及以下Spring Integration路線:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			 xmlns:beans="http://www.springframework.org/schema/beans"
			 xmlns="http://www.springframework.org/schema/integration"
			 xsi:schemaLocation="http://www.springframework.org/schema/beans
			https://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/integration
			http://www.springframework.org/schema/integration/spring-integration.xsd">


	<!-- REQUIRED FOR TESTING -->
	<bridge input-channel="output"
			output-channel="outputTest"/>

	<channel id="outputTest">
		<queue/>
	</channel>

</beans:beans>

這些示例適用于三種情況:

  • 方案1(無(wú)輸入消息)
  • 方案2(由輸入觸發(fā)的輸出)
  • 方案3(輸入無(wú)輸出)

方案1(無(wú)輸入消息)

要通過(guò)return_book_1標(biāo)簽觸發(fā)消息,請(qǐng)使用StubTigger接口,如下所示:

stubFinder.trigger('return_book_1')

要監(jiān)聽(tīng)發(fā)送到output的消息的輸出,請(qǐng)執(zhí)行以下操作:

Message<?> receivedMessage = messaging.receive('outputTest')

收到的消息將通過(guò)以下斷言:

receivedMessage != null
assertJsons(receivedMessage.payload)
receivedMessage.headers.get('BOOK-NAME') == 'foo'

場(chǎng)景2(由輸入觸發(fā)輸出)

由于已為您設(shè)置了路由,因此您可以向output目標(biāo)發(fā)送消息:

messaging.send(new BookReturned('foo'), [sample: 'header'], 'input')

要監(jiān)聽(tīng)發(fā)送到output的消息的輸出,請(qǐng)執(zhí)行以下操作:

Message<?> receivedMessage = messaging.receive('outputTest')

收到的消息傳遞以下斷言:

receivedMessage != null
assertJsons(receivedMessage.payload)
receivedMessage.headers.get('BOOK-NAME') == 'foo'

方案3(輸入無(wú)輸出)

由于已為您設(shè)置了路由,因此您可以向input目標(biāo)發(fā)送消息:

messaging.send(new BookReturned('foo'), [sample: 'header'], 'delete')


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)