This sample application demonstrates using the Spring Framework on Google Cloud Config Client Starter in your code.
The same content is also available in the Dynamic configuration for Spring Boot applications codelab.
-
Run
$ mvn clean installfrom the root directory of the project. -
Move to the config sample directory by running
$ cd spring-cloud-gcp-samples/spring-cloud-gcp-config-sample -
Checkout the code of this sample.
-
Enable the Google Runtime Configuration API.
-
Create a configuration using the Google Cloud SDK.
$ gcloud beta runtime-config configs create myapp_prod
Then set the variables that your application will load:
$ gcloud beta runtime-config configs variables set \ myapp.queue-size 25 \ --config-name myapp_prod $ gcloud beta runtime-config configs variables set \ myapp.feature-x-enabled true \ --config-name myapp_prod
-
Update src/main/resources/bootstrap.properties:
spring.cloud.gcp.config.enabled=true spring.cloud.gcp.config.name=myapp #default profile = default spring.cloud.gcp.config.profile=prod
-
Notice that MyAppProperties.java uses
@ConfigurationPropertiesto bind themyappnamespace from the environment. -
Note that in order to expose /actuator/refresh endpoint, application.properties file includes the following property:
management.endpoints.web.exposure.include=refresh
-
Run the application using the
$ mvn spring-boot:runcommand and verify the values are set appropriately at http://localhost:8080/ or use theWeb Previewbutton in Cloud Shell to preview the app on port 8080. -
Update a property with
gcloud:$ gcloud beta runtime-config configs variables set \ myapp.queue-size 200 \ --config-name myapp_prod $ gcloud beta runtime-config configs variables set \ myapp.feature-x-enabled false \ --config-name myapp_prod
-
Send a POST request to the refresh endpoint:
$ curl -XPOST http://localhost:8080/actuator/refresh
-
Verify the values are updated at http://localhost:8080/ or Cloud Shell
Web Preview.