Skip to main content Link Menu Expand (external link) Document Search Copy Copied

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:

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 as config, datasource and cloud.

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

  1. 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
    
  2. 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
    
  3. 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)
    
  4. 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
    
  5. 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 line 12. This file includes the following changes:

    • An additional spring.jms.servicebus section from lines 20 to 24. Notice that the connection-string property contains a translate to the connectionstring property (without the -). This is because Key Vault does not allow for - in secret keys.
  6. Commit and push your changes to the remote repository.

    git add .
    git commit -m 'added service bus'
    git push