This code sample demonstrates how to use the Spring Cloud Stream binder for Azure Service Bus queue.
The sample app exposes a rest api to receive string message. Then message is sent through Azure
Service Bus 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 Service Bus namespace and queue. Please see how to create.
-
Update application.properties
# Fill service bus namespace connection string copied from portal spring.cloud.azure.servicebus.connection-string=[servicebus-namespace-connection-string]
-
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.azureauthis encoded with UTF-8. -
Put credential file under
src/main/resources/. -
Create Azure Service Bus namespace and queue. Please see how to create. Or enable auto create resources feature in application.properties:
spring.cloud.azure.auto-create-resources=true # Example region is westUS, northchina # spring.cloud.azure.region=[region]
-
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.servicebus.namespace=[servicebus-namespace]
-
Update stream binding related properties in application.properties file
# For example here, the destination name of input and output should be the same. spring.cloud.stream.bindings.input.destination=[servicebus-queue-name] spring.cloud.stream.bindings.output.destination=[servicebus-queue-name-same-as-above]
-
Run the
mvn clean spring-boot:runin the root of the code sample to get the app running. -
Send a POST request
$ curl -X POST localhost:8080/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.