golang etcd watch

package etcd import ( "context" "encoding/json" "fmt" "LearnEtcd/tailf" "strings" "time" "github.com/astaxie/beego/logs" etcd_client "github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/mvcc/mvccpb" "LearnEtcd/Ip" ) type EtcdClient struct { Client *etcd_client.Client Keys []string } var ( etcdClient *EtcdClient ) func InitEtcd(addr string, key string) (collectConf []tailf.CollectConf, err error) { cli, err := etcd_client.New(etcd_client.Config{ Endpoints: []string{"localhost:2379", "localhost:22379", "localhost:32379"}, DialTimeout: 5 * time.Second, }) if err != nil { logs.Error("connect etcd failed, err:", err) return } etcdClient = &EtcdClient{ Client: cli, } if strings. [Read More]

Confd+etcd实现高可用自动发现

Confd是一个轻量级的配置管理工具。通过查询Etcd,结合配置模板引擎,保持本地配置最新,同时具备定期探测机制,配置变更自动reload。其后端支持的 数据类型有:etcd、consul、vault、environment variables、redis、zookeeper、dynamodb、stackengine、rancher。不过一般使用Confd和 etcd的配合使用比较多。其常用架构如下: 一、简单配置 1、配置etcd数据 具体步骤这里略过,这里只配置两条数据 etcdctl set /myapp/database/url www.qq.com etcdctl set /myapp/database/user rob 2、confd安装 confd比较简单就一个文件,拿过来就可以执行,可以从github上下载:https://github.com/kelseyhightower/confd/releases ,并将其放到 /usr/local/bin目录下即可。不过使用前需要创建相应的配置目录: [[email protected] bin]# mkdir -p /etc/confd/{conf.d,templates} 3、创建confd配置文件 vim /etc/confd/conf.d/myconfig.toml [template] src = "myconfig.conf.tmpl" dest = "/tmp/myconfig.conf" keys = [ "/myapp/database/url", "/myapp/database/user", ] 4、创建模板文件 vim /etc/confd/templates/myconfig.conf.tmpl [myconfig] database_url = {{getv "/myapp/database/url"}} database_user = {{getv "/myapp/database/user"}} 5、执行生成配置文件 confd -onetime -backend etcd -node http://127.0.0.1:2379 #只一次 confd -interval=60 -backend etcd -node http://127.0.0.1:2379 & #按时间轮询 6、验证文件生成 [[email protected] tmp]# cat /tmp/myconfig. [Read More]