confd介绍
confd原理
通过定时任务去服务端获取配置信息,生成配置文本,应用程序从本机读取配置文本。
confd的目的
1.统一服务端管理各种配置文件,配置文件模板和数据是分离的,可各自单独管理
2.动态修改配置属性,然后重新生成相关的配置文件
3.提供cli修改数据
confd的特点及优势
1.基础解析:配置文件使用toml格式,模板数据分离
2.多种存储支持:local file、redis、zookeeper
3.两种运行模式:Debug或Daemon
4.cli工具:修改store,批量更新配置
搭建步骤:
etcd搭建
yum install -y etcd
confd搭建
代码:https://github.com/kelseyhightower/confd
快速搭建:https://github.com/kelseyhightower/confd/blob/master/docs/quick-start-guide.md
$ mkdir -p $GOPATH/src/github.com/kelseyhightower
$ git clone https://github.com/kelseyhightower/confd.git $GOPATH/src/github.com/kelseyhightower/confd
$ cd $GOPATH/src/github.com/kelseyhightower/confd
$ make
通过git即可安装confd,需要依赖go1.10及以上环境。
Create a template resource config
Template resources are defined in TOML config files under the confdir
.
/etc/confd/conf.d/myconfig.toml
[template]
src = "myconfig.conf.tmpl"
dest = "/tmp/myconfig.conf"
keys = [
"/myapp/database/url",
"/myapp/database/user",
]
Create the source template
Source templates are Golang text templates.
/etc/confd/templates/myconfig.conf.tmpl
[myconfig]
database_url = {{getv "/myapp/database/url"}}
database_user = {{getv "/myapp/database/user"}}
服务端从Redis取数据,命令如下:
redis
confd -onetime -backend redis -node 192.168.255.210:6379
or if you want to connect to a specific redis database (4 in this example):
confd -onetime -backend redis -node 192.168.255.210:6379/4
服务端从etcd取数据,命令如下:
etcd
confd -onetime -backend etcd -node http://127.0.0.1:2379