Files
spring-cloud-static/spring-cloud-config/2.1.9.RELEASE/multi/multi__spring_cloud_config_client.html
2020-05-28 16:14:40 +00:00

83 lines
20 KiB
HTML

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>7.&nbsp;Spring Cloud Config Client</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-config.html" title="Spring Cloud Config"><link rel="up" href="multi_spring-cloud-config.html" title="Spring Cloud Config"><link rel="prev" href="multi__push_notifications_and_spring_cloud_bus.html" title="6.&nbsp;Push Notifications and Spring Cloud Bus"></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">7.&nbsp;Spring Cloud Config Client</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__push_notifications_and_spring_cloud_bus.html">Prev</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;</td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_spring_cloud_config_client" href="#_spring_cloud_config_client"></a>7.&nbsp;Spring Cloud Config Client</h1></div></div></div><p>A Spring Boot application can take immediate advantage of the Spring Config Server (or other external property sources provided by the application developer).
It also picks up some additional useful features related to <code class="literal">Environment</code> change events.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="config-first-bootstrap" href="#config-first-bootstrap"></a>7.1&nbsp;Config First Bootstrap</h2></div></div></div><p>The default behavior for any application that has the Spring Cloud Config Client on the classpath is as follows:
When a config client starts, it binds to the Config Server (through the <code class="literal">spring.cloud.config.uri</code> bootstrap configuration property) and initializes Spring <code class="literal">Environment</code> with remote property sources.</p><p>The net result of this behavior is that all client applications that want to consume the Config Server need a <code class="literal">bootstrap.yml</code> (or an environment variable) with the server address set in <code class="literal">spring.cloud.config.uri</code> (it defaults to "http://localhost:8888").</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="discovery-first-bootstrap" href="#discovery-first-bootstrap"></a>7.2&nbsp;Discovery First Bootstrap</h2></div></div></div><p>If you use a <code class="literal">DiscoveryClient</code> implementation, such as Spring Cloud Netflix and Eureka Service Discovery or Spring Cloud Consul, you can have the Config Server register with the Discovery Service.
However, in the default <span class="quote">&#8220;<span class="quote">Config First</span>&#8221;</span> mode, clients cannot take advantage of the registration.</p><p>If you prefer to use <code class="literal">DiscoveryClient</code> to locate the Config Server, you can do so by setting <code class="literal">spring.cloud.config.discovery.enabled=true</code> (the default is <code class="literal">false</code>).
The net result of doing so is that client applications all need a <code class="literal">bootstrap.yml</code> (or an environment variable) with the appropriate discovery configuration.
For example, with Spring Cloud Netflix, you need to define the Eureka server address (for example, in <code class="literal">eureka.client.serviceUrl.defaultZone</code>).
The price for using this option is an extra network round trip on startup, to locate the service registration.
The benefit is that, as long as the Discovery Service is a fixed point, the Config Server can change its coordinates.
The default service ID is <code class="literal">configserver</code>, but you can change that on the client by setting <code class="literal">spring.cloud.config.discovery.serviceId</code> (and on the server, in the usual way for a service, such as by setting <code class="literal">spring.application.name</code>).</p><p>The discovery client implementations all support some kind of metadata map (for example, we have <code class="literal">eureka.instance.metadataMap</code> for Eureka).
Some additional properties of the Config Server may need to be configured in its service registration metadata so that clients can connect correctly.
If the Config Server is secured with HTTP Basic, you can configure the credentials as <code class="literal">user</code> and <code class="literal">password</code>.
Also, if the Config Server has a context path, you can set <code class="literal">configPath</code>.
For example, the following YAML file is for a Config Server that is a Eureka client:</p><p><b>bootstrap.yml.&nbsp;</b>
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">eureka</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> instance</span>:
...
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> metadataMap</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> user</span>: osufhalskjrtl
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> password</span>: lviuhlszvaorhvlo5847
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> configPath</span>: /config</pre><p>
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="config-client-fail-fast" href="#config-client-fail-fast"></a>7.3&nbsp;Config Client Fail Fast</h2></div></div></div><p>In some cases, you may want to fail startup of a service if it cannot connect to the Config Server.
If this is the desired behavior, set the bootstrap configuration property <code class="literal">spring.cloud.config.fail-fast=true</code> to make the client halt with an Exception.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="config-client-retry" href="#config-client-retry"></a>7.4&nbsp;Config Client Retry</h2></div></div></div><p>If you expect that the config server may occasionally be unavailable when your application starts, you can make it keep trying after a failure.
First, you need to set <code class="literal">spring.cloud.config.fail-fast=true</code>.
Then you need to add <code class="literal">spring-retry</code> and <code class="literal">spring-boot-starter-aop</code> to your classpath.
The default behavior is to retry six times with an initial backoff interval of 1000ms and an exponential multiplier of 1.1 for subsequent backoffs.
You can configure these properties (and others) by setting the <code class="literal">spring.cloud.config.retry.*</code> configuration properties.</p><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Tip"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="images/tip.png"></td><th align="left">Tip</th></tr><tr><td align="left" valign="top"><p>To take full control of the retry behavior, add a <code class="literal">@Bean</code> of type <code class="literal">RetryOperationsInterceptor</code> with an ID of <code class="literal">configServerRetryInterceptor</code>.
Spring Retry has a <code class="literal">RetryInterceptorBuilder</code> that supports creating one.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_locating_remote_configuration_resources" href="#_locating_remote_configuration_resources"></a>7.5&nbsp;Locating Remote Configuration Resources</h2></div></div></div><p>The Config Service serves property sources from <code class="literal">/{application}/{profile}/{label}</code>, where the default bindings in the client app are as follows:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">"name" = <code class="literal">${spring.application.name}</code></li><li class="listitem">"profile" = <code class="literal">${spring.profiles.active}</code> (actually <code class="literal">Environment.getActiveProfiles()</code>)</li><li class="listitem">"label" = "master"</li></ul></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>When setting the property <code class="literal">${spring.application.name}</code> do not prefix your app name with the reserved word <code class="literal">application-</code> to prevent issues resolving the correct property source.</p></td></tr></table></div><p>You can override all of them by setting <code class="literal">spring.cloud.config.*</code> (where <code class="literal">*</code> is <code class="literal">name</code>, <code class="literal">profile</code> or <code class="literal">label</code>).
The <code class="literal">label</code> is useful for rolling back to previous versions of configuration.
With the default Config Server implementation, it can be a git label, branch name, or commit ID.
Label can also be provided as a comma-separated list.
In that case, the items in the list are tried one by one until one succeeds.
This behavior can be useful when working on a feature branch.
For instance, you might want to align the config label with your branch but make it optional (in that case, use <code class="literal">spring.cloud.config.label=myfeature,develop</code>).</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_specifying_multiple_urls_for_the_config_server" href="#_specifying_multiple_urls_for_the_config_server"></a>7.6&nbsp;Specifying Multiple Urls for the Config Server</h2></div></div></div><p>To ensure high availability when you have multiple instances of Config Server deployed and expect one or more instances to be unavailable from time to time, you can either specify multiple URLs (as a comma-separated list under the <code class="literal">spring.cloud.config.uri</code> property) or have all your instances register in a Service Registry like Eureka ( if using Discovery-First Bootstrap mode ). Note that doing so ensures high availability only when the Config Server is not running (that is, when the application has exited) or when a connection timeout has occurred. For example, if the Config Server returns a 500 (Internal Server Error) response or the Config Client receives a 401 from the Config Server (due to bad credentials or other causes), the Config Client does not try to fetch properties from other URLs. An error of that kind indicates a user issue rather than an availability problem.</p><p>If you use HTTP basic security on your Config Server, it is currently possible to support per-Config Server auth credentials only if you embed the credentials in each URL you specify under the <code class="literal">spring.cloud.config.uri</code> property. If you use any other kind of security mechanism, you cannot (currently) support per-Config Server authentication and authorization.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_configuring_timeouts" href="#_configuring_timeouts"></a>7.7&nbsp;Configuring Timeouts</h2></div></div></div><p>If you want to configure timeout thresholds:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">Read timeouts can be configured by using the property <code class="literal">spring.cloud.config.request-read-timeout</code>.</li><li class="listitem">Connection timeouts can be configured by using the property <code class="literal">spring.cloud.config.request-connect-timeout</code>.</li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_security_2" href="#_security_2"></a>7.8&nbsp;Security</h2></div></div></div><p>If you use HTTP Basic security on the server, clients need to know the password (and username if it is not the default).
You can specify the username and password through the config server URI or via separate username and password properties, as shown in the following example:</p><p><b>bootstrap.yml.&nbsp;</b>
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> config</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: https://user:secret@myconfig.mycompany.com</pre><p>
</p><p>The following example shows an alternate way to pass the same information:</p><p><b>bootstrap.yml.&nbsp;</b>
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> config</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: https://myconfig.mycompany.com
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> username</span>: user
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> password</span>: secret</pre><p>
</p><p>The <code class="literal">spring.cloud.config.password</code> and <code class="literal">spring.cloud.config.username</code> values override anything that is provided in the URI.</p><p>If you deploy your apps on Cloud Foundry, the best way to provide the password is through service credentials (such as in the URI, since it does not need to be in a config file).
The following example works locally and for a user-provided service on Cloud Foundry named <code class="literal">configserver</code>:</p><p><b>bootstrap.yml.&nbsp;</b>
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> config</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: ${vcap.services.configserver.credentials.uri:http://user:password@localhost:<xslthl:number xmlns:xslthl="http://xslthl.sourceforge.net/">8888</xslthl:number><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span></pre><p>
</p><p>If you use another form of security, you might need to <a class="link" href="multi__spring_cloud_config_client.html#custom-rest-template" title="7.8.2&nbsp;Providing A Custom RestTemplate">provide a <code class="literal">RestTemplate</code></a> to the <code class="literal">ConfigServicePropertySourceLocator</code> (for example, by grabbing it in the bootstrap context and injecting it).</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_health_indicator_2" href="#_health_indicator_2"></a>7.8.1&nbsp;Health Indicator</h3></div></div></div><p>The Config Client supplies a Spring Boot Health Indicator that attempts to load configuration from the Config Server.
The health indicator can be disabled by setting <code class="literal">health.config.enabled=false</code>.
The response is also cached for performance reasons.
The default cache time to live is 5 minutes.
To change that value, set the <code class="literal">health.config.time-to-live</code> property (in milliseconds).</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="custom-rest-template" href="#custom-rest-template"></a>7.8.2&nbsp;Providing A Custom RestTemplate</h3></div></div></div><p>In some cases, you might need to customize the requests made to the config server from the client.
Typically, doing so involves passing special <code class="literal">Authorization</code> headers to authenticate requests to the server.
To provide a custom <code class="literal">RestTemplate</code>:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">Create a new configuration bean with an implementation of <code class="literal">PropertySourceLocator</code>, as shown in the following example:</li></ol></div><p><b>CustomConfigServiceBootstrapConfiguration.java.&nbsp;</b>
</p><pre class="programlisting"><xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Configuration</xslthl:annotation>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> CustomConfigServiceBootstrapConfiguration {
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Bean</xslthl:annotation>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> ConfigServicePropertySourceLocator configServicePropertySourceLocator() {
ConfigClientProperties clientProperties = configClientProperties();
ConfigServicePropertySourceLocator configServicePropertySourceLocator = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> ConfigServicePropertySourceLocator(clientProperties);
configServicePropertySourceLocator.setRestTemplate(customRestTemplate(clientProperties));
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> configServicePropertySourceLocator;
}
}</pre><p>
</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">In <code class="literal">resources/META-INF</code>, create a file called
<code class="literal">spring.factories</code> and specify your custom configuration, as shown in the following example:</li></ol></div><p><b>spring.factories.&nbsp;</b>
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">org.springframework.cloud.bootstrap.BootstrapConfiguration </span>= com.my.config.client.CustomConfigServiceBootstrapConfiguration</pre><p>
</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_vault" href="#_vault"></a>7.8.3&nbsp;Vault</h3></div></div></div><p>When using Vault as a backend to your config server, the client needs to supply a token for the server to retrieve values from Vault.
This token can be provided within the client by setting <code class="literal">spring.cloud.config.token</code>
in <code class="literal">bootstrap.yml</code>, as shown in the following example:</p><p><b>bootstrap.yml.&nbsp;</b>
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> config</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> token</span>: YourVaultToken</pre><p>
</p></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_nested_keys_in_vault" href="#_nested_keys_in_vault"></a>7.9&nbsp;Nested Keys In Vault</h2></div></div></div><p>Vault supports the ability to nest keys in a value stored in Vault, as shown in the following example:</p><p><code class="literal">echo -n '{"appA": {"secret": "appAsecret"}, "bar": "baz"}' | vault write secret/myapp -</code></p><p>This command writes a JSON object to your Vault.
To access these values in Spring, you would use the traditional dot(<code class="literal">.</code>) annotation, as shown in the following example</p><pre class="programlisting"><xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Value("${appA.secret}")</xslthl:annotation>
String name = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"World"</span>;</pre><p>The preceding code would sets the value of the <code class="literal">name</code> variable to <code class="literal">appAsecret</code>.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__push_notifications_and_spring_cloud_bus.html">Prev</a>&nbsp;</td><td width="20%" align="center">&nbsp;</td><td width="40%" align="right">&nbsp;</td></tr><tr><td width="40%" align="left" valign="top">6.&nbsp;Push Notifications and Spring Cloud Bus&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud-config.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;</td></tr></table></div></body></html>