轉載自原文地址node
一個乞丐版自更新配置中心,更新配置後,能在各個服務器實現更新git
如下是create節點的代碼,set的同,這是簡單的操做github
package main
import (
"fmt"
. "go-zk/connect"
"github.com/samuel/go-zookeeper/zk"
)
func main() {
conn := Connect()
defer conn.Close()
flags := int32(zk.FlagSequence)
acl := zk.WorldACL(zk.PermAll)
// create node
path := PathConfig.ZNodePath +"/"+ "huodong-"
data := []byte(`{"num":6.13,"strs":["a","b"]}`)
createPath, err := conn.Create(path, data, flags, acl)
if err != nil {
panic(err)
}
}
複製代碼
package main
import (
"fmt"
"github.com/samuel/go-zookeeper/zk"
. "go-zk/connect"
"os"
"sync"
)
type Watch struct {
}
func (this *Watch)ZkChildrenWatch(c *zk.Conn, path string) {
for {
v, _, get_ch, err := c.ChildrenW(path)
if err != nil {
fmt.Println(err)
}
fmt.Printf("value of path[%s]=[%s].\n", path, v)
for {
select {
case ch_event := <-get_ch:
{
fmt.Printf("%+v\n", ch_event)
if ch_event.Type == zk.EventNodeCreated {
fmt.Printf("has new node[%d] create\n", ch_event.Path)
} else if ch_event.Type == zk.EventNodeDeleted {
fmt.Printf("has node[%s] detete\n", ch_event.Path)
} else if ch_event.Type == zk.EventNodeDataChanged {
this.Callback(c, ch_event.Path)
} else if ch_event.Type == zk.EventNodeChildrenChanged {
fmt.Printf("children node change%+v\n", ch_event.Path)
}
}
}
break
}
}
}
func (this *Watch)ZkNodeWatch(c *zk.Conn, path string) {
for {
v, _, get_ch, err := c.GetW(path)
if err != nil {
fmt.Println(err)
}
fmt.Printf("value of path[%s]=[%s].\n", path, v)
for {
select {
case ch_event := <-get_ch:
{
if ch_event.Type == zk.EventNodeCreated {
fmt.Printf("has new node[%d] create\n", ch_event.Path)
} else if ch_event.Type == zk.EventNodeDeleted {
fmt.Printf("has node[%s] detete\n", ch_event.Path)
} else if ch_event.Type == zk.EventNodeDataChanged {
this.Callback(c, ch_event.Path)
}
}
}
break
}
}
}
func (this *Watch)Callback(c *zk.Conn, path string) {
data, _, err := c.Get(path)
if err != nil {
fmt.Println(err)
}
// create file
fileName := PathConfig.LocalPath + path + ".json"
os.Create(fileName)
f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_TRUNC, 0600)
defer f.Close()
if err != nil {
fmt.Println(err.Error())
} else {
_,err=f.Write([]byte(data))
fmt.Println(err)
return
}
fmt.Print("Write File OK !!!")
}
func main() {
conn := Connect()
// 監聽全部子節點變化
children, _, err := conn.Children(PathConfig.ZNodePath)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", children)
w := Watch{}
var wg sync.WaitGroup
wg.Add(1)
go func(path string) {
w.ZkChildrenWatch(conn, path)
}(PathConfig.ZNodePath)
wg.Wait()
// 監聽節點內容變化
//var wg sync.WaitGroup
//wg.Add(len(children))
//
//for _, path := range children{
// path = PathConfig.ZNodePath + "/" + path
// go func(path string) {
// defer wg.Done()
// log.Print("Zookeeper Watcher Starting, ", path)
// w.ZkNodeWatch(conn, path)
// }(path)
//}
//wg.Wait()
}
複製代碼
讀取配置的方式不少樣,兩種思路:web
# This is Zookeeper config file.
title = "Zookeeper config file"
[zookeeper]
servers = ["10.00.85.70:2181", "10.00.80.191:2181", "10.00.97.239:2181"]
port = 2181
session_timeout = 500
enabled = true
[path]
znode_path = "/huodong/conf"
local_path = "/tmp/zookeeper"
複製代碼
因爲在本地測試,嫌麻煩就沒有部署到服務器了,將locah_path分別改爲"/tmp/zookeeper"、"/tmp/zookeeper1"、"/tmp/zookeeper2",起三個進程json
鏈接到zk服務器,修改節點的內容 set /huodong/conf/huodong-0000000001 '{"num":6.13,"strs":["a","b"]}'
緩存
看下本地文件就會生成對應的配置文件bash
1、zookeeper 基礎介紹服務器
2、zookeeper 集羣搭建session