Test the messaging functionality
In the spring-petclinic-microservices repository, the spring-petclinic-messaging-emulator
microservice is already prepared to send messages to an Azure Service Bus namespace. You can add this microservice to your current Spring Petclinic project in the parent pom.xml
file, deploy it as an extra microservice in your Azure Spring Apps service and use this microservice’s public endpoint to send messages to your Service Bus namespace. Test this functionality and inspect whether messages end up in the Service Bus namespace you just created by using the Service Bus Explorer for the visits-requests
queue. You can use the following guidance to implement these changes:
Step by step guidance
-
As a first step in the parent
pom.xml
file, you’ll have to uncomment the module for thespring-petclinic-messaging-emulator
in the<modules>
element at line 26.<module>spring-petclinic-messaging-emulator</module>
-
Update the compiled version of the microservices available by running an additional build.
cd ~/workspaces/java-microservices-asa-e-lab/src mvn clean package -DskipTests -rf :spring-petclinic-messaging-emulator
-
Create a new application in your Spring Apps service for the
messaging-emulator
and assign a public endpoint to it.MESSAGING_EMULATOR=messaging-emulator az spring app create \ --name $MESSAGING_EMULATOR \ --assign-endpoint true
Wait for the provisioning to complete. This might take about 3 minutes.
-
Update the Application Configuration Service to also pick up the messaging-emulator config.
az spring application-configuration-service git repo update \ --resource-group $RESOURCE_GROUP \ --name spring-petclinic-config \ --service $SPRING_APPS_SERVICE \ --label main \ --patterns "api-gateway,customers-service,vets-service,visits-service,admin-server,messaging-emulator" \ --uri $GIT_REPO \ --password $GIT_PASSWORD \ --username $GIT_USERNAME
In case you are using a branch other than
main
in your config repo, you can change the branch name with thelabel
parameter.Wait for the operation to complete. This might take about 2 minutes.
-
Bind this new app to the Application Configuration Service and to the Service Registry.
az spring application-configuration-service bind --app ${MESSAGING_EMULATOR} az spring service-registry bind --app ${MESSAGING_EMULATOR}
-
Create a new user assigned managed identity for this new application and assign it to the
messaging-emulator
app:MESSAGING_EMULATOR_ID=$(az identity create -g $RESOURCE_GROUP -n messaging-svc-uid --query id -o tsv) az spring app identity assign \ --resource-group $RESOURCE_GROUP \ --name $MESSAGING_EMULATOR \ --user-assigned $MESSAGING_EMULATOR_ID
-
Grant to the newly assigned identity the get and list permissions on your Key Vault secrets.
MESSAGING_EMULATOR_UID=$(az identity show -g $RESOURCE_GROUP -n messaging-svc-uid --query principalId -o tsv) az keyvault set-policy \ --name $KEYVAULT_NAME \ --resource-group $RESOURCE_GROUP \ --secret-permissions get list \ --object-id $MESSAGING_EMULATOR_UID
-
Since the messaging-emulator will also save data in the database, you will also need to create a service connection for it:
MESSAGING_EMULATOR_CID=$(az identity show -g $RESOURCE_GROUP -n messaging-svc-uid --query clientId -o tsv) az spring connection create mysql-flexible \ --resource-group $RESOURCE_GROUP \ --service $SPRING_APPS_SERVICE \ --app $MESSAGING_EMULATOR \ --target-resource-group $RESOURCE_GROUP \ --server $MYSQL_SERVER_NAME \ --database $DATABASE_NAME \ --user-identity mysql-identity-id=$ADMIN_IDENTITY_RESOURCE_ID client-id=$MESSAGING_EMULATOR_CID subs-id=$SUBID
-
You can now deploy the messaging-emulator application.
MESSAGING_EMULATOR_JAR=spring-petclinic-messaging-emulator/target/spring-petclinic-messaging-emulator-$VERSION.jar az spring app deploy --name ${MESSAGING_EMULATOR} \ --config-file-patterns ${MESSAGING_EMULATOR} \ --artifact-path ${MESSAGING_EMULATOR_JAR}
-
Switch to the web browser window displaying the Azure Portal, navigate to the resource group containing the resources you deployed in this lab, and, from there, navigate to the Azure Spring Apps Service.
-
In the navigation menu, in the Settings section, select Apps, wait until the Provisioning state of the
messaging-emulator
app changes to Succeeded, and then select themessaging-emulator
app entry.The provisioning might take about 3 minutes. Select Refresh in order to update the provisioning status.
-
Once provisioning is complete. Select the apps URL and open this in a new browser window.
-
On the newly opened browser page titled Message, enter 1 in the Pet text box and a random text in the Message text box, and then select Submit.
-
In the Azure Portal, navigate to your resource group and select the Service Bus namespace you deployed in the previous task.
-
In the navigation menu, in the Entities section, select Queues and then select the
visits-requests
queue entry. -
On the Overview page of the
visits-requests
queue, verify that the active message count is set to 1. -
Select Service Bus Explorer (Preview) and select Peek from start. This operation allows you to peek at the top messages on the queue, without dequeuing them.
-
Select the message entry in the queue and review the Message Body section to confirm that its content matches the message you submitted.
You might want to inspect the code of the messaging-emulator
microservice. Take a look at:
- The dependencies for the Service Bus in the
pom.xml
file. - The
PetClinicVisitRequestSender
andPetClinicMessageResponsesReceiver
classes in theservice
folder. These are the classes that enable sending and receiving of messages to and from a queue using JMS. - The
PetClinicMessageRequest
andPetClinicMessageResponse
classes in theentity
folder. These are the messages being sent back and forth. - The
MessagingConfig
class in theconfig
folder. This class provides conversion to and from JSON. - The
AzureServiceBusResource
class in theweb
folder. This class makes use of the above classed to send a message to the Service Bus.
In the next steps you will add similar functionality to the visits
service.