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. For a more secure connection, you should also use managed identities associated with your microservices to connect directly to the Service Bus namespace. In this lab, you could use the identity that attached to the container apps environment earlier.

The connection to the Service Bus needs to be stored in the spring.jms.servicebus application property. Add the following section to the application.yml file and fill out the Service Bus namespace and the client id of the identity in your configuration repository.

     jms:
       queue:
         visits-requests: visits-requests
         visits-confirmations: visits-confirmations
       servicebus:
         namespace: ${SERVICEBUS_NAMESPACE}
         pricing-tier: premium
         passwordless-enabled: true
         credential:
           managed-identity-enabled: true
           client-id: ${CLIENT_ID}

Particular attention to indentation as shown above is important: jms should be at the same indentation level as config, datasource and cloud.

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 resource id of the new created Service Bus namespace.
    SERVICEBUS_ID=$(az servicebus namespace show --resource-group $RESOURCE_GROUP --name $SERVICEBUS_NAMESPACE --query id -o tsv)
    echo $SERVICEBUS_ID
    
  4. Retrieve the information of the user identity attached to the container app environment.
    SP_ID=$(az identity show --resource-group $RESOURCE_GROUP --name $ACA_IDENTITY --query principalId --output tsv)
    echo $SP_ID
    CLIENT_ID=$(az identity show --resource-group $RESOURCE_GROUP --name $ACA_IDENTITY --query clientId --output tsv)
    echo $CLIENT_ID
    
  5. Assign access for the identity to send message to or receive message from the Service Bus. Azure provides a set of built-in roles for authorizing access to a Service Bus namespace.

    az role assignment create --assignee $SP_ID --scope $SERVICEBUS_ID --role "Azure Service Bus Data Sender"
    az role assignment create --assignee $SP_ID --scope $SERVICEBUS_ID --role "Azure Service Bus Data Receiver"
    
  6. 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 0801_application.yml file. Make sure you fill out your current MySQL server name on line 12, the service bus namespace and identity client id will be injected by above environment variables. This file includes the following changes:

    • An additional spring.jms.servicebus section from lines 20 to 27.
  7. Commit and push your changes to the remote repository.

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