Create an Azure Service Bus resource
First, you need to create an Azure Service Bus namespace and one or more queues to send messages to. In your implementation, you will create a queue named visits-requests
. You can use the following guidance to implement these changes:
- Use the Azure CLI to create a Service Bus namespace and a queue.
- Use Azure CLI to create a Service Bus topic and subscriptions to the topic.
Make sure to create the Service Bus namespace with the Premium SKU, since this is required in order to support JMS 2.0 messaging. You should also add a connection string to your Service Bus namespace in the Key Vault instance you provisioned earlier in this lab, so the microservices can retrieve its value.
As a more secure alternative, you could use managed identities associated with your microservices to connect directly to the Service Bus namespace. However, in this lab, you will store the connection string in your Key Vault.
The connection to the Service Bus needs to be stored in the spring.jms.servicebus.connection-string
application property. Name your Key Vault secret SPRING-JMS-SERVICEBUS-CONNECTIONSTRING
and add the following section to the application.yml
file in your configuration repository.
jms:
servicebus:
connection-string: ${spring.jms.servicebus.connectionstring}
idle-timeout: 60000
pricing-tier: premium
Particular attention to indentation as shown above is important:
jms
should be at the same indentation level asconfig
,datasource
andcloud
.
This translates the secret in Key Vault to the correct application property for your microservices. This usage of properties is described in the following documentation: Special Characters in Property Name.
Step by step guidance
-
On your lab computer, in Git Bash window, from the Git Bash prompt, run the following command to create a Service Bus namespace. Note that the name of the namespace needs to be globally unique, so adjust it accordingly in case the randomly generated name is already in use. You will need to create the namespace with the Premium sku. This is needed to use JMS 2.0 messaging later on in the lab.
SERVICEBUS_NAMESPACE=sb-$APPNAME-$UNIQUEID az servicebus namespace create \ --resource-group $RESOURCE_GROUP \ --name $SERVICEBUS_NAMESPACE \ --location $LOCATION \ --sku Premium
-
You can now create a queue in this namespace called visits-requests.
az servicebus queue create \ --resource-group $RESOURCE_GROUP \ --namespace-name $SERVICEBUS_NAMESPACE \ --name visits-requests
-
You can now create another queue in this namespace called visits-confirmations that will be used later in the lab.
az servicebus queue create \ --resource-group $RESOURCE_GROUP \ --namespace-name $SERVICEBUS_NAMESPACE \ --name visits-confirmations
-
Retrieve the value of the connection string to the newly created Service Bus namespace:
SERVICEBUS_CONNECTIONSTRING=$(az servicebus namespace authorization-rule keys list \ --resource-group $RESOURCE_GROUP \ --namespace-name $SERVICEBUS_NAMESPACE \ --name RootManageSharedAccessKey \ --query primaryConnectionString \ --output tsv)
-
Create a new Key Vault secret for this connection string.
az keyvault secret set \ --name SPRING-JMS-SERVICEBUS-CONNECTIONSTRING \ --value $SERVICEBUS_CONNECTIONSTRING \ --vault-name $KEYVAULT_NAME
-
In your configuration repository replace the
application.yml
with the contents of this application.yml file. This file contains the following changes:- It adds the
spring.jms.servicebus
configuration on lines 16 to 20. - Make sure you replace the
<your-kv-name>
replacement value with the name of your Key Vault.
- It adds the
-
Commit and push your changes to the remote repository.
cd ~/workspaces/java-microservices-asa-e-lab git add . git commit -m 'added service bus' git push