Spring Cloud GCP Reference Documentation

João André Martins

Table of Contents

1. Introduction
2. Dependency Management
3. Spring Cloud GCP Core
3.1. Project ID
3.2. Credentials
3.2.1. Scopes
4. Spring Cloud GCP for Pub/Sub
4.1. Pub/Sub operations abstraction
4.1.1. Publishing to a topic
4.2. Pub/Sub management
4.2.1. Creating a topic
4.2.2. Deleting a topic
4.2.3. Listing topics
4.2.4. Creating a subscription
4.2.5. Deleting a subscription
4.2.6. Listing subscriptions
4.3. Configuration
5. Spring Resources
5.1. Google Cloud Storage
5.2. Configuration
6. Spring JDBC
6.1. Prerequisites
6.2. Spring Boot Starter for Google Cloud SQL
6.2.1. DataSource creation flow
7. Spring Integration
7.1. Channel Adapters for Google Cloud Pub/Sub
7.1.1. Inbound channel adapter
7.1.2. Outbound channel adapter
8. Spring Cloud Sleuth
8.1. Spring Boot Starter for Stackdriver Trace
9. Spring Cloud Config
9.1. Configuration
9.2. Quick start
9.3. Refreshing the configuration at runtime

1. Introduction

The Spring Cloud GCP project aims at making the Spring Framework a first-class citizen of Google Cloud Platform (GCP).

Currently, Spring Cloud GCP lets you leverage the power and simplicity of the Spring framework to:

  1. Publish and subscribe from Google Cloud Pub/Sub topics
  2. Exchange messages with Spring Integration using Google Cloud Pub/Sub on the background
  3. Write and read from Spring Resources backed up by Google Cloud Storage
  4. Configure Spring JDBC with a few properties to use Google Cloud SQL on the background
  5. Trace the execution of your app with Spring Cloud Sleuth and Google Stackdriver Trace
  6. Configure your app with Spring Cloud Config, backed up by the Google Runtime Configuration API

2. Dependency Management

The Spring Cloud GCP Bill of Materials (BOM) contains the versions of all the dependencies it uses.

If you’re a Maven user, adding the following to your pom.xml file will allow you to not specify any Spring Cloud GCP dependency versions. Instead, the version of the BOM you’re using determines the versions of the used dependencies.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-dependencies</artifactId>
            <version>1.0.0.BUILD-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

In the following sections, the Maven dependency snippets will assume you are using the Spring Cloud GCP BOM and the dependency versions are omitted.

Gradle users can achieve the same kind of BOM experience using Spring’s dependency-management-plugin Gradle plugin.

3. Spring Cloud GCP Core

At the center of every Spring Cloud GCP module are the concepts of GcpProjectIdProvider and CredentialsProvider.

Spring Cloud GCP provides a Spring Boot starter to auto-configure the core components.

Maven coordinates, using Spring Cloud GCP BOM:

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

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-core', version: '1.0.0.BUILD-SNAPSHOT'
}

3.1 Project ID

GcpProjectIdProvider is a functional interface that returns a GCP project ID string.

public interface GcpProjectIdProvider {
	String getProjectId();
}

The Spring Boot GCP Core starter auto-configures a GcpProjectIdProvider. If a spring.cloud.gcp.project-id property is specified, the provided GcpProjectIdProvider returns that property value.

spring.cloud.gcp.project-id=my-gcp-project-id

Otherwise, the project ID is discovered based on a set of rules:

  1. The project ID specified by the GOOGLE_CLOUD_PROJECT environment variable
  2. The Google App Engine project ID
  3. The project ID specified in the JSON credentials file pointed by the GOOGLE_APPLICATION_CREDENTIALS environment variable
  4. The Google Cloud SDK project ID
  5. The Google Compute Engine project ID, from the Google Compute Engine Metadata Server

3.2 Credentials

CredentialsProvider is a functional interface that returns the credentials to authenticate and authorize calls to Google Cloud Client Libraries.

public interface CredentialsProvider {
  Credentials getCredentials() throws IOException;
}

The Spring Boot GCP Core starter auto-configures a CredentialsProvider. It uses the spring.cloud.gcp.credentials.location property to locate the OAuth2 private key of a Google service account. Keep in mind this property is a Spring Resource, so the credentials file can be obtained from a number of different locations such as the file system, classpath, URL, etc. The next example specifies the credentials location property in the file system.

spring.cloud.gcp.credentials.location=file:/usr/local/key.json

If that property isn’t specified, the starter tries to discover credentials from a number of places:

  1. Credentials file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
  2. Credentials provided by the Google Cloud SDK gcloud auth application-default login command
  3. Google App Engine built-in credentials
  4. Google Cloud Shell built-in credentials
  5. Google Compute Engine built-in credentials

3.2.1 Scopes

By default, the credentials provided by the Spring Cloud GCP Core Starter contain scopes for every service supported by Spring Cloud GCP.

The Spring Boot GCP Core starter allows you to configure a custom scope list for the provided credentials. To do that, specify a comma-delimited list of scopes in the spring.cloud.gcp.credentials.scopes property.

spring.cloud.gcp.credentials.scopes=https://www.googleapis.com/auth/pubsub,https://www.googleapis.com/auth/sqlservice.admin

4. Spring Cloud GCP for Pub/Sub

Spring Cloud GCP provides a Spring abstraction layer to publish to and subscribe from Google Cloud Pub/Sub topics and to create, list or delete Google Cloud Pub/Sub topics and subscriptions.

A Spring Boot starter is provided to auto-configure the various required Pub/Sub components.

Maven coordinates, using Spring Cloud GCP BOM:

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

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-pubsub', version: '1.0.0.BUILD-SNAPSHOT'
}

4.1 Pub/Sub operations abstraction

PubSubOperations is an abstraction that allows Spring users to use Google Cloud Pub/Sub without depending on any Google Cloud Pub/Sub API semantics. It provides the common set of operations needed to interact with Google Cloud Pub/Sub. PubSubTemplate is the default implementation of PubSubOperations and it uses the Google Cloud Java Client for Pub/Sub to interact with Google Cloud Pub/Sub.

PubSubTemplate depends on a PublisherFactory, which is a functional interface to provide a Google Cloud Java Client for Pub/Sub Publisher. The Spring Boot starter for GCP Pub/Sub auto-configures a PublisherFactory with default settings and uses the GcpProjectIdProvider and CredentialsProvider auto-configured by the Spring Boot GCP Core starter.

The PublisherFactory implementation provided by Spring Cloud GCP Pub/Sub, DefaultPublisherFactory, caches Publisher instances by topic name, in order to optimize resource utilization.

4.1.1 Publishing to a topic

PubSubTemplate provides asynchronous methods to publish messages to a Google Cloud Pub/Sub topic. It supports different types of payloads, including Strings with different encodings, byte[], ByteString and PubsubMessage.

public ListenableFuture<String> publish(String topic, String payload, Map<String, String> headers)

public ListenableFuture<String> publish(String topic, String payload, Map<String, String> headers,
Charset charset)

public ListenableFuture<String> publish(String topic, byte[] payload, Map<String, String> headers)

public ListenableFuture<String> publish(String topic, ByteString payload, Map<String, String> headers)

public ListenableFuture<String> publish(String topic, PubsubMessage pubsubMessage)

Here is an example of how to publish a message to a Google Cloud Pub/Sub topic:

@Autowired
private PubSubTemplate pubSubTemplate;

public void publishMessage() {
    this.pubSubTemplate.publish("topic", "your message payload", ImmutableMap.of("key1", "val1"));
}

4.2 Pub/Sub management

PubSubAdmin is the abstraction provided by Spring Cloud GCP to manage Google Cloud Pub/Sub resources. It allows for the creation, deletion and listing of topics and subscriptions.

PubSubAdmin depends on GcpProjectIdProvider and either a CredentialsProvider or a TopicAdminClient and a SubscriptionAdminClient. If given a CredentialsProvider, it creates a TopicAdminClient and a SubscriptionAdminClient with the Google Cloud Java Library for Pub/Sub default settings. The Spring Boot starter for GCP Pub/Sub auto-configures a PubSubAdmin object using the GcpProjectIdProvider and the CredentialsProvider auto-configured by the Spring Boot GCP Core starter.

4.2.1 Creating a topic

PubSubAdmin implements a method to create topics:

public Topic createTopic(String topicName)

Here is an example of how to create a Google Cloud Pub/Sub topic:

@Autowired
private PubSubAdmin admin;

public void newTopic() {
    admin.createTopic("topicName");
}

4.2.2 Deleting a topic

PubSubAdmin implements a method to delete topics:

public void deleteTopic(String topicName)

Here is an example of how to delete a Google Cloud Pub/Sub topic:

@Autowired
private PubSubAdmin admin;

public void deleteTopic() {
    admin.deleteTopic("topicName");
}

4.2.3 Listing topics

PubSubAdmin implements a method to list topics:

public List<Topic> listTopics

Here is an example of how to list every Google Cloud Pub/Sub topic name in a project:

@Autowired
private PubSubAdmin admin;

public List<String> listTopics() {
    return admin
        .listTopics()
        .stream()
        .map(Topic::getNameAsTopicName)
        .map(TopicName::getTopic)
        .collect(Collectors.toList());
}

4.2.4 Creating a subscription

PubSubAdmin implements a method to create subscriptions to existing topics:

public Subscription createSubscription(String subscriptionName, String topicName, Integer ackDeadline, String pushEndpoint)

Here is an example of how to create a Google Cloud Pub/Sub subscription:

@Autowired
private PubSubAdmin admin;

public void newSubscription() {
    admin.createSubscription("subscriptionName", "topicName", 10, “http://my.endpoint/push”);
}

Alternative methods with default settings are provided for ease of use. The default value for ackDeadline is 10 seconds. If pushEndpoint isn’t specified, the subscription uses message pulling, instead.

public Subscription createSubscription(String subscriptionName, String topicName)
public Subscription createSubscription(String subscriptionName, String topicName, Integer ackDeadline)
public Subscription createSubscription(String subscriptionName, String topicName, String pushEndpoint)

4.2.5 Deleting a subscription

PubSubAdmin implements a method to delete subscriptions:

public void deleteSubscription(String subscriptionName)

Here is an example of how to delete a Google Cloud Pub/Sub subscription:

@Autowired
private PubSubAdmin admin;

public void deleteSubscription() {
    admin.deleteSubscription("subscriptionName");
}

4.2.6 Listing subscriptions

PubSubAdmin implements a method to list subscriptions:

public List<Subscription> listSubscriptions()

Here is an example of how to list every subscription name in a project:

@Autowired
private PubSubAdmin admin;

public List<String> listSubscriptions() {
return admin
    .listSubscriptions()
    .stream()
    .map(Subscription::getNameAsSubscriptionName)
    .map(SubscriptionName::getSubscription)
    .collect(Collectors.toList());
}

4.3 Configuration

The Spring Boot starter for Google Cloud Pub/Sub provides the following configuration options:

Name

Description

Optional

Default value

spring.cloud.gcp.pubsub.subscriber-executor-threads

Number of threads used by Subscriber instances created by SubscriberFactory

Yes

4

spring.cloud.gcp.pubsub.publisher-executor-threads

Number of threads used by Publisher instances created by PublisherFactory

Yes

4

spring.cloud.gcp.pubsub.project-id

GCP project ID where the Google Cloud Pub/Sub API is hosted, if different from the one in the Spring Cloud GCP Core Module

Yes

 

spring.cloud.gcp.pubsub.credentials.location

OAuth2 credentials for authenticating with the Google Cloud Pub/Sub API, if different from the ones in the Spring Cloud GCP Core Module

Yes

 

spring.cloud.gcp.pubsub.credentials.scopes

OAuth2 scope for Spring Cloud GCP Config credentials

Yes

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

5. Spring Resources

Spring Resources are an abstraction for a number of low-level resources, such as file system files, classpath files, servlet context-relative files, etc. Spring Cloud GCP adds a new resource type: a Google Cloud Storage (GCS) object.

A Spring Boot starter is provided to auto-configure the various Storage components.

Maven coordinates, using Spring Cloud GCP BOM:

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

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-storage', version: '1.0.0.BUILD-SNAPSHOT'
}

5.1 Google Cloud Storage

The Spring Resource Abstraction for Google Cloud Storage allows GCS objects to be accessed by their GCS URL:

@Value("gs://[YOUR_GCS_BUCKET]/[GCS_FILE_NAME]")
private Resource gcsResource;

This creates a Resource object that can be used to read the object, among other possible operations.

It is also possible to write to a Resource, although a cast to WriteableResource is required.

try (OutputStream os = ((WritableResource) gcsResource).getOutputStream()) {
  os.write("foo".getBytes());
}

The Spring Boot Starter for Google Cloud Storage auto-configures the Storage bean required by the spring-cloud-gcp-storage module, based on the CredentialsProvider provided by the Spring Boot GCP Core starter.

5.2 Configuration

The Spring Boot Starter for Google Cloud Storage provides the following configuration options:

Name

Description

Optional

Default value

spring.cloud.gcp.storage.auto-create-files

Creates files on Google Cloud Storage when writes are made to non-existent files

Yes

true

spring.cloud.gcp.storage.credentials.location

OAuth2 credentials for authenticating with the Google Cloud Storage API, if different from the ones in the Spring Cloud GCP Core Module

Yes

 

spring.cloud.gcp.storage.credentials.scopes

OAuth2 scope for Spring Cloud GCP Config credentials

Yes

https://www.googleapis.com/auth/devstorage.read_write

6. Spring JDBC

Spring Cloud GCP adds integrations with Spring JDBC so you can run your MySQL or PostgreSQL databases in Google Cloud SQL using Spring JDBC, or other libraries that depend on it like Spring Data JPA.

The Cloud SQL support provided by Spring Cloud GCP is in the form of a Spring Boot starter. The role of the starter is to read configuration from properties and assume default settings so that user experience connecting to MySQL is as easy as possible.

Maven coordinates, using Spring Cloud GCP BOM:

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

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-sql', version: '1.0.0.BUILD-SNAPSHOT'
}

6.1 Prerequisites

In order to use the Spring Boot Starter for Google Cloud SQL features, the Google Cloud SQL API must be enabled in your GCP project.

To do that, go to the API library page of the Google Cloud Console, search for "Cloud SQL API", click the first result and enable the API.

[Note]Note

There are several similar "Cloud SQL" results. You must access the "Google Cloud SQL API" one and enable the API from there.

6.2 Spring Boot Starter for Google Cloud SQL

The Spring Boot Starter for Google Cloud SQL provides an auto-configured DataSource object. Coupled with Spring JDBC, it provides a JdbcTemplate object bean that allows for operations such as querying and modifying a database.

Note that this starter requires the Google Cloud SQL API to be enabled in in your GCP project.

@Autowired
private JdbcTemplate jdbcTemplate;

public List<Map<String, Object>> listUsers() {
    return jdbcTemplate.queryForList("SELECT * FROM user;");
}

This starter is configured through DataSourceProperties. Properties like the SQL username, spring.datasource.username, and password, spring.datasource.password, should be configured via the Spring Boot Datasource configuration. There is also some configuration specific to Google Cloud SQL:

Property name

Description

Default value

Unused if specified property(ies)

spring.cloud.gcp.sql.database-type

The type of database used. Can either be mysql or postgresql.

mysql

 

spring.cloud.gcp.sql.database-name

Name of the database to connect to.

 

spring.datasource.url

spring.cloud.gcp.sql.instance-connection-name

A string containing a Google Cloud SQL instance’s project ID, region and name, each separated by a colon. For example, my-project-id:my-region:my-instance-name.

 

spring.datasource.url

spring.cloud.gcp.sql.credentials.location

File system path to the Google OAuth2 credentials private key file. Used to authenticate and authorize new connections to a Google Cloud SQL instance.

Default credentials provided by the Spring GCP Core Boot starter

 

6.2.1 DataSource creation flow

Based on the previous properties, the Spring Boot starter for Google Cloud SQL creates a CloudSqlJdbcInfoProvider object which is used to obtain an instance’s JDBC URL and driver class name. If you provide your own CloudSqlJdbcInfoProvider bean, it is used instead and the properties related to building the JDBC URL or driver class are ignored.

It is in the CloudSqlJdbcInfoProvider creation step that the credentials factory is registered in a system property to be SqlCredentialFactory. This is especially relevant if you want to provide your own CloudSqlJdbcInfoProvider bean, in which case you should also register the credentials factory class name under the cloudSql.socketFactory.credentialFactory system property. Otherwise, the application default credentials are used by default to connect to Google Cloud SQL.

DataSource creation is delegated to Spring Boot. You can select the type of connection pool (e.g., Tomcat, HikariCP, etc.) by adding their dependency to the classpath. Prior to DataSource creation, if no JDBC URL and/or driver class name are configured, they are resolved by a CloudSqlJdbcInfoProvider and applied to the DataSource configuration.

Using the created DataSource in conjunction with Spring JDBC provides you with a fully configured and operational JdbcTemplate object that you can use to interact with your SQL database. You can connect to your database with as little as a database and instance names!

7. Spring Integration

Spring Cloud GCP integrates with Spring Integration by providing channel adapters that connect your Spring MessageChannels to Google Cloud Pub/Sub. This enables messaging between different processes, applications or micro-services backed up by Google Cloud Pub/Sub.

Maven coordinates, using Spring Cloud GCP BOM:

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

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-integration-gcp', version: '1.0.0.BUILD-SNAPSHOT'
}

7.1 Channel Adapters for Google Cloud Pub/Sub

7.1.1 Inbound channel adapter

PubSubInboundChannelAdapter is the inbound channel adapter for GCP Pub/Sub that listens to a GCP Pub/Sub subscription for new messages. It converts new messages to an internal Spring Message and then sends it to the bound output channel.

To use the inbound channel adapter, a PubSubInboundChannelAdapter must be provided and configured on the user application side.

@Bean
public MessageChannel pubsubInputChannel() {
    return new PublishSubscribeChannel();
}

@Bean
public PubSubInboundChannelAdapter messageChannelAdapter(
    @Qualifier("pubsubInputChannel") MessageChannel inputChannel,
    SubscriberFactory subscriberFactory) {
    PubSubInboundChannelAdapter adapter =
        new PubSubInboundChannelAdapter(subscriberFactory, "subscriptionName");
    adapter.setOutputChannel(inputChannel);
    adapter.setAckMode(AckMode.MANUAL);

    return adapter;
}

In the example, we first specify the MessageChannel where the adapter is going to write incoming messages to. The MessageChannel implementation isn’t important here. Depending on your use case, you might want to use a MessageChannel other than PublishSubscribeChannel.

Then, we declare a PubSubInboundChannelAdapter bean. It requires the channel we just created and a SubscriberFactory, which creates Subscriber objects from the Google Cloud Java Client for Pub/Sub. The Spring Boot starter for GCP Pub/Sub provides a configured SubscriberFactory.

It is also possible to set the message acknowledgement mode on the adapter, which is automatic by default. On automatic acking, a message is acked with GCP Pub/Sub if the adapter sent it to the channel and no exceptions were thrown. If a RuntimeException is thrown while the message is processed, then the message is nacked. On manual acking, the adapter attaches an AckReplyConsumer object to the Message headers, which users can extract using the GcpHeaders.ACKNOWLEDGEMENT key and use to (n)ack a message.

@Bean
@ServiceActivator(inputChannel = "pubsubInputChannel")
public MessageHandler messageReceiver() {
    return message -> {
        LOGGER.info("Message arrived! Payload: " + message.getPayload());
        AckReplyConsumer consumer =
              message.getHeaders().get(GcpHeaders.ACKNOWLEDGEMENT, AckReplyConsumer.class);
        consumer.ack();
    };
}

7.1.2 Outbound channel adapter

PubSubMessageHandler is the outbound channel adapter for GCP Pub/Sub that listens for new messages on a Spring MessageChannel. It uses PubSubTemplate to convert new Message instances to the GCP Pub/Sub format and post them to a GCP Pub/Sub topic.

To use the outbound channel adapter, a PubSubMessageHandler bean must be provided and configured on the user application side.

@Bean
@ServiceActivator(inputChannel = "pubsubOutputChannel")
public MessageHandler messageSender(PubSubTemplate pubsubTemplate) {
    return new PubSubMessageHandler(pubsubTemplate, "topicName");
}

The provided PubSubTemplate contains all the necessary configuration to publish messages to a GCP Pub/Sub topic.

PubSubMessageHandler publishes messages asynchronously by default. A publish timeout can be configured for synchronous publishing. If none is provided, the adapter waits indefinitely for a response.

It is possible to set user-defined callbacks for the publish() call in PubSubMessageHandler through the setPublishFutureCallback() method. These are useful to process the message ID, in case of success, or the error if any was thrown.

8. Spring Cloud Sleuth

Spring Cloud Sleuth is an instrumentation framework for Spring Boot applications. It captures trace informations and can forward trace traces to services like Zipkin for storage and analysis.

Google Cloud Platform provides its own managed distributed tracing service called Stackdriver Trace. Instead of running and maintaining your own Zipkin instance and storage, you can use Stackdriver Trace to store traces, view trace details, generate latency distributions graphs, and generate performance regression reports.

This Spring Cloud GCP starter can forward Spring Cloud Sleuth traces to Stackdriver Trace without an intermediary Zipkin server.

Maven coordinates, using Spring Cloud GCP BOM:

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

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-trace', version: '1.0.0.BUILD-SNAPSHOT'
}

You must enable Stackdriver Trace API from the Google Cloud Console in order to capture traces. Navigate to the Stackdriver Trace API for your project and make sure it’s enabled.

[Note]Note

If you are already using a Zipkin server capturing trace information from multiple platform/frameworks, you also use a Stackdriver Zipkin proxy to forward those traces to Stackdriver Trace without modifying existing applications.

8.1 Spring Boot Starter for Stackdriver Trace

Spring Boot Starter for Stackdriver Trace uses Spring Cloud Sleuth and auto-configures a SpanReporter that sends the Sleuth’s trace information to Stackdriver Trace.

This starter will send traces asynchronously using a buffered trace consumer that auto-flushes when its buffered trace messages exceed its buffer size or have been buffered for longer than its scheduled delay.

There are several parameters you can use to tune the Stackdriver Trace adapter. All configurations are optional:

Name

Description

Optional

Default value

spring.cloud.gcp.trace.enabled

Auto-configure Spring Cloud Sleuth to send traces to Stackdriver Trace.

Yes

true

spring.cloud.gcp.trace.executor-threads

Number of threads to use by the underlying gRPC channel to send the trace request to Stackdriver.

Yes

4

spring.cloud.gcp.trace.buffer-size-bytes

Buffer size in bytes. Traces will be flushed to Stackdriver when buffered trace messages exceed this size.

Yes

1% of Runtime.totalMemory()

spring.cloud.gcp.trace.scheduled-delay-seconds

Buffered trace messages will be flushed to Stackdriver when buffered longer than scheduled delays (in seconds) even if the buffered message size didn’t exceed the buffer size.

Yes

10

spring.cloud.gcp.trace.project-id

Overrides the project ID from the Spring Cloud GCP Core Module

Yes

 

spring.cloud.gcp.trace.credentials.location

Overrides the credentials location from the Spring Cloud GCP Core Module

Yes

 

spring.cloud.gcp.trace.credentials.scopes

Overrides the credentials scopes from the Spring Cloud GCP Core Module

Yes

 

You can use core Spring Cloud Sleuth properties to control Sleuth’s sampling rate, etc. Read Sleuth documentation for more information on Sleuth configurations.

For example, when you are testing to see the traces are going through, you can set the sampling rate to 100%.

spring.sleuth.sampler.percentage=1                      # Send 100% of the request traces to Stackdriver.
spring.sleuth.web.skipPattern=(^cleanup.*|.+favicon.*)  # Ignore some URL paths.

Stackdriver Trace requires the use of 128-bit Trace ID. This starter ignores spring.sleuth.traceId128 property and always uses 128-bit Trace ID.

9. 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 a Spring Boot starter that enables the use of the @Value annotation for injecting values stored in the Google Runtime Configuration API into fields of Spring-managed beans.

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', version: '1.0.0.BUILD-SNAPSHOT'
}

9.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

true

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

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, if different from the one in the Spring Cloud GCP Core Module

Yes

 

spring.cloud.gcp.config.credentials.location

OAuth2 credentials for authenticating with the Google Runtime Configuration API, if different from the ones in the Spring Cloud GCP Core Module

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.

9.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 queue_size 25 --config-name myapp_prod
  2. Configure your bootstrap.yml file with your application’s configuration data:

    spring.cloud.gcp.config.name=myapp
    spring.cloud.gcp.config.profile=prod
  3. Add a field annotated with @Value to a Spring-managed bean:

    @Component
    public class SampleConfig {
    
      @Value("${queue_size}")
      private String queueSize;
    
      public String getQueueSize() {
        return this.queueSize;
      }
    
      public void setQueueSize(String queueSize) {
        this.queueSize = queueSize;
      }
    }

When your Spring application starts, the queueSize field value will be set to 25 for any auto-wired instances of the above SampleConfig bean.

9.3 Refreshing the configuration at runtime

Using 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 a @RefreshScope annotation to your class(es) containing remote configuration properties. Then, if you change the value of myapp_prod’s `queue_size variable in the Google Runtime Configuration API and then hit the /refresh endpoint of your application, you can verify that the value of the queueSize field has been updated.