Cloud Parameter Manager

Spring Framework on Google Cloud makes it possible to use the Google Parameter Manager as a Spring Cloud Parameter server to remotely store your application configuration data.

The Spring Framework on Google Cloud Parameter support is provided via its own Spring Boot starter. It enables the use of the Google Parameter Manager as a source for Spring Boot configuration properties.

Maven coordinates:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-parameter</artifactId>
    <version>7.1.0</version>
</dependency>

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.cloud',
    name: 'spring-cloud-gcp-starter-parameter',
    version: '7.1.0'
}

Configuration

The following parameters are configurable in Spring Framework on Google Cloud Parameter Manager:

Name

Description

Required

Default value

spring.cloud.gcp.parameter.enabled

Enables the Parameter client

No

false

spring.cloud.gcp.parameter.name

Name of your application

No

Value of the spring.application.name property. If none, application

spring.cloud.gcp.parameter.profile

Active profile

No

Value of the spring.profiles.active property. If more than a single profile, last one is chosen

spring.cloud.gcp.parameter.project-id

Google Cloud project ID where the Google Parameter Manager is hosted

No

spring.cloud.gcp.parameter.credentials.location

OAuth2 credentials for authenticating with the Google Parameter Manager

No

spring.cloud.gcp.parameter.credentials.encoded-key

Base64-encoded OAuth2 credentials for authenticating with the Google Parameter Manager

No

These properties should be specified in a bootstrap.yml/bootstrap.properties file, rather than the usual applications.yml/application.properties.
Core properties, as described in Spring Framework on Google Cloud Core Module, do not apply to Spring Framework on Google Cloud Parameter Manager.

Quick start

  1. Create a parameter in the Google Parameter Manager that is called ${spring.application.name}. Create a parameter version called ${spring.profiles.active}. In other words, if spring.application.name is myapp and spring.profiles.active is prod, the parameter should be called myapp and version should be called prod.

    In order to do that, you should have the Google Cloud SDK installed, own a Google Cloud Project and run the following command:

    $ gcloud init # if this is your first Google Cloud SDK run.
    
    $ gcloud parametermanager parameters create myapp --location=global --parameter-format=JSON
    
    $ gcloud parametermanager parameters versions create prod --parameter=myapp --location=global --payload-data="{\"myapp.username\":\"test-user\",\"myapp.password\":\"test-password\"}"

In the parameter version payload, myapp.username and myapp.password correspond to properties where myapp is the prefix specified in the @ConfigurationProperties annotation. For example: @ConfigurationProperties("myapp").

  1. Configure your bootstrap.properties file with your application’s configuration data:

    spring.application.name=myapp
    spring.profiles.active=prod
  2. Add the @ConfigurationProperties annotation to a Spring-managed bean:

    @Component
    @ConfigurationProperties("myapp")
    public class MyParamAppProperties {
    
      private String username;
    
      private String password;
    
      public String getUsername() {
        return username;
      }
    
      public void setUsername(String username) {
        this.username = username;
      }
    
      public String getPassword() {
        return password;
      }
    
      public void setPassword(String password) {
        this.password = password;
      }
    }

When your Spring application starts, the username field value will be set to test-user and password field value will be test-password for the above MyParameterAppProperties bean.

Refreshing the configuration at runtime

Spring Cloud provides support to have configuration parameters be reloadable with the POST request to /actuator/refresh endpoint.

  1. Add the Spring Boot Actuator dependency:

    Maven coordinates:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    Gradle coordinates:

    dependencies {
        implementation("org.springframework.boot:spring-boot-starter-actuator")
    }
  2. Add @RefreshScope to your Spring configuration class to have parameters be reloadable at runtime.

  3. Add management.endpoints.web.exposure.include=refresh to your application.properties to allow unrestricted access to /actuator/refresh.

  4. Update a property with gcloud. For Parameter Manager, need to delete and create the version:

    $ gcloud parametermanager parameters versions delete prod --parameter=myapp --location=global
    
    $ gcloud parametermanager parameters versions create prod --parameter=myapp --location=global --payload-data="{\"username\":\"test-user\",\"password\":\"test-password-refreshed\"}"
  5. Send a POST request to the refresh endpoint:

    $ curl -XPOST https://myapp.host.com/actuator/refresh

Sample

A sample application is available.