Update application config
You now have all relevant components in place to switch to the secrets stored in Azure Key Vault and remove them from your config repo. To complete your configuration, you now need to set the config repository to reference the Azure Key Vault instance. You also need to update the pom.xml file to ensure that the visits, vets and customers services use the com.azure.spring:spring-cloud-azure-starter-keyvault-secrets
dependency. You can use the following guidance to accomplish this task:
Spring Cloud Azure Starter Key Vault Secrets Build a sample Spring Boot app with Spring Boot starter
Step by step guidance
-
From the Git Bash window, in the config repository you cloned locally, use your favorite text editor to open the
application.yml
file. Replace the contents of this file with the contents of this application.yml file. This file contains the following changes:- The spring.datasource properties are no longer there. These are now in your Key Vault and are no longer needed in the application.yml file.
- Line 25 to 32 contain new config for your Key Vault. Make sure you replace the
<your-kv-name>
placeholder on line 31 with the name of your Key Vault.
-
Save the file and commit and push these changes to your remote config repository.
git add . git commit -m 'added key vault' git push
Update, rebuild, and redeploy each app
-
From the Git Bash window, in the
java-microservices-asa-e-lab
repository you cloned locally, use your favorite text editor to open thepom.xml
files of the customers, visits and vets services (within thesrc/spring-petclinic-customers-service
,src/spring-petclinic-visits-service
, andsrc/spring-petclinic-vets-service
directories). For each, add the following dependencies (within the<dependencies>...</dependencies>
section) and save the change.<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-keyvault-secrets</artifactId> </dependency>
-
From the Git Bash window, in the
java-microservices-asa-e-lab
repository you cloned locally, use your favorite text editor to open thepom.xml
file in thesrc
directory of the cloned repo. Add to the file a dependency tocom.azure.spring
. This should be added within the<dependencyManagement><dependencies></dependencies></dependencyManagement>
section.<dependencyManagement> <dependencies> //... existing dependencies <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-dependencies</artifactId> <version>${version.spring.cloud.azure}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
In the same file, add a property for
version.spring.cloud.azure
. This should be added within the<properties></properties>
section.<version.spring.cloud.azure>5.2.0</version.spring.cloud.azure>
-
Save the changes to the
pom.xml
file and close it. -
Rebuild the services by running the following command in the root directory of the application.
cd ~/workspaces/java-microservices-asa-e-lab/src mvn clean package -DskipTests
-
Verify that the build succeeds by reviewing the output of the
mvn clean package -DskipTests
command, which should have the following format:[INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for spring-petclinic-microservices 3.0.2: [INFO] [INFO] spring-petclinic-microservices ..................... SUCCESS [ 0.249 s] [INFO] spring-petclinic-admin-server ...................... SUCCESS [ 16.123 s] [INFO] spring-petclinic-customers-service ................. SUCCESS [ 6.749 s] [INFO] spring-petclinic-vets-service ...................... SUCCESS [ 4.845 s] [INFO] spring-petclinic-visits-service .................... SUCCESS [ 5.063 s] [INFO] spring-petclinic-config-server ..................... SUCCESS [ 1.777 s] [INFO] spring-petclinic-discovery-server .................. SUCCESS [ 2.563 s] [INFO] spring-petclinic-api-gateway ....................... SUCCESS [ 15.582 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 55.901 s [INFO] Finished at: 2023-06-02T14:07:49Z [INFO] ------------------------------------------------------------------------
-
Redeploy the customers, visits and vets services to their respective apps in your Spring Apps service by running the following commands:
az spring app deploy --name ${CUSTOMERS_SERVICE} \ --config-file-patterns ${CUSTOMERS_SERVICE} \ --artifact-path ${CUSTOMERS_SERVICE_JAR} az spring app deploy --name ${VETS_SERVICE} \ --config-file-patterns ${VETS_SERVICE} \ --artifact-path ${VETS_SERVICE_JAR} az spring app deploy --name ${VISITS_SERVICE} \ --config-file-patterns ${VISITS_SERVICE} \ --artifact-path ${VISITS_SERVICE_JAR}
-
Retest your application through its public endpoint. Ensure that the application is functional, while the connection string secrets are retrieved from Azure Key Vault.
-
In case you don’t see data in your application, take a look at the
customers-service
logs to make sure the configuration gets picked up correctly and there are no errors on startup.az spring app logs --name ${CUSTOMERS_SERVICE} --follow
In case you see no errors, you can escape out of the log statement with
Ctrl+C
and you can proceed with the next steps. In case you see errors, review the steps you executed and retry. The LabTips file also contains steps on how to recover from errors. -
To verify that secrets from Key Vault are picked up, in the Azure Portal, navigate to the page of the Azure Key Vault instance you provisioned. On the Overview page, select the Monitoring tab and review the graph representing requests for access to the vault’s secrets.