Azure Setup
The Azure Spring Apps setup can be reused from the above section.
Azure Service Bus
az servicebus namespace create -n <name> --sku Standard
Since the applications use Topics, a Standard plan is needed. The Basic plan supports only Queues.
Once the Service Bus namespace is created, add the Topic. The name used in the ValueController is values-topic
az servicebus topic create -n values-topic --namespace-name
<namespace_name>
Now, retrieve the value of primaryConnectionString from the json returned by the below command. This will be used by the application to connect to the Service Bus namespace in this example.
az servicebus namespace authorization-rule keys list
--namespace-name <namespace_name> --name
RootManageSharedAccessKey
Save this connection string for later. This is just for the demo. In a Production app, integrate with Azure Key Vault to securely access the connection string.
Since the Processor and Consumer uses the uppercase-values-topic, create it by following the above steps.
Once the topics are created, appropriate subscriptions have to be created as well. In this example, Processor subscribes to values-topic and Consumer subscribes to uppercase-values-topic. So, these two services have to be registered as the subscribers in the corresponding topics. Create them as below:
az servicebus topic subscription create --namespace-name
<namespace_name> --topic-name values-topic --name processor
az servicebus topic subscription create --namespace-name
<namespace_name> --topic-name uppercase-values-topic --name consumer
Note: Before deploying the Processor and Consumer apps, both the topics and the subscriptions should have been created. Otherwise, the apps won’t start.
Source Code Changes
Clone the Spring Cloud Stream with RabbitMQ code from the Git. The Spring Cloud Stream abstracts away any code needed to connect to a particular messaging platform. As a result, most of the changes will be in the build.gradle file.
Producer
Dependencies
Since Producer is a Web project that will publish an event to Azure Service Bus, select the below list of dependencies in https://start.spring.io/
Spring Web, Cloud Stream, Azure Support, Lombok
If the Azure Support is greyed out, try a different, stable version of Spring Boot.The generated build.gradle should resemble the file here.
Apart from these dependencies from the Spring Initializer, Service Bus specific dependency needs to be added manually. Since this app uses a topic (not queue) in the RabbitMQ example, the topic specific dependency needs to be added as below:
implementation 'com.azure.spring:azure-spring-cloud-stream-binder-servicebus-topic:2.5.0'
Configuration
In order to connect to Azure Service Bus, a connection string needs to be provided. So, create an application.yml file and add the below lines. Use the connection string value obtained earlier during the Setup
spring:
cloud:
azure:
servicebus:
connection-string:
Note: Providing connection string directly in the yml file is only recommended for local/test purposes.
For Production Ready applications, integrate with Azure Key Vault to securely retrieve the value.
Processor & Consumer
Dependencies
The build.gradle from the Producer can be reused by removing the web dependency alone since Processor and Consumer doesn’t have web interfaces. Ensure that the servicebus-topic dependency exists.
Configuration
Since the Processor and Consumer applications already have an application.yml with Topic details, we just need to add the connection string property. Copy that section alone from the Producer application.
With this, the code changes are completed. The final code is available in the GitHub.
Deployment
Now the code is ready to be deployed. Generate the boot jars using clean and assemble. Deploy the apps to Azure Spring Apps using the command below:
az spring app deploy -n <app_name> -s
<name_of_azure_spring_apps> --artifact-path
<path_of_generated_jar>
The app log can be tailed in a different set of windows using:
az spring app logs -n <app_name> -s <name_of_azure_spring_apps> -f
Once the apps are deployed and started, the changes can be tested using the Test URL of Producer app in the Azure Spring Apps apps page.