page_type | languages | products | description | urlFragment | ||
---|---|---|---|---|---|---|
sample |
|
|
Azure Spring Cloud Stream Binder Sample project for Multiple Event Hub Namespace client library |
azure-spring-cloud-sample-eventhubs-multibinders |
Spring Cloud Azure Stream Binder for Multiple Event Hub Namespace Code Sample shared library for Java
This code sample demonstrates how to use the Spring Cloud Stream Binder for multiple Azure Event Hub namespaces. In this sample you will bind to two Event Hub namespaces separately through two binders. The sample app has two operating modes. One way is to expose a Restful API to receive string message, another way is to automatically provide string messages. These messages are published to an event hub. The sample will also consume messages from the same event hub.
Running this sample will be charged by Azure. You can check the usage and bill at this link.
-
Create two Event Hubs in different Event Hub namespace. Please refer to Azure Event Hubs. Please note
Basic
tier is unsupported. -
Create Azure Storage for checkpoint use.
-
[Optional] if you want to use service principal, please follow create service principal from Azure CLI to create one.
-
[Optional] if you want to use managed identity, please follow create managed identity to set up managed identity.
-
Update stream binding related properties in application.yaml. If you choose to use service principal or managed identity, update the
application-sp.yaml
orapplication-mi.yaml
respectively.spring: cloud: stream: # To specify which functional bean to bind to the external destination(s) exposed by the bindings function: definition: consume1;supply1;consume2;supply2 bindings: consume1-in-0: destination: [eventhub-1-name] group: [consumer-group] supply1-out-0: destination: [the-same-eventhub-1-name-as-above] consume2-in-0: binder: eventhub-2 destination: [eventhub-2-name] group: [consumer-group] supply2-out-0: binder: eventhub-2 destination: [the-same-eventhub-2-name-as-above] binders: eventhub-1: type: eventhub default-candidate: true environment: spring: cloud: azure: eventhub: connection-string: [connection-string-of-first-eventhub-namespace] checkpoint-storage-account: [checkpoint-storage-account] checkpoint-access-key: [checkpoint-access-key] checkpoint-container: [checkpoint-container-1] eventhub-2: type: eventhub default-candidate: false environment: spring: cloud: azure: eventhub: connection-string: [connection-string-of-second-eventhub-namespace] checkpoint-storage-account: [checkpoint-storage-account] checkpoint-access-key: [checkpoint-access-key] checkpoint-container: [checkpoint-container-2] eventhub: bindings: consume1-in-0: consumer: checkpoint-mode: MANUAL consume2-in-0: consumer: checkpoint-mode: MANUAL poller: initial-delay: 0 fixed-delay: 1000
The defaultCandidate configuration item: Whether the binder configuration is a candidate for being considered a default binder, or can be used only when explicitly referenced. This allows adding binder configurations without interfering with the default processing.
Important
When using the Restful API to send messages, the Active profiles must contain manual
.
-
Run the
mvn clean spring-boot:run
in the root of the code sample to get the app running. -
Send a POST request to test the default binder
$ curl -X POST http://localhost:8080/messages1?message=hello
-
Verify in your app’s logs that a similar message was posted:
[1] New message1 received: 'hello' [1] Message1 'hello' successfully checkpointed
-
Send another POST request to test the other binder
$ curl -X POST http://localhost:8080/messages2?message=hello
-
Verify in your app’s logs that a similar message was posted:
[2] New message2 received: 'hello' [2] Message2 'hello' successfully checkpointed
-
Delete the resources on Azure Portal to avoid unexpected charges.