Try out an existing microservice
In the java-microservices-aks-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 AKS cluster 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-aks-labrepository you cloned locally, navigate to thesrcdirectory and use your favorite text editor to open thepom.xmlfile in the root directory of the cloned repo. you’ll have to uncomment the module for thespring-petclinic-messaging-emulatorin the<modules>element at line 26.<module>spring-petclinic-messaging-emulator</module> -
Run a build of the messaging emulator.
cd ~/workspaces/java-microservices-aks-lab/src mvn clean package -DskipTests -rf :spring-petclinic-messaging-emulator -
Copy the newly compiled jar file for the messaging-emulator to the staging-acr directory and build the container image for it.
cd staging-acr cp ../spring-petclinic-messaging-emulator/target/spring-petclinic-messaging-emulator-$VERSION.jar spring-petclinic-messaging-emulator-$VERSION.jar docker build -t $MYACR.azurecr.io/spring-petclinic-messaging-emulator:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-messaging-emulator-$VERSION.jar \ --build-arg APP_PORT=8080 \ --build-arg AI_JAR=ai.jar \ . docker push $MYACR.azurecr.io/spring-petclinic-messaging-emulator:$VERSION -
You will also need to add a mapping for the SPRING-JMS-SERVICEBUS-CONNECTION-STRING secret in Key Vault in the SecretProviderClass. You can update the SecretProviderClass with the following bash statement.
cat <<EOF | kubectl apply -n spring-petclinic -f - apiVersion: secrets-store.csi.x-k8s.io/v1 kind: SecretProviderClass metadata: name: azure-kvname-user-msi spec: provider: azure secretObjects: - secretName: gitpatsecret type: Opaque data: - objectName: gitpat key: gitpat - secretName: sbsecret type: Opaque data: - objectName: sbconn key: sbconn parameters: usePodIdentity: "false" useVMManagedIdentity: "false" clientID: $USER_ASSIGNED_CLIENT_ID keyvaultName: $KEYVAULT_NAME cloudName: "" objects: | array: - | objectName: SPRING-JMS-SERVICEBUS-CONNECTIONSTRING objectType: secret objectAlias: sbconn objectVersion: "" - | objectName: GIT-PAT objectType: secret objectAlias: gitpat objectVersion: "" tenantId: $ADTENANT EOF -
In the kubernetes directory, create a new file spring-petclinic-messaging-emulator.yml, and copy the contents of the spring-petclinic-messaging-emulator.yml file to it. This file will contain all the changes and environment variables you have also defined in the other microservices. It also contains 1 extra environment variable for the
SPRING_JMS_SERVICEBUS_CONNECTIONSTRINGand thevolumesandvolumeMountdefinitions. You can again curl the updates for this file and then fill out the correct container registry name.cd ../kubernetes curl -o spring-petclinic-messaging-emulator.yml https://raw.githubusercontent.com/Azure-Samples/java-microservices-aks-lab/main/docs/05_lab_messaging/spring-petclinic-messaging-emulator.yml IMAGE=${MYACR}.azurecr.io/spring-petclinic-messaging-emulator:$VERSION sed -i "s|#image#|$IMAGE|g" spring-petclinic-messaging-emulator.yml -
Apply the
spring-petclinic-messaging-emulator.ymlyaml file on your cluster.kubectl apply -f spring-petclinic-messaging-emulator.ymlYou can run kubectl get pods -n spring-petclinic -w in another bash window to see the microservices spin up.
-
Double check the secrets, you should see an additional secret for sbsecret.
kubectl get secrets -
The messaging-emulator is configured with a load balancer as a service. Once everything is up and running you inspect the services in the cluster and copy the public IP of the messaging-emulator service.
kubectl get services -
Use your browser to go to this IP on port 8080. 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-requestsqueue entry. -
On the Overview page of the
visits-requestsqueue, 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.xmlfile. - The
PetClinicVisitRequestSenderandPetClinicMessageResponsesReceiverclasses in theservicefolder. These are the classes that enable sending and receiving of messages to and from a queue using JMS. - The
PetClinicMessageRequestandPetClinicMessageResponseclasses in theentityfolder. These are the messages being sent back and forth. - The
MessagingConfigclass in theconfigfolder. This class provides conversion to and from JSON. - The
AzureServiceBusResourceclass in thewebfolder. 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.