This code sample demonstrates how to use the Spring Cloud Stream Kafka binder for Azure Event Hub.
The sample app exposes a rest api to receive string message. Then message is sent through Azure
Event Hub to a sink
which simply logs the message.
Running this sample will be charged by Azure. You can check the usage and bill at this link.
-
Create Azure credential file. Please see how to create credential file
$ az login $ az account set --subscription <name or id> $ az ad sp create-for-rbac --sdk-auth > my.azureauth
Make sure
my.azureauth
is encoded with UTF-8. -
Put credential file under
src/main/resources/
. -
Create Kafka enabled event hub. Please follow How to create Kafka enabled Event Hubs
-
Update application.properties file
# Enter 'my.azureauth' here if following step 1 and 2 spring.cloud.azure.credential-file-path=[credential-file-path] spring.cloud.azure.resource-group=[resource-group] spring.cloud.azure.eventhub.namespace=[eventhub-namespace] spring.cloud.stream.bindings.input.destination=[eventhub-name] spring.cloud.stream.bindings.input.group=[consumer-group] spring.cloud.stream.bindings.output.destination=[eventhub-name]
-
Run the
mvn clean spring-boot:run
in the root of the code sample to get the app running. -
Send a POST request
$ curl -X POST http://localhost:8080/messages?message=hello
-
Verify in your app’s logs that a similar message was posted:
New message received: hello
-
Delete the resources on Azure Portal to avoid unexpected charges.