38 lines
10 KiB
HTML
38 lines
10 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>69. Distributed Configuration with Consul</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_consul.html" title="Part IX. Spring Cloud Consul"><link rel="prev" href="multi_spring-cloud-consul-discovery.html" title="68. Service Discovery with Consul"><link rel="next" href="multi_spring-cloud-consul-retry.html" title="70. Consul Retry"></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">69. Distributed Configuration with Consul</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi_spring-cloud-consul-discovery.html">Prev</a> </td><th width="60%" align="center">Part IX. Spring Cloud Consul</th><td width="20%" align="right"> <a accesskey="n" href="multi_spring-cloud-consul-retry.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="spring-cloud-consul-config" href="#spring-cloud-consul-config"></a>69. Distributed Configuration with Consul</h2></div></div></div><p>Consul provides a <a class="link" href="https://consul.io/docs/agent/http/kv.html" target="_top">Key/Value Store</a> for storing configuration and other metadata. Spring Cloud Consul Config is an alternative to the <a class="link" href="https://github.com/spring-cloud/spring-cloud-config" target="_top">Config Server and Client</a>. Configuration is loaded into the Spring Environment during the special "bootstrap" phase. Configuration is stored in the <code class="literal">/config</code> folder by default. Multiple <code class="literal">PropertySource</code> instances are created based on the application’s name and the active profiles that mimicks the Spring Cloud Config order of resolving properties. For example, an application with the name "testApp" and with the "dev" profile will have the following property sources created:</p><pre class="screen">config/testApp,dev/
|
|
config/testApp/
|
|
config/application,dev/
|
|
config/application/</pre><p>The most specific property source is at the top, with the least specific at the bottom. Properties in the <code class="literal">config/application</code> folder are applicable to all applications using consul for configuration. Properties in the <code class="literal">config/testApp</code> folder are only available to the instances of the service named "testApp".</p><p>Configuration is currently read on startup of the application. Sending a HTTP POST to <code class="literal">/refresh</code> will cause the configuration to be reloaded. <a class="xref" href="multi_spring-cloud-consul-config.html#spring-cloud-consul-config-watch" title="69.3 Config Watch">Section 69.3, “Config Watch”</a> will also automatically detect changes and reload the application context.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_how_to_activate_2" href="#_how_to_activate_2"></a>69.1 How to activate</h2></div></div></div><p>To get started with Consul Configuration use the starter with group <code class="literal">org.springframework.cloud</code> and artifact id <code class="literal">spring-cloud-starter-consul-config</code>. See the <a class="link" href="https://projects.spring.io/spring-cloud/" target="_top">Spring Cloud Project page</a> for details on setting up your build system with the current Spring Cloud Release Train.</p><p>This will enable auto-configuration that will setup Spring Cloud Consul Config.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_customizing" href="#_customizing"></a>69.2 Customizing</h2></div></div></div><p>Consul Config may be customized using the following properties:</p><p><b>bootstrap.yml. </b>
|
|
</p><pre class="screen">spring:
|
|
cloud:
|
|
consul:
|
|
config:
|
|
enabled: true
|
|
prefix: configuration
|
|
defaultContext: apps
|
|
profileSeparator: '::'</pre><p>
|
|
</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">enabled</code> setting this value to "false" disables Consul Config</li><li class="listitem"><code class="literal">prefix</code> sets the base folder for configuration values</li><li class="listitem"><code class="literal">defaultContext</code> sets the folder name used by all applications</li><li class="listitem"><code class="literal">profileSeparator</code> sets the value of the separator used to separate the profile name in property sources with profiles</li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-consul-config-watch" href="#spring-cloud-consul-config-watch"></a>69.3 Config Watch</h2></div></div></div><p>The Consul Config Watch takes advantage of the ability of consul to <a class="link" href="https://www.consul.io/docs/agent/watches.html#keyprefix" target="_top">watch a key prefix</a>. The Config Watch makes a blocking Consul HTTP API call to determine if any relevant configuration data has changed for the current application. If there is new configuration data a Refresh Event is published. This is equivalent to calling the <code class="literal">/refresh</code> actuator endpoint.</p><p>To change the frequency of when the Config Watch is called change <code class="literal">spring.cloud.consul.config.watch.delay</code>. The default value is 1000, which is in milliseconds. The delay is the amount of time after the end of the previous invocation and the start of the next.</p><p>To disable the Config Watch set <code class="literal">spring.cloud.consul.config.watch.enabled=false</code>.</p><p>The watch uses a Spring <code class="literal">TaskScheduler</code> to schedule the call to consul. By default it is a <code class="literal">ThreadPoolTaskScheduler</code> with a <code class="literal">poolSize</code> of 1. To change the <code class="literal">TaskScheduler</code>, create a bean of type <code class="literal">TaskScheduler</code> named with the <code class="literal">ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME</code> constant.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-consul-config-format" href="#spring-cloud-consul-config-format"></a>69.4 YAML or Properties with Config</h2></div></div></div><p>It may be more convenient to store a blob of properties in YAML or Properties format as opposed to individual key/value pairs. Set the <code class="literal">spring.cloud.consul.config.format</code> property to <code class="literal">YAML</code> or <code class="literal">PROPERTIES</code>. For example to use YAML:</p><p><b>bootstrap.yml. </b>
|
|
</p><pre class="screen">spring:
|
|
cloud:
|
|
consul:
|
|
config:
|
|
format: YAML</pre><p>
|
|
</p><p>YAML must be set in the appropriate <code class="literal">data</code> key in consul. Using the defaults above the keys would look like:</p><pre class="screen">config/testApp,dev/data
|
|
config/testApp/data
|
|
config/application,dev/data
|
|
config/application/data</pre><p>You could store a YAML document in any of the keys listed above.</p><p>You can change the data key using <code class="literal">spring.cloud.consul.config.data-key</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-consul-config-git2consul" href="#spring-cloud-consul-config-git2consul"></a>69.5 git2consul with Config</h2></div></div></div><p>git2consul is a Consul community project that loads files from a git repository to individual keys into Consul. By default the names of the keys are names of the files. YAML and Properties files are supported with file extensions of <code class="literal">.yml</code> and <code class="literal">.properties</code> respectively. Set the <code class="literal">spring.cloud.consul.config.format</code> property to <code class="literal">FILES</code>. For example:</p><p><b>bootstrap.yml. </b>
|
|
</p><pre class="screen">spring:
|
|
cloud:
|
|
consul:
|
|
config:
|
|
format: FILES</pre><p>
|
|
</p><p>Given the following keys in <code class="literal">/config</code>, the <code class="literal">development</code> profile and an application name of <code class="literal">foo</code>:</p><pre class="screen">.gitignore
|
|
application.yml
|
|
bar.properties
|
|
foo-development.properties
|
|
foo-production.yml
|
|
foo.properties
|
|
master.ref</pre><p>the following property sources would be created:</p><pre class="screen">config/foo-development.properties
|
|
config/foo.properties
|
|
config/application.yml</pre><p>The value of each key needs to be a properly formatted YAML or Properties file.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-consul-failfast" href="#spring-cloud-consul-failfast"></a>69.6 Fail Fast</h2></div></div></div><p>It may be convenient in certain circumstances (like local development or certain test scenarios) to not fail if consul isn’t available for configuration. Setting <code class="literal">spring.cloud.consul.config.failFast=false</code> in <code class="literal">bootstrap.yml</code> will cause the configuration module to log a warning rather than throw an exception. This will allow the application to continue startup normally.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi_spring-cloud-consul-discovery.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="multi__spring_cloud_consul.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="multi_spring-cloud-consul-retry.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">68. Service Discovery with Consul </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud.html">Home</a></td><td width="40%" align="right" valign="top"> 70. Consul Retry</td></tr></table></div></body></html> |