etcd+confd配置中心搭建

confd是一种用于自动管理和更新配置文件的工具。它通过定时任务从服务端如Redis或etcd获取配置信息,并生成本地配置文件供应用使用。支持多种存储方式和运行模式,可通过CLI进行配置管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值