etcd+conf


安装 etcd

#yum安装
yum install etcd -y
#更新
https://github.com/coreos/etcd/releases
替换掉etcd和etcdctl
sed -i  's/localhost/0.0.0.0/g'  /etc/etcd/etcd.conf
systemctl   start  etcd  &&  systemctl  enable  etcd

etcdctl

etcdctl get / --prefix --keys-only|grep "斗地主-NEW"|xargs -i etcdctl del {} #批量删除
export ETCDCTL_API=3 
etcdctl put mykey "this is awesome" #设置
etcdctl get mykey #获取
etcdctl del mykey #删除
etcdctl get "" --from-key  #获取所有的键值
etcdctl get "" --from-key --keys-only #只获取所有的key不含value
etcdctl get "" --from-key --keys-only --limit 3 #获取key只显示3个
etcdctl get --prefix my #获取所有以my开头的key及对应value
etcdctl del my --prefix #删除所有以my开头的键值

etcd for python

pip install etcd3
import etcd3
etcd = etcd3.client()
etcd.get('foo')
etcd.put('bar', 'doot')
etcd.delete('bar')

安装confd

https://github.com/kelseyhightower/confd/releases
cp confd-0.16.0-linux-amd64 /usr/bin/confd
chmod 755 /usr/bin/confd
# mkdir -p /etc/confd/{conf.d,templates}
conf.d #资源模板,下面文件必须以toml后缀
templates #配置文件模板,下面文件必须以tmpl后缀
cat fg_sit.conf.toml 
[template]
src = "fg_sit.conf.tmpl"
dest = "/etc/nginx/sit/allGame_fg_sit.conf"
keys = [
   "/nginx/chess/fg_sit",
   "/nginx/hunter/fg_sit",
   "/nginx/swf/fg_sit",
]
reload_cmd ="/usr/sbin/service nginx restart"



md5-ebc7b76c46f90fe06a7e13af9c6fd00b



cat fg_sit.conf.tmpl 
#############chess#############
{{range $game := lsdir "/nginx/chess/fg_sit/"}}
{{range $gametype := ls (printf "/nginx/chess/fg_sit/%s" $game)}}
## 游戏类型:{{.}} 游戏名称:{{$game}}
{{with getv (printf "/nginx/chess/fg_sit/%s/%s" $game $gametype)}}
{{$data := json .}}
upstream {{$gametype}} {
    server {{$data.sip}}:{{$data.sport}};
}
server
{
    listen {{$data.nport}} ssl;{{ $pre := split $data.ndomain "." }}
    server_name {{$data.ndomain}} ~^({{index $pre 0}}-.*).blizzmi.cn$;
    set $proxy_add {{$gametype}};
    include proxy-sit-wss.conf;
}
{{end}}{{end}}{{end}}
#############hunter#############
{{range $game := lsdir "/nginx/hunter/fg_sit/"}}
{{range $gametype := ls (printf "/nginx/hunter/fg_sit/%s" $game)}}
## 游戏类型:{{.}} 游戏名称:{{$game}}
{{with getv (printf "/nginx/hunter/fg_sit/%s/%s" $game $gametype)}}
{{$data := json .}}
upstream {{$gametype}} {
    server {{$data.sip}}:{{$data.sport}};
}
server
{
    listen {{$data.nport}} ssl;{{ $pre := split $data.ndomain "." }}
    server_name {{$data.ndomain}} ~^({{index $pre 0}}-.*).blizzmi.cn$;
    set $proxy_add {{$gametype}};
    include proxy-sit-wss.conf;
}
{{end}}{{end}}{{end}}
#############swf#############
{{range $game := lsdir "/nginx/swf/fg_sit/"}}
{{range $gametype := ls (printf "/nginx/swf/fg_sit/%s" $game)}}
## 游戏类型:{{.}} 游戏名称:{{$game}}
{{with getv (printf "/nginx/swf/fg_sit/%s/%s" $game $gametype)}}
{{$data := json .}}
upstream {{$gametype}} {
    server {{$data.sip}}:{{$data.sport}};
}
server
{
    listen {{$data.nport}} ssl;{{ $pre := split $data.ndomain "." }}
    server_name {{$data.ndomain}} ~^({{index $pre 0}}-.*).blizzmi.cn$;
    set $proxy_add {{$gametype}};
    include proxy-sit-wss.conf;
}
{{end}}{{end}}{{end}}



md5-2cfb24fcf5699a5edcf0775bd0777326



confd -onetime -backend etcdv3 -node http://127.0.0.1:2379
confd -watch -backend etcdv3 -node http://127.0.0.1:2379
confd -interval 60 -backend etcdv3 -node http://127.0.0.1:2379



md5-3a08eeaa6070471bb9c86b7d99a6b8c0



1. 假设我们定义了一个结构体:

type Article struct {
    ArticleId int
    ArticleContent string

那么我们在模板内可以通过.ArticleContent和.ArticleId的方式来获取并把变量的内容渲染到模板内。

{{.ArticleContent}}{{.ArticleId}}

2. with语句创建一个封闭的作用域,在其范围内,可以使用.action,而与外面的.无关,只与with的参数有关:

{{ with arg }}
    此时的点 . 就是arg
{{ end }}

3. 循环依靠range语句

{{range gets "/services/zookeeper/*"}}
{{$data := json .Value}}
  id: {{$data.Id}}
  ip: {{$data.IP}}
{{end}}

4. 如果取回的值是json格式的,可以创建一个map
例如: etcdctl set /myapp/upstream/app1 '{"IP": "99.99.99.99","NAME": "jhon"}'

{{with get "/myapp/upstream/app1"}}
  key: {{base .Key}}
  {{$data := json .Value}}
  Ipaddress: {{$data.IP}} Username: {{$data.NAME}}
{{end}}

5. confd一些常用的函数
get 返回匹配的“键-值”对
gets 返回所有匹配的“键-值”对

getv 返回匹配“键”的“值”
getvs 返回所有匹配“键”的“值”

ls 返回指定路径下的所有子键
lsdir 返回指定路径下所有具有子目录的子键
dir 返回指定“键”的父目录
exist 检查指定的“键”是否存在
base 返回路径的最后一个元素

另有一些go函数的别名,具体用法看文档
join
split
replace
toUpper
toLower
getenv
datatime