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

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

  1. From the Git Bash window, in the java-microservices-aks-lab repository you cloned locally, navigate to the src directory and use your favorite text editor to open the pom.xml file in the root directory of the cloned repo. you’ll have to uncomment the module for the spring-petclinic-messaging-emulator in the <modules> element at line 26.

     <module>spring-petclinic-messaging-emulator</module>
    
  2. Run a build of the messaging emulator.

    cd ~/workspaces/java-microservices-aks-lab/src
    mvn clean package -DskipTests -rf :spring-petclinic-messaging-emulator
    
  3. 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
    
  4. 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
    
  5. 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_CONNECTIONSTRING and the volumes and volumeMount definitions. 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
    
  6. Apply the spring-petclinic-messaging-emulator.yml yaml file on your cluster.

    kubectl apply -f spring-petclinic-messaging-emulator.yml
    

    You can run kubectl get pods -n spring-petclinic -w in another bash window to see the microservices spin up.

  7. Double check the secrets, you should see an additional secret for sbsecret.

    kubectl get secrets
    
  8. 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
    
  9. Use your browser to go to this IP on port 8080. This will open up the messaging emulator page.

  10. 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.

  11. In the Azure Portal, navigate to your resource group and select the Service Bus namespace you deployed in the previous task.

  12. In the navigation menu, in the Entities section, select Queues and then select the visits-requests queue entry.

  13. On the Overview page of the visits-requests queue, verify that the active message count is set to 1.

  14. 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.

  15. 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 and PetClinicMessageResponsesReceiver classes in the service folder. These are the classes that enable sending and receiving of messages to and from a queue using JMS.
  • The PetClinicMessageRequest and PetClinicMessageResponse classes in the entity folder. These are the messages being sent back and forth.
  • The MessagingConfig class in the config folder. This class provides conversion to and from JSON.
  • The AzureServiceBusResource class in the web 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.