Try out an existing microservice
In the java-microservices-aca-lab repository’s src directory, 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 ACA environment 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
-
From the Git Bash window, in the
java-microservices-aca-lab
repository you cloned locally, navigate to thesrc
directory and use your favorite text editor to open thepom.xml
file in the root directory of the cloned repo. 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>
-
Run a build of the messaging emulator.
cd ~/workspaces/java-microservices-aca-lab/src mvn clean package -DskipTests -rf :spring-petclinic-messaging-emulator
-
Once the build is done, move to the
staging-acr
directory and create the extra one container app for the messaging emulator.export APP_NAME="messaging-emulator" cp ../spring-petclinic-$APP_NAME/target/spring-petclinic-$APP_NAME-$VERSION.jar spring-petclinic-$APP_NAME-$VERSION.jar sed -i "s|my-service|$APP_NAME|g" Dockerfile az containerapp create \ --name $APP_NAME \ --resource-group $RESOURCE_GROUP \ --source . \ --env-vars APPLICATIONINSIGHTS_CONNECTION_STRING=$AI_CONNECTIONSTRING APPLICATIONINSIGHTS_CONFIGURATION_CONTENT='{"role": {"name": "messaging-emulator"}}' InstrumentationKey=$AI_CONNECTIONSTRING SERVICEBUS_NAMESPACE=$SERVICEBUS_NAMESPACE CLIENT_ID=$CLIENT_ID \ --registry-server $MYACR.azurecr.io \ --registry-identity $USER_ID \ --environment $ACA_ENVIRONMENT \ --user-assigned $USER_ID \ --ingress external \ --target-port 8080 \ --min-replicas 1 \ --bind $JAVA_CONFIG_COMP_NAME $JAVA_EUREKA_COMP_NAME \ --runtime java sed -i "s|$APP_NAME|my-service|g" Dockerfile rm spring-petclinic-$APP_NAME-$VERSION.jar
- Update the container app to connect to database securely using identity.
EMULATOR_ID=$(az containerapp show \ --resource-group $RESOURCE_GROUP \ --name $APP_NAME \ --query id \ -o tsv) az containerapp connection create mysql-flexible \ --connection mysql_conn \ --source-id $EMULATOR_ID \ --target-id $DB_ID \ --client-type SpringBoot \ --user-identity client-id=$CLIENT_ID subs-id=$SUBID mysql-identity-id=$ADMIN_IDENTITY_RESOURCE_ID \ -c messaging-emulator
- You configured the messaging-emulator with an external ingress. You can go to the portal and check application url for the messaging-emulator container app.
messaging_emulator_FQDN=$(az containerapp show \ --resource-group $RESOURCE_GROUP \ --name messaging-emulator \ --query properties.configuration.ingress.fqdn \ -o tsv) echo $messaging_emulator_FQDN
-
Use a browser window to connect to messaging_emulator_FQDN. This will open up the messaging emulator page.
-
On the newly open 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 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
.