The Google Cloud Runtime Configurator allows you to dynamically configure and expose variables through Google Cloud Platform.

In this codelab, you will learn how to use the Runtime Configurator to manage configuration in your Spring Boot Application and dynamically make updates.

The Spring Cloud for GCP Config starter allows you to use Runtime Configurator configuration values in your application.

What you'll learn

What you'll need

How will you use use this tutorial?

Read it through only Read it and complete the exercises

How would you rate your experience with building HTML/CSS web apps?

Novice Intermediate Proficient

How would you rate your experience with using Google Cloud Platform services?

Novice Intermediate Proficient

Codelab-at-a-conference setup

If you see a "request account button" at the top of the main Codelabs window, click it to obtain a temporary account. Otherwise ask one of the staff for a coupon with username/password.

These temporary accounts have existing projects that are set up with billing so that there are no costs associated for you with running this codelab.

Note that all these accounts will be disabled soon after the codelab is over.

Use these credentials to log into the machine or to open a new Google Cloud Console window https://console.cloud.google.com/. Accept the new account Terms of Service and any updates to Terms of Service.

Here's what you should see once logged in:

When presented with this console landing page, please select the only project available. Alternatively, from the console home page, click on "Select a Project" :

First, enable the Runtime Config API. Click on Enable.

In Cloud Shell, use the command line to create a new runtime configuration:

$ gcloud beta runtime-config configs create myapp_prod

The configuration name is required to follow the format of "application-name"_"environment".

We will use this later in our Spring Boot application.

Now create a few configuration variables:

$ gcloud beta runtime-config configs variables set \
  queue_size 25 --config-name myapp_prod
$ gcloud beta runtime-config configs variables set \
  feature_x_enabled true --config-name myapp_prod

(Optional) List the configuration variables:

$ gcloud beta runtime-config configs variables list \
  --config-name myapp_prod

Generate a new Spring Boot application with Spring initializr:

$ curl https://start.spring.io/starter.tgz -d packaging=jar \
  -d dependencies=web -d baseDir=spring-config-example | tar -xzvf -
$ cd spring-config-example

Add configuration variables that map to the variables we created earlier in a new controller in DemoApplication.java. You can use vim,nano,or emacs to edit the file.

src/main/java/com/example/demo/DemoApplication.java

package com.example.demo;

...
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


@SpringBootApplication
@RestController
public class DemoApplication {
  @Value("${queue_size}")
  private int queueSize;
  @Value("${feature_x_enabled}")
  private boolean isFeatureXEnabled;
  @GetMapping("/")
  public String showConfiguration() {
    return String.valueOf("Queue size : " + queueSize + "/" + "X enabled: " + isFeatureXEnabled);
  }

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

}

Add the Spring Runtime configurator starter to your pom.xml:

You can use vim,nano,or emacs to edit the file.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
    ...
    <!-- Add Spring Cloud GCP Dependency BOM -->
    <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-dependencies</artifactId>
            <version>1.0.0.M4</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
      </dependencies>
    </dependencyManagement>
    <dependencies>
      ...
        <!-- Add Spring Cloud Config starter for Runtime Configuration -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-gcp-starter-config</artifactId>
      </dependency>
      ...
    </dependencies>
    <!-- Use Spring Milestone Repository -->
    <repositories>
      <repository>
        <id>repository.spring.milestone</id>
        <name>Spring Milestones Repository</name>
        <url>http://repo.spring.io/milestone</url>
      </repository>
    </repositories>
</project>

Create src/main/resources/bootstrap.properties with the following properties.

You can use vim,nano,or emacs to edit the file.

src/main/resources/bootstrap.properties

spring.cloud.gcp.config.enabled=true
spring.cloud.gcp.config.name=myapp
spring.cloud.gcp.config.profile=prod
$ export GOOGLE_CLOUD_PROJECT=`gcloud config list --format 'value(core.project)'`
$ ./mvnw spring-boot:run

Access the Web Preview on the top right of the cloud shell as shown here and preview on port 8080.

You should see the configuration values as shown here in your browser:

You will now add dynamic configuration support to this application.

Spring Boot Actuator starter adds several production grade services to your application with little effort on your part.

One of these services is the /actuator/refresh endpoint that allows reloading of Spring components that have been annotated with @RefreshScope.

Update Maven pom.xml

Add the Spring Boot Actuator starter to your pom.xml.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
  ...
    <dependencies>
      ...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
      ...
    </dependencies>
  ...
</project>

Add @RefreshScope to the Controller

src/main/java/com/example/demo/DemoApplication.java

...

import org.springframework.cloud.context.config.annotation.RefreshScope;

@SpringBootApplication
@RestController
@RefreshScope
public class DemoApplication {
  ...
}

Allow unrestricted access to /refresh for testing

By default, you need authentication to trigger the /refresh endpoint to reload the configuration. For the purpose of the lab, let's disable this security feature so you can test the configuration reload easily.

src/main/resources/application.properties

# To allow unrestricted access to /refresh for testing
management.endpoints.web.exposure.include=refresh

Run the Application Locally

Restart the Spring Boot application and verify that the Web Preview shows the current configuration values.

$ export GOOGLE_CLOUD_PROJECT=`gcloud config list --format 'value(core.project)'`
$ ./mvnw spring-boot:run

Update configuration values

Click on + to open a new Cloud Shell session so you don't need to terminate the server:

In the new Cloud Shell session, update the configuration values set earlier:

$ gcloud beta runtime-config configs variables set \
     queue_size 250 --config-name myapp_prod
$ gcloud beta runtime-config configs variables set \
     feature_x_enabled false --config-name myapp_prod

Refresh configuration

The ‘/refresh' endpoint allows you to dynamically reload the Spring components tagged with the

@RefreshScope.

$ curl -XPOST http://localhost:8080/actuator/refresh

Refresh the Web Preview in your browser. You should now see the updated configuration values.

In this step, you configured a Spring Boot web application to use the Runtime Configurator API to dynamically update the configuration of your Spring component.

Congratulations!

You learned how to dynamically configure your Spring Boot applications using the Spring Cloud Config Starter for GCP in your Spring Boot application!

Learn More

License

This work is licensed under a Creative Commons Attribution 2.0 Generic License.