有时,当配置文件很长时,将配置移动到领事密钥值存储很困难。
通常,我们在application.properties文件中具有与应用程序相关的配置。每当我们需要更改值时,我们都将在文件中对其进行更改,并且必须重建并重新部署应用程序以对应用程序进行更改。
领事提供密钥/值存储,用于存储配置和其他元数据。在特殊的“引导”阶段将配置加载到Spring环境中。
使用Consul,您将所有配置属性存储在Consul KV存储中,从而避免了代码泄漏和重新部署。
这是引导程序配置的示例:
spring:
application:
name: #app-name
profiles:
active: #spring.profiles.active#
cloud:
consul:
config:
enabled: true // This line actually enables the consul config
host: localhost
port: 8500 // This is where your config server will be running
因为Consul的键值存储将Key-Value Json文件作为输入,所以将.yaml配置转换为Consul KV格式变得非常复杂,尤其是当您使用大型配置时。
请在此处找到使用Java客户端编写的转换代码:https://github.com/himanshukumars/utils/tree/master/utils。
默认情况下,配置存储在/ config文件夹中。
请确保您具有以下依赖项:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
<dependency>
<groupId>com.github.wnameless</groupId>
<artifactId>json-flattener</artifactId>
<version>0.2.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
为了使用此功能,请确保您已使用https://onlineyamltools.com/convert-yaml-to-json之类的在线转换器将YAML转换为JSON。>