多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ package zookeeper import ( "errors" "github.com/samuel/go-zookeeper/zk" "project/library/dao/common" "project/library/dao/config" "time" ) type ZookeeperMgr struct { Conn *zk.Conn /* flags有4种取值: 0:永久,除非手动删除 1:短暂,session断开则改节点也被删除 2:会自动在节点后面添加序号 3:即,短暂且自动添加序号 */ //初始化时默认0 flags int32 //初始化时默认允许所有操作 acl []zk.ACL } func InitZookeeper(cfg *config.ZkCfg) *ZookeeperMgr { conn, _, err := zk.Connect(cfg.Servers, time.Second*5) if err != nil { common.SimplePanic("InitZookeeper失败", err.Error()) } return &ZookeeperMgr{Conn: conn, acl: zk.WorldACL(zk.PermAll)} } func InitZookeeper2(cfg *config.ZkCfg) (error, *ZookeeperMgr) { conn, _, err := zk.Connect(cfg.Servers, time.Second*5) if err != nil { return errors.New("InitZookeeper失败"), nil } return nil, &ZookeeperMgr{Conn: conn, acl: zk.WorldACL(zk.PermAll)} } func (mgr *ZookeeperMgr) Create(path string, data []byte) (string, error) { return mgr.Conn.Create(path, data, mgr.flags, mgr.acl) } func (mgr *ZookeeperMgr) Delete(path string) error { _, stat, err := mgr.Conn.Get(path) if err != nil { return err } return mgr.Conn.Delete(path, stat.Version) } func (mgr *ZookeeperMgr) Update(path string, data []byte) error { _, stat, err := mgr.Conn.Get(path) if err != nil { return err } _, err = mgr.Conn.Set(path, data, stat.Version) return err } func (mgr *ZookeeperMgr) Get(path string) ([]byte, *zk.Stat, error) { return mgr.Conn.Get(path) } ~~~