10. Spring Cloud Config

Spring Cloud GCP makes it possible to use the Google Runtime Configuration API as a Spring Cloud Config server to remotely store your application configuration data.

The Spring Cloud GCP Config support is provided via its own Spring Boot starter. It enables the use of the Google Runtime Configuration API as a source for Spring Boot configuration properties.

Maven coordinates, using Spring Cloud GCP BOM:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-config</artifactId>
</dependency>

Gradle coordinates:

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

10.1 Configuration

The following parameters are configurable in Spring Cloud GCP Config:

Name

Description

Optional

Default value

spring.cloud.gcp.config.enabled

Enables the Config client

Yes

false

spring.cloud.gcp.config.name

Name of your application

No

 

spring.cloud.gcp.config.profile

Configuration’s Spring profile (e.g., prod)

Yes

default

spring.cloud.gcp.config.timeout-millis

Timeout in milliseconds for connecting to the Google Runtime Configuration API

Yes

60000

spring.cloud.gcp.config.project-id

GCP project ID where the Google Runtime Configuration API is hosted

Yes

 

spring.cloud.gcp.config.credentials.location

OAuth2 credentials for authenticating with the Google Runtime Configuration API

Yes

 

spring.cloud.gcp.config.credentials.scopes

OAuth2 scope for Spring Cloud GCP Config credentials

Yes

https://www.googleapis.com/auth/cloudruntimeconfig

[Note]Note

These properties should be specified in a bootstrap.yml/bootstrap.properties file, rather than the usual applications.yml/application.properties.

[Note]Note

Also note that core properties as described in Spring Cloud GCP Core Module do not apply to Spring Cloud GCP Config.

10.2 Quick start

  1. Create a configuration in the Google Runtime Configuration API that is called ${spring.cloud.gcp.config.name}_${spring.cloud.gcp.config.profile}. In other words, if spring.cloud.gcp.config.name is myapp and spring.cloud.gcp.config.profile is prod, the configuration should be called myapp_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 beta runtime-config configs create myapp_prod
    gcloud beta runtime-config configs variables set myapp.queue-size 25 --config-name myapp_prod
  2. Configure your bootstrap.properties file with your application’s configuration data:

    spring.cloud.gcp.config.name=myapp
    spring.cloud.gcp.config.profile=prod
  3. Add the @ConfigurationProperties annotation to a Spring-managed bean:

    @Component
    @ConfigurationProperties("myapp")
    public class SampleConfig {
    
      private int queueSize;
    
      public int getQueueSize() {
        return this.queueSize;
      }
    
      public void setQueueSize(int queueSize) {
        this.queueSize = queueSize;
      }
    }

When your Spring application starts, the queueSize field value will be set to 25 for the above SampleConfig bean.

10.3 Refreshing the configuration at runtime

Spring Boot Actuator enables a /refresh endpoint in your application that can be used to refresh the values of all the Spring Cloud Config-managed variables.

To achieve that, add the Spring Boot Actuator dependency.

Maven coordinates:

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

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
}

Add a @RefreshScope annotation to your class(es) containing remote configuration properties. Then, if you change the value of the myapp.queue-size variable in the myapp_prod configuration and hit the /refresh endpoint of your application, you can verify that the value of the queueSize field has been updated.

[Note]Note

If you’re developing locally or just not using authentication in your application, you should add management.security.enabled=false to your application.properties file to allow unrestricted access to the /refresh endpoint.