35 lines
15 KiB
HTML
35 lines
15 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>162. Spring Cloud Config</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="multi_spring-cloud.html" title="Spring Cloud"><link rel="up" href="multi_spring-cloud-gcp-reference.html" title="Part XVIII. Spring Cloud GCP"><link rel="prev" href="multi__stackdriver_logging.html" title="161. Stackdriver Logging"><link rel="next" href="multi__spring_data_cloud_spanner.html" title="163. Spring Data Cloud Spanner"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">162. Spring Cloud Config</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__stackdriver_logging.html">Prev</a> </td><th width="60%" align="center">Part XVIII. Spring Cloud GCP</th><td width="20%" align="right"> <a accesskey="n" href="multi__spring_data_cloud_spanner.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="_spring_cloud_config_2" href="#_spring_cloud_config_2"></a>162. Spring Cloud Config</h2></div></div></div><p>Spring Cloud GCP makes it possible to use the <a class="link" href="https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/" target="_top">Google Runtime Configuration API</a> as a <a class="link" href="https://cloud.spring.io/spring-cloud-config/" target="_top">Spring Cloud Config</a> server to remotely store your application configuration data.</p><p>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.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>The Google Cloud Runtime Configuration service is in beta status.</p></td></tr></table></div><p>Maven coordinates, using Spring Cloud GCP BOM:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><dependency></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><groupId></span>org.springframework.cloud<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></groupId></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><artifactId></span>spring-cloud-gcp-starter-config<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></artifactId></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></dependency></span></pre><p>Gradle coordinates:</p><pre class="screen">dependencies {
|
|
compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-config'
|
|
}</pre><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_configuration_5" href="#_configuration_5"></a>162.1 Configuration</h2></div></div></div><p>The following parameters are configurable in Spring Cloud GCP Config:</p><div class="informaltable"><table class="informaltable" style="border-collapse: collapse;border-top: 1px solid ; border-bottom: 1px solid ; "><colgroup><col class="col_1"><col class="col_2"><col class="col_3"><col class="col_4"></colgroup><tbody><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Name</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Description</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Required</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p>Default value</p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.config.enabled</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Enables the Config client</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">false</code></p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.config.name</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Name of your application</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p>Value of the <code class="literal">spring.application.name</code> property.
|
|
If none, <code class="literal">application</code></p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.config.profile</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Active profile</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p>Value of the <code class="literal">spring.profiles.active</code> property.
|
|
If more than a single profile, last one is chosen</p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.config.timeout-millis</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Timeout in milliseconds for connecting to the Google Runtime Configuration API</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">60000</code></p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.config.project-id</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>GCP project ID where the Google Runtime Configuration API is hosted</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"> </td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.config.credentials.location</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>OAuth2 credentials for authenticating with the Google Runtime Configuration API</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"> </td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.config.credentials.encoded-key</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Base64-encoded OAuth2 credentials for authenticating with the Google Runtime Configuration API</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"> </td></tr><tr><td style="border-right: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.config.credentials.scopes</code></p></td><td style="border-right: 1px solid ; " align="left" valign="top"><p><a class="link" href="https://developers.google.com/identity/protocols/googlescopes" target="_top">OAuth2 scope</a> for Spring Cloud GCP Config credentials</p></td><td style="border-right: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="" align="left" valign="top"><p><a class="link" href="https://www.googleapis.com/auth/cloudruntimeconfig" target="_top">https://www.googleapis.com/auth/cloudruntimeconfig</a></p></td></tr></tbody></table></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>These properties should be specified in a <a class="link" href="https://cloud.spring.io/spring-cloud-static/spring-cloud.html#_the_bootstrap_application_context" target="_top"><code class="literal">bootstrap.yml</code>/<code class="literal">bootstrap.properties</code></a> file, rather than the usual <code class="literal">applications.yml</code>/<code class="literal">application.properties</code>.</p></td></tr></table></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Core properties, as described in <a class="link" href="multi_spring-cloud-gcp-core.html" title="154. Spring Cloud GCP Core">Spring Cloud GCP Core Module</a>, do not apply to Spring Cloud GCP Config.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_quick_start_6" href="#_quick_start_6"></a>162.2 Quick start</h2></div></div></div><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p class="simpara">Create a configuration in the Google Runtime Configuration API that is called <code class="literal">${spring.application.name}_${spring.profiles.active}</code>.
|
|
In other words, if <code class="literal">spring.application.name</code> is <code class="literal">myapp</code> and <code class="literal">spring.profiles.active</code> is <code class="literal">prod</code>, the configuration should be called <code class="literal">myapp_prod</code>.</p><p class="simpara">In order to do that, you should have the <a class="link" href="https://cloud.google.com/sdk/" target="_top">Google Cloud SDK</a> installed, own a Google Cloud Project and run the following command:</p></li></ol></div><pre class="screen">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</pre><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p class="simpara">Configure your <code class="literal">bootstrap.properties</code> file with your application’s configuration data:</p><pre class="screen">spring.application.name=myapp
|
|
spring.profiles.active=prod</pre></li><li class="listitem"><p class="simpara">Add the <code class="literal">@ConfigurationProperties</code> annotation to a Spring-managed bean:</p><pre class="screen">@Component
|
|
@ConfigurationProperties("myapp")
|
|
public class SampleConfig {
|
|
|
|
private int queueSize;
|
|
|
|
public int getQueueSize() {
|
|
return this.queueSize;
|
|
}
|
|
|
|
public void setQueueSize(int queueSize) {
|
|
this.queueSize = queueSize;
|
|
}
|
|
}</pre></li></ol></div><p>When your Spring application starts, the <code class="literal">queueSize</code> field value will be set to 25 for the above <code class="literal">SampleConfig</code> bean.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_refreshing_the_configuration_at_runtime" href="#_refreshing_the_configuration_at_runtime"></a>162.3 Refreshing the configuration at runtime</h2></div></div></div><p><a class="link" href="https://cloud.spring.io/spring-cloud-static/docs/1.0.x/spring-cloud.html#_endpoints" target="_top">Spring Cloud</a> provides support to have configuration parameters be reloadable with the POST request to <code class="literal">/actuator/refresh</code> endpoint.</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">Add the Spring Boot Actuator dependency:</li></ol></div><p>Maven coordinates:</p><pre class="screen"><dependency>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
|
</dependency></pre><p>Gradle coordinates:</p><pre class="screen">dependencies {
|
|
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
|
|
}</pre><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">Add <code class="literal">@RefreshScope</code> to your Spring configuration class to have parameters be reloadable at runtime.</li><li class="listitem">Add <code class="literal">management.endpoints.web.exposure.include=refresh</code> to your <code class="literal">application.properties</code> to allow unrestricted access to <code class="literal">/actuator/refresh</code>.</li><li class="listitem"><p class="simpara">Update a property with <code class="literal">gcloud</code>:</p><pre class="literallayout">$ gcloud beta runtime-config configs variables set \
|
|
myapp.queue_size 200 \
|
|
--config-name myapp_prod</pre></li><li class="listitem"><p class="simpara">Send a POST request to the refresh endpoint:</p><pre class="literallayout">$ curl -XPOST https://myapp.host.com/actuator/refresh</pre></li></ol></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_sample_8" href="#_sample_8"></a>162.4 Sample</h2></div></div></div><p>A <a class="link" href="https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-samples/spring-cloud-gcp-config-sample" target="_top">sample application</a> and a <a class="link" href="https://codelabs.developers.google.com/codelabs/cloud-spring-runtime-config/index.html" target="_top">codelab</a> are available.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__stackdriver_logging.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="multi_spring-cloud-gcp-reference.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="multi__spring_data_cloud_spanner.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">161. Stackdriver Logging </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud.html">Home</a></td><td width="40%" align="right" valign="top"> 163. Spring Data Cloud Spanner</td></tr></table></div></body></html> |