spring cloud config 之 consul

Reference:
使用Consul作为配置信息的Repository

类似ZooKeeper,Consul服务本事提供了Key/Value存储功能,所以很自然的通过Spring Cloud Consul Config就能完成Config Server and Client的功能。实际上,可以理解成配置信息从保存在git文件系统,变成了保存在Consul里。

1)激活Spring Cloud Consul Config,只需依赖spring-cloud-consul-config。

org.springframework.cloud:spring-cloud-consul-config

2)在bootstrap.yml(或bootstrap.property)中配置spring cloud consul config。

spring:
  cloud:
    consul:
      config:
        enabled: true
        prefix: configuration
        defaultContext: apps
        profileSeparator: '::'

3)如果你认为把原来保存在property/yml文件中的properties作为value保存在以data(作为key)下更方便些,可用通过设定format可以实现。

spring:
  cloud:
    consul:
      config:
        format: YAML


config/testApp,dev/data
config/testApp/data
config/application,dev/data
config/application/data

可以通过下面的设置,修改健data的值

spring.cloud.consul.config.data-key

4)git2consul
consul社区的开源项目git2consul可以把存在git目录里的文件导入到consule中不同的key中。缺省地,文件名就是key的名字。详细用法参见[]https://github.com/Cimpress-MCP/git2consul](https://github.com/Cimpress-MCP/git2consul)。