Deploy the component applications to Azure Container Apps
You now have everything ready to deploy the workload’s component applications to Azure, which will involve:
- Getting the sample code from the upstream Git repository,
- Building the projects to generate a JAR file for each application.
- Preparing the Dockerfile that you’ll use to build each container image.
- Creating the container apps instances for the applications.
Azure Container Apps can collect Java Virtual Machine (JVM) metrics for java applications. Enable JVM metrics with the option –runtime java.
Step-by-step guidance
-
Get the application code from public upstream repo, and then build the applications.
cd spring-petclinic-microservices git submodule update --init mvn clean package -DskipTests
-
After your build has finished, you can create images for each of the applications.
Use Azure Container Registry to create images, and then create container apps with those images. A Dockerfile is required for Azure Container Registry to build the image. The content of the Dockerfile is as follows:
# syntax=docker/dockerfile:1 FROM mcr.microsoft.com/openjdk/jdk:17-distroless # Prepare the Application Insights Java agent # This is optional for Lab 2. It's prepared for the Application Insights monitoring in Lab 3. ARG AI_VERSION=3.6.2 ADD https://github.com/microsoft/ApplicationInsights-Java/releases/download/$AI_VERSION/applicationinsights-agent-$AI_VERSION.jar /applicationinsights-agent.jar COPY ./target/*.jar app.jar EXPOSE 8080 # Run the jar file ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
This file is saved in ../tools/Dockerfile. You’ll use this file in the next steps.
- Deploy the api-gateway. Note the following:
- This is the public-facing entry point to the other applications, and you’ll create it with the setting –ingress external.
- You’ll bind this app to the config server and Eureka Server that you created earlier.
- To connect these applications to the Azure Container Registry, you’ll apply the user-assigned managed identity you created earlier using the setting –registry-identity $APPS_IDENTITY_ID.
APP_NAME=api-gateway cp -f ../tools/Dockerfile ./spring-petclinic-$APP_NAME/Dockerfile az containerapp create \ --name $APP_NAME \ --resource-group $RESOURCE_GROUP \ --environment $ACA_ENVIRONMENT \ --source ./spring-petclinic-$APP_NAME \ --registry-server $MYACR.azurecr.io \ --registry-identity $APPS_IDENTITY_ID \ --ingress external \ --target-port 8080 \ --min-replicas 1 \ --bind $JAVA_CONFIG_COMP_NAME $JAVA_EUREKA_COMP_NAME \ --runtime java
-
After the provisioning process finishes, you can create the other applications —
customers-service
,vets-service
, andvisits-service
.To simplify this process, use the script ../tools/create-apps.sh:
export RESOURCE_GROUP ACA_ENVIRONMENT MYACR APPS_IDENTITY_ID JAVA_CONFIG_COMP_NAME JAVA_EUREKA_COMP_NAME MYSQL_SERVER_NAME MYSQL_ADMIN_USERNAME MYSQL_ADMIN_PASSWORD ../tools/create-apps.sh
The script ../tools/create-apps.sh includes all the steps to create the target apps, and it creates them in parallel to save the lab execution time. For more details, review the script file’s contents in a code editor.
(Optional) If you encounter errors when running this script, you can try to manually create the apps, one by one. Click here for the CLI commands you can use to do this.
PROFILE=mysql APP_NAME=customers-service cp -f ../tools/Dockerfile ./spring-petclinic-$APP_NAME/Dockerfile az containerapp create \ --name $APP_NAME \ --resource-group $RESOURCE_GROUP \ --environment $ACA_ENVIRONMENT \ --source ./spring-petclinic-$APP_NAME \ --registry-server $MYACR.azurecr.io \ --registry-identity $APPS_IDENTITY_ID \ --ingress internal \ --target-port 8080 \ --min-replicas 1 \ --env-vars SQL_SERVER=$MYSQL_SERVER_NAME SQL_USER=$MYSQL_ADMIN_USERNAME SQL_PASSWORD=secretref:sql-password SPRING_PROFILES_ACTIVE=$PROFILE \ --secrets "sql-password=$MYSQL_ADMIN_PASSWORD" \ --bind $JAVA_CONFIG_COMP_NAME $JAVA_EUREKA_COMP_NAME \ --runtime java APP_NAME=vets-service cp -f ../tools/Dockerfile ./spring-petclinic-$APP_NAME/Dockerfile az containerapp create \ --name $APP_NAME \ --resource-group $RESOURCE_GROUP \ --environment $ACA_ENVIRONMENT \ --source ./spring-petclinic-$APP_NAME \ --registry-server $MYACR.azurecr.io \ --registry-identity $APPS_IDENTITY_ID \ --ingress internal \ --target-port 8080 \ --min-replicas 1 \ --env-vars SQL_SERVER=$MYSQL_SERVER_NAME SQL_USER=$MYSQL_ADMIN_USERNAME SQL_PASSWORD=secretref:sql-password SPRING_PROFILES_ACTIVE=$PROFILE \ --secrets "sql-password=$MYSQL_ADMIN_PASSWORD" \ --bind $JAVA_CONFIG_COMP_NAME $JAVA_EUREKA_COMP_NAME \ --runtime java APP_NAME=visits-service cp -f ../tools/Dockerfile ./spring-petclinic-$APP_NAME/Dockerfile az containerapp create \ --name $APP_NAME \ --resource-group $RESOURCE_GROUP \ --environment $ACA_ENVIRONMENT \ --source ./spring-petclinic-$APP_NAME \ --registry-server $MYACR.azurecr.io \ --registry-identity $APPS_IDENTITY_ID \ --ingress internal \ --target-port 8080 \ --min-replicas 1 \ --env-vars SQL_SERVER=$MYSQL_SERVER_NAME SQL_USER=$MYSQL_ADMIN_USERNAME SQL_PASSWORD=secretref:sql-password SPRING_PROFILES_ACTIVE=$PROFILE \ --secrets "sql-password=$MYSQL_ADMIN_PASSWORD" \ --bind $JAVA_CONFIG_COMP_NAME $JAVA_EUREKA_COMP_NAME \ --runtime java
Here’s some more information about these back-end applications that you may find useful:
- The back-end applications don’t have external endpoints, so you’ll create the container apps with the setting
--ingress internal
. -
The back-end applications will connect to the MySQL database. For more details, check the application-mysql.yml profile. We’ll add the environment variable
SPRING_PROFILES_ACTIVE=mysql
to each app.spring: datasource: url: jdbc:mysql://${SQL_SERVER}.mysql.database.azure.com:3306/petclinic?useSSL=true username: ${SQL_USER:sqladmin} password: ${SQL_PASSWORD:} sql: init: schema-locations: classpath*:db/mysql/schema.sql data-locations: classpath*:db/mysql/data.sql mode: ALWAYS
- To set the variables for the MySQL connection, we put the password into secrets and put the server name and username in environment variables (refer to Manage secrets in Azure Container Apps).
- The back-end applications don’t have external endpoints, so you’ll create the container apps with the setting