This code sample demonstrates how to use the Spring Cloud Stream 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.
We have several ways to config the Spring Cloud Stream Binder for Azure Event Hub. You can choose anyone of them.
-
Create Azure Event Hubs. Please note
Basic
tier is unsupported. After creating the Azure Event Hub, you can create your own Consumer Group or use the default "$Default" Consumer Group. -
Create Azure Storage for checkpoint use.
-
Update application.properties.
# Fill event hub namespace connection string copied from portal spring.cloud.azure.eventhub.connection-string=[eventhub-namespace-connection-string] # Fill checkpoint storage account name, accese key and container spring.cloud.azure.eventhub.checkpoint-storage-account=[checkpoint-storage-account] spring.cloud.azure.eventhub.checkpoint-access-key=[checkpoint-accesskey] spring.cloud.azure.eventhub.checkpoint-container=[checkpoint-container] # Fill eventhub infomation spring.cloud.stream.bindings.input.destination=[eventhub-name] spring.cloud.stream.bindings.input.group=[consumer-group] spring.cloud.stream.bindings.output.destination=[eventhub-name] # Use manual checkpoint mode spring.cloud.stream.eventhub.bindings.input.consumer.checkpoint-mode=MANUAL # Config this property if you want to create resource automatically #spring.cloud.azure.auto-create-resources=true #spring.cloud.azure.region=[region]
-
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 Azure Event Hubs and Azure Storage. Please note event hub
Basic
tier is unsupported. Or enable auto create resources feature in application.properties:spring.cloud.azure.auto-create-resources=true # Default environment is GLOBAL. Provide your own if in another environment # Example environment is China, GLOBAL # spring.cloud.azure.environment=[environment] # Example region is westUS, northchina spring.cloud.azure.region=[region]
-
Update credential file based properties in application.properties
# 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.azure.eventhub.checkpoint-storage-account=[checkpoint-storage-account] spring.cloud.azure.eventhub.checkpoint-container=[checkpoint-container] # Fill eventhub infomation spring.cloud.stream.bindings.input.destination=[eventhub-name] spring.cloud.stream.bindings.input.group=[consumer-group] spring.cloud.stream.bindings.output.destination=[eventhub-name]
-
Create Azure Event Hubs. Please note
Basic
tier is unsupported. -
Create Azure Storage for checkpoint use.
Please note your application should run in VM (Virtual Machine) or App Services on Azure for support of MSI. Choose any of them.
-
Create VM in Azure portal. Please refer to Create a Windows virtual machine in the Azure portal or Create a Linux virtual machine in the Azure portal. Choose any one according to your needs.
-
Create an user-assigned identity in Azure Portal. Please refer to Create an user-assigned managed identity.
-
Assign the user-assigned identity to the VM. Please refer to Assign an user-assigned managed identity to an existing VM.
-
1. Deploy this sample’s Spring Boot JAR file to App Service.
You can follow Deploy a Spring Boot JAR file to Azure App Service to deploy the JAR file.
Another way to deploy an executable JAR is via FTP/S. Follow
Deploy your app to App Service using FTP/S. And the JAR file’s name must be app.jar
.
-
2. Create a managed identity for App Service.
If you choose system-assigned identity, follow Adding a system assigned identity.
If you choose user-assigned identity, follow Adding a user assigned identity.
Add role assignment to Event hub, Storage Account and Resource Group.
-
Event Hub:
Contributor
role. -
Storage Account:
Storage Account Key Operator Service Role
role. -
Resource Group:
Reader
role.
See Managed identities for Azure resources with Event Hubs to add role assignment for Event Hub, Storage Account and Resource Group are similar.
For different built-in role’s descriptions, please see Built-in role descriptions.
-
Update application.properties
# Enable MSI for event hub and storage account spring.cloud.azure.msi-enabled=true # Fill subscription ID copied from portal spring.cloud.azure.subscription-id=[subscription-id] # Fill client ID if user-assigned identity is used in App Service spring.cloud.azure.managed-identity.client-id=[The ID of the user-assigned identity to be used] # Fill resource group name spring.cloud.azure.resource-group=[resource-group] # Fill checkpoint storage account name spring.cloud.azure.eventhub.checkpoint-storage-account=[checkpoint-storage-account] # Fill checkpoint storage container spring.cloud.azure.eventhub.checkpoint-container=[checkpoint-container] # Fill event hub namespace spring.cloud.azure.eventhub.namespace=[eventhub-namespace] # Fill eventhub infomation spring.cloud.stream.bindings.input.destination=[eventhub-name] spring.cloud.stream.bindings.input.group=[consumer-group] spring.cloud.stream.bindings.output.destination=[eventhub-name] spring.cloud.stream.eventhub.bindings.input.consumer.checkpoint-mode=MANUAL
-
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
or when the app runs on App Service or VM
$ curl -d -X POST https://[your-app-URL]/messages?message=hello
-
Verify in your app’s logs that a similar message was posted:
New message received: 'hello' Message 'hello' successfully checkpointed
-
Delete the resources on Azure Portal to avoid unexpected charges.