初始化项目

This commit is contained in:
皮蛋13361098506
2025-01-06 16:01:02 +08:00
commit 1b77f62820
575 changed files with 69193 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
package zooKeeperMgr
import (
"fmt"
"strings"
"github.com/samuel/go-zookeeper/zk"
)
// 级联创建path节点如果中间节点不存在则创建
func CascadeCreate(conn *zk.Conn, path string) error {
if path == "" {
return nil
}
subPathArr := strings.Split(path, "/")
path = ""
var data = []byte("")
var flags int32 = 0
var acls = zk.WorldACL(zk.PermAll)
for i := 0; i < len(subPathArr); i++ {
// Omit the first one which is /
if subPathArr[i] == "" {
continue
}
path += fmt.Sprintf("/%s",subPathArr[i])
fmt.Printf("Path:%s\n",path)
exists, _, err := conn.Exists(path)
if err != nil {
return err
}
if exists {
continue
}
retPath, err := conn.Create(path, data, flags, acls)
if err != nil {
if err == zk.ErrNodeExists {
fmt.Println(err)
continue
}
return err
}
fmt.Printf("ZooKeeper created:%s\n", retPath)
}
return nil
}

View File

@@ -0,0 +1,27 @@
package zooKeeperMgr
import (
"testing"
"time"
"github.com/samuel/go-zookeeper/zk"
)
func TestCascadeCreate(t *testing.T) {
zkConfigObj := &ZooKeeperConfig {
Servers: "172.27.0.4:2181,172.27.0.6:2181,172.27.0.9:2181",
StartPath: "/Develop/GameServer",
SessionTimeout: 10 * time.Second,
}
conn, _, err := zk.Connect(zkConfigObj.GetServerList(), zkConfigObj.SessionTimeout)
if err != nil {
panic(err)
}
defer conn.Close()
err = CascadeCreate(conn, zkConfigObj.StartPath)
if err != nil {
t.Errorf("There should be no error, but now there is:%v", err)
}
}

View File

@@ -0,0 +1,27 @@
package zooKeeperMgr
import (
"strings"
"time"
)
// ZooKeeper配置对象
type ZooKeeperConfig struct {
// ZooKeeper的服务器地址(如果有多个地址,则用,分隔)
Servers string
// 起始节点路径
StartPath string
// 会话超时时间(单位:秒)
SessionTimeout time.Duration
}
// 获取所有的ZooKeeper服务器列表
func (this *ZooKeeperConfig) GetServerList() []string {
return strings.Split(this.Servers, ",")
}
func (this *ZooKeeperConfig) Parse() {
this.SessionTimeout = this.SessionTimeout * time.Second
}