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
-
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 the config repository you will need to add the service bus connection information. Replace the contents of the current
application.yml
file with the contents of the 0501_application.yml file. Make sure you fill out your current MySQL server name on line12
. This file includes the following changes:- An additional
spring.jms.servicebus
section from lines20
to24
. Notice that theconnection-string
property contains a translate to theconnectionstring
property (without the-
). This is because Key Vault does not allow for-
in secret keys.
- An additional
-
Commit and push your changes to the remote repository.
git add . git commit -m 'added service bus' git push