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 |
|
Enables the Parameter client |
No |
|
|
Name of your application |
No |
Value of the |
|
Active profile |
No |
Value of the |
|
Google Cloud project ID where the Google Parameter Manager is hosted |
No |
|
|
OAuth2 credentials for authenticating with the Google Parameter Manager |
No |
|
|
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
-
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, ifspring.application.nameismyappandspring.profiles.activeisprod, the parameter should be calledmyappand version should be calledprod.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").
-
Configure your
bootstrap.propertiesfile with your application’s configuration data:spring.application.name=myapp spring.profiles.active=prod -
Add the
@ConfigurationPropertiesannotation 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.
-
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") } -
Add
@RefreshScopeto your Spring configuration class to have parameters be reloadable at runtime. -
Add
management.endpoints.web.exposure.include=refreshto yourapplication.propertiesto allow unrestricted access to/actuator/refresh. -
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\"}" -
Send a POST request to the refresh endpoint:
$ curl -XPOST https://myapp.host.com/actuator/refresh
Sample
A sample application is available.