Note
This sample project applies to Azure Spring Apps standard # plan.
As Config Server is a protected resource in Azure Spring Apps service, the client from outside needs to customize the request when using Config Data or Bootstrap. Since Spring Boot 2.4, the latter approach has been deprecated. This sample shows how to custom RestTemplate using Config Data and send Bearer token in the Authorization
header to access Config Server.
Clone the sample repository to your develop environment.
git clone https://github.com/Azure-Samples/azure-spring-apps-samples
cd azure-spring-apps-samples/custom-config-server-client
Please reference doc to provision Azure Spring Apps instance: https://learn.microsoft.com/azure/spring-apps/quickstart?pivots=sc-standard
Create environment variables file setup-env-variables.sh
based on template.
cp setup-env-variables-template.sh setup-env-variables.sh
Update below resource information in setup-env-variables.sh
.
export SUBSCRIPTION='subscription-id' # replace it with your subscription-id
export RESOURCE_GROUP='resource-group-name' # existing resource group or one that will be created in next steps
export SPRING_APPS_SERVICE='azure-spring-apps-name' # name of the service that will be created in the next steps
Source setting.
source ./setup-env-variables.sh
Update default subscription.
az account set --subscription ${SUBSCRIPTION}
# Install Azure CLI extension for Azure Spring Apps
az extension add --name spring
# Create an instance of Azure Spring Apps
az spring create -g ${RESOURCE_GROUP} -n ${SPRING_APPS_SERVICE}
# Using piggymetrics-config as the backend git repo from which Azure Spring Apps to pull config
az spring config-server git set -g ${RESOURCE_GROUP} -n ${SPRING_APPS_SERVICE} --uri "https://github.com/Azure-Samples/piggymetrics-config.git"
- Create a service principal to access the Config Server in your application. Please mark down
# Get resource id of service instance
RESOURCE_ID=$(az spring show -g ${RESOURCE_GROUP} -n ${SPRING_APPS_SERVICE} --query id -o tsv)
# Create service principal and create role assignment
az ad sp create-for-rbac --scopes ${RESOURCE_ID} --role "Azure Spring Cloud Config Server Reader"
The output includes credentials you will need in the next step.
- Update the
application.properties
undersrc/main/resources
with the credentials you get above and your Azure Spring Apps instance.spring.config.import=configserver:https://<service name>.svc.asc-test.net/config access.token.clientId=<appId> access.token.secret=<password> access.token.tenantId=<tenant>
mvn clean package -D skipTests
mvn spring-boot:run
# Get output of config and check its value
curl http://127.0.0.1:8080/config