• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang zk.Connect函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/samuel/go-zookeeper/zk.Connect函数的典型用法代码示例。如果您正苦于以下问题:Golang Connect函数的具体用法?Golang Connect怎么用?Golang Connect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Connect函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: CreateConnection

func (mgr *MetadataManager) CreateConnection() {
	if mgr.zkConn != nil {
		// Return if the connection is good already
		if mgr.zkConn.State() == zk.StateConnected ||
			mgr.zkConn.State() == zk.StateHasSession ||
			mgr.zkConn.State() == zk.StateConnecting {
			return
		}
		// Close the connection because it probably expired
		mgr.zkConn.Close()
	}

	conn, _, err := zk.Connect(mgr.zookeepers, time.Second)
	if err != nil {
		conn.Close()
		log.Panic(err)
	}
	bns := baseNamespace{}
	ns := makeSubSpace(makeSubSpace(makeSubSpace(bns, "riak"), "frameworks"), mgr.frameworkID)
	lockPath := makeSubSpace(ns, "lock")
	zkLock := zk.NewLock(conn, lockPath.GetZKPath(), zk.WorldACL(zk.PermAll))

	mgr.zkConn = conn
	mgr.namespace = ns
	mgr.zkLock = *zkLock
}
开发者ID:sanmiguel,项目名称:riak-mesos,代码行数:26,代码来源:manager.go


示例2: connect

func connect() *zk.Conn {
	zksStr := os.Getenv("ZOOKEEPER_SERVERS")
	zks := strings.Split(zksStr, ",")
	conn, _, err := zk.Connect(zks, time.Second)
	must(err)
	return conn
}
开发者ID:w3-iot,项目名称:zookeeper-and-go,代码行数:7,代码来源:ex-children.go


示例3: NewZookeeperClient

func NewZookeeperClient(machines []string) (*Client, error) {
	c, _, err := zk.Connect(machines, time.Second) //*10)
	if err != nil {
		panic(err)
	}
	return &Client{c}, nil
}
开发者ID:ninefive,项目名称:confd,代码行数:7,代码来源:client.go


示例4: NewStormClient

func NewStormClient(app *ApplicationContext, cluster string) (*StormClient, error) {
	// here we share the timeout w/ global zk
	zkconn, _, err := zk.Connect(app.Config.Storm[cluster].Zookeepers, time.Duration(app.Config.Zookeeper.Timeout)*time.Second)
	if err != nil {
		return nil, err
	}

	client := &StormClient{
		app:            app,
		cluster:        cluster,
		conn:           zkconn,
		stormGroupLock: sync.RWMutex{},
		stormGroupList: make(map[string]bool),
	}

	// Now get the first set of offsets and start a goroutine to continually check them
	client.refreshConsumerGroups()
	client.stormRefreshTicker = time.NewTicker(time.Duration(client.app.Config.Lagcheck.StormGroupRefresh) * time.Second)
	go func() {
		for _ = range client.stormRefreshTicker.C {
			client.refreshConsumerGroups()
		}
	}()

	return client, nil
}
开发者ID:Crowdmix,项目名称:Burrow,代码行数:26,代码来源:storm.go


示例5: Connect

func (z *ZookeeperClient) Connect() {
	var err error
	z.zkConn, _, err = zk.Connect(z.config.Servers, z.config.RecvTimeout)
	if err != nil {
		fmt.Println(err)
	}
}
开发者ID:shiaho,项目名称:go-kafka,代码行数:7,代码来源:zookeeper_client.go


示例6: Connect

func (zr *ZookeeperReporter) Connect() (zk.State, error) {
	if zr.ZKConnection != nil {
		state := zr.ZKConnection.State()
		switch state {
		case zk.StateUnknown, zk.StateConnectedReadOnly, zk.StateExpired, zk.StateAuthFailed, zk.StateConnecting:
			{
				//Disconnect, and let Reconnection happen
				log.Warn("Zookeeper Connection is in BAD State [", state, "] Reconnect")
				zr.ZKConnection.Close()
			}
		case zk.StateConnected, zk.StateHasSession:
			{
				log.Debug("Zookeeper Connection of [", zr.ServiceName, "][", zr.InstanceID, "] connected(", state, "), nothing to do.")
				return state, nil
			}
		case zk.StateDisconnected:
			{
				log.Info("Reporter Connection is Disconnected -> Reconnection")
			}
		}
	}
	conn, _, err := zk.Connect(zr.ZKHosts, 10*time.Second)
	if err != nil {
		zr.ZKConnection = nil
		log.Warn("Unable to Connect to ZooKeeper (", err, ")")
		return zk.StateDisconnected, err
	}
	zr.ZKConnection = conn
	var zkLogger ZKDebugLogger
	zr.ZKConnection.SetLogger(zkLogger)
	zr.ZKConnection = conn
	state := zr.ZKConnection.State()
	return state, nil
}
开发者ID:cvasseur,项目名称:go-nerve,代码行数:34,代码来源:zookeeper.go


示例7: TestConnectToZooKeeper

func TestConnectToZooKeeper(t *testing.T) {
	c, err := ConnectToZooKeeper(15, time.Millisecond*500, func(url string) bool {
		conn, _, err := zk.Connect([]string{url}, time.Second)
		if err != nil {
			return false
		}
		defer conn.Close()

		// Verify that we can perform operations, and that it's
		// a clean slate (/zookeeper should be the only path)
		children, _, err := conn.Children("/")
		if err != nil {
			return false
		}
		if len(children) != 1 {
			return false
		}
		if children[0] != "zookeeper" {
			return false
		}

		return true
	})
	assert.Nil(t, err)
	defer c.KillRemove()
}
开发者ID:nmarcetic,项目名称:mainflux,代码行数:26,代码来源:docker_integration_test.go


示例8: GetPreviousFrameworkID

func GetPreviousFrameworkID(
	zkServers []string,
	zkChroot string,
	frameworkName string,
) (fwid string, err error) {
	request := func() (string, error) {
		c, _, err := zk.Connect(zkServers, RPC_TIMEOUT)
		if err != nil {
			return "", err
		}
		defer c.Close()
		rawData, _, err := c.Get(zkChroot + "/" + frameworkName + "_framework_id")
		return string(rawData), err
	}

	backoff := 1
	for retries := 0; retries < RPC_RETRIES; retries++ {
		fwid, err = request()
		if err == nil {
			return fwid, err
		}
		time.Sleep(time.Duration(backoff) * time.Second)
		backoff = int(math.Min(float64(backoff<<1), 8))
	}
	return "", err
}
开发者ID:puppetizeme,项目名称:etcd-mesos,代码行数:26,代码来源:zk.go


示例9: GetPreviousReconciliationInfo

func GetPreviousReconciliationInfo(
	zkServers []string,
	zkChroot string,
	frameworkName string,
) (recon map[string]string, err error) {
	request := func() (map[string]string, error) {
		c, _, err := zk.Connect(zkServers, RPC_TIMEOUT)
		if err != nil {
			return map[string]string{}, err
		}
		defer c.Close()
		rawData, _, err := c.Get(zkChroot + "/" + frameworkName + "_reconciliation")
		if err == zk.ErrNoNode {
			return map[string]string{}, nil
		}
		if err != nil {
			return map[string]string{}, err
		}
		reconciliationInfo := map[string]string{}
		err = json.Unmarshal(rawData, &reconciliationInfo)
		return reconciliationInfo, err
	}

	backoff := 1
	for retries := 0; retries < RPC_RETRIES; retries++ {
		recon, err = request()
		if err == nil {
			return recon, err
		}
		time.Sleep(time.Duration(backoff) * time.Second)
		backoff = int(math.Min(float64(backoff<<1), 8))
	}
	return recon, err
}
开发者ID:puppetizeme,项目名称:etcd-mesos,代码行数:34,代码来源:zk.go


示例10: DialZkTimeout

// DialZkTimeout dial the server, and wait up to timeout until connection
func DialZkTimeout(zkAddr string, baseTimeout time.Duration, connectTimeout time.Duration) (*ZkConn, <-chan zookeeper.Event, error) {
	servers, err := resolveZkAddr(zkAddr)
	if err != nil {
		return nil, nil, err
	}

	sem.Acquire()
	defer sem.Release()
	zconn, session, err := zookeeper.Connect(servers, baseTimeout)
	if err != nil {
		return nil, nil, err
	}

	// Wait for connection, with a timeout, skipping transition states
	timer := time.NewTimer(connectTimeout)
	for {
		select {
		case <-timer.C:
			zconn.Close()
			return nil, nil, context.DeadlineExceeded
		case event := <-session:
			switch event.State {
			case zookeeper.StateConnected:
				// success
				return &ZkConn{conn: zconn}, session, nil

			case zookeeper.StateAuthFailed:
				// fast fail this one
				zconn.Close()
				return nil, nil, fmt.Errorf("zk connect failed: StateAuthFailed")
			}
		}
	}
}
开发者ID:xujianhai,项目名称:vitess,代码行数:35,代码来源:zkconn.go


示例11: createNode

func createNode() {
	const Node = "/greet"

	conn, _, _ := zk.Connect([]string{zkServer}, time.Second*5)

	create := func() error {
		var err error
		// try creating ephemeral node
		_, err = conn.Create(Node, []byte(httpPort), zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
		return err
	}

	if create() != nil {
		// watch ephemeral node event.
		another, _, eventChan, _ := conn.GetW(Node)
		fmt.Println("Now listen", string(another))
	loop:
		for {
			event := <-eventChan
			if event.Type == zk.EventNodeDeleted || event.Type.String() == "Unknown" {
				// retry creating ephemeral node
				if create() != nil {
					break loop
				}
			}
		}
	}
}
开发者ID:Jun-Chang,项目名称:zsd,代码行数:28,代码来源:server.go


示例12: DialZk

// DialZk dials a ZK server and waits for connection event. Returns a ZkConn
// encapsulating the zookeeper.Conn, and the zookeeper session event
// channel to monitor the connection
//
// The value for baseTimeout is used as a session timeout as well, and
// will be used to negotiate a 'good' value with the server. From
// reading the zookeeper source code, it has to be between 6 and 60
// seconds (2x and 20x the tickTime by default, with default tick time
// being 3 seconds).  min session time, max session time and ticktime
// can all be overwritten on the zookeeper server side, so these
// numbers may vary.
//
// Then this baseTimeout is used to compute other related timeouts:
// - connect timeout is 1/3 of baseTimeout
// - recv timeout is 2/3 of baseTimeout minus a ping time
// - send timeout is 1/3 of baseTimeout
// - we try to send a ping a least every baseTimeout / 3
//
// Note the baseTimeout has *nothing* to do with the time between we
// call Dial and the maximum time before we receive the event on the
// session. The library will actually try to re-connect in the background
// (after each timeout), and may *never* send an event if the TCP connections
// always fail. Use DialZkTimeout to enforce a timeout for the initial connect.
func DialZk(zkAddr string, baseTimeout time.Duration) (*ZkConn, <-chan zookeeper.Event, error) {
	servers, err := resolveZkAddr(zkAddr)
	if err != nil {
		return nil, nil, err
	}

	sem.Acquire()
	defer sem.Release()
	zconn, session, err := zookeeper.Connect(servers, baseTimeout)
	if err != nil {
		return nil, nil, err
	}

	// Wait for connection, possibly forever, skipping transition states
	for {
		event := <-session
		switch event.State {
		case zookeeper.StateConnected:
			// success
			return &ZkConn{conn: zconn}, session, nil

		case zookeeper.StateAuthFailed:
			// fast fail this one
			zconn.Close()
			return nil, nil, fmt.Errorf("zk connect failed: StateAuthFailed")
		}
	}
}
开发者ID:xujianhai,项目名称:vitess,代码行数:51,代码来源:zkconn.go


示例13: GetConnect

func GetConnect() (conn *zk.Conn, err error) {
	conn, _, err = zk.Connect(hosts, timeOut*time.Second)
	if err != nil {
		fmt.Println(err)
	}
	return
}
开发者ID:NotBadPad,项目名称:go-learn,代码行数:7,代码来源:zkutil.go


示例14: zkHealthCheck

// a health check for zookeeper
func zkHealthCheck() error {

	start := time.Now()
	lastError := time.Now()
	minUptime := time.Second * 2
	timeout := time.Second * 30
	zookeepers := []string{"127.0.0.1:2181"}

	for {
		if conn, _, err := zk.Connect(zookeepers, time.Second*10); err == nil {
			conn.Close()
		} else {
			conn.Close()
			glog.V(1).Infof("Could not connect to zookeeper: %s", err)
			lastError = time.Now()
		}
		// make sure that service has been good for at least minUptime
		if time.Since(lastError) > minUptime {
			break
		}
		if time.Since(start) > timeout {
			return fmt.Errorf("Zookeeper did not respond.")
		}
		time.Sleep(time.Millisecond * 1000)
	}
	glog.Info("zookeeper container started, browser at http://localhost:12181/exhibitor/v1/ui/index.html")
	return nil
}
开发者ID:jhuie-zenoss,项目名称:serviced,代码行数:29,代码来源:zk.go


示例15: NewZk

// NewZk connect zookeeper
func NewZk(zks []string) (*zk.Conn, error) {
	c, _, err := zk.Connect(zks, time.Second*1)
	if err != nil {
		return nil, err
	}
	return c, nil
}
开发者ID:ohlinux,项目名称:monitor_kafka_offset,代码行数:8,代码来源:monitor_kafka_offset.go


示例16: PersistFrameworkID

func PersistFrameworkID(
	fwid *mesos.FrameworkID,
	zkServers []string,
	zkChroot string,
	frameworkName string,
) error {
	c, _, err := zk.Connect(zkServers, RPC_TIMEOUT)
	if err != nil {
		return err
	}
	defer c.Close()
	// attempt to create the path
	_, err = c.Create(
		zkChroot,
		[]byte(""),
		0,
		zk.WorldACL(zk.PermAll),
	)
	if err != nil && err != zk.ErrNodeExists {
		return err
	}
	// attempt to write framework ID to <path> / <frameworkName>
	_, err = c.Create(zkChroot+"/"+frameworkName+"_framework_id",
		[]byte(fwid.GetValue()),
		0,
		zk.WorldACL(zk.PermAll))
	// TODO(tyler) when err is zk.ErrNodeExists, cross-check value
	if err != nil {
		return err
	}
	log.Info("Successfully persisted Framework ID to zookeeper.")

	return nil
}
开发者ID:puppetizeme,项目名称:etcd-mesos,代码行数:34,代码来源:zk.go


示例17: Connect

func (this *ServiceBase) Connect() (err error) {
	if this.Conn != nil {
		this.Conn.Close()
	}
	this.Conn, _, err = zk.Connect(this.Hosts, this.ConnTimeout)
	return err
}
开发者ID:yangzhao28,项目名称:rtb,代码行数:7,代码来源:servicebase.go


示例18: InitFunc

// CreateTestData: Implement the zkTestCallbacks interface
// Initialize the steps
func (test *zkLeaderResignationTest) InitFunc() ([]time.Duration, error) {
	test.info.Println("InitFunc()")

	// Create the connection to Zookeeper
	zkConn, _, err := zk.Connect([]string{test.testSetup.zkURL}, test.testSetup.heartBeat)
	test.zkConn = zkConn
	test.wg = &sync.WaitGroup{}

	if err != nil {
		test.error.Printf("leaderResignationTest: Error in zk.Connect (%s): %v",
			test.testSetup.zkURL, err)
		return nil, err
	}

	// Create the election node in ZooKeeper
	_, err = test.zkConn.Create(test.testSetup.electionNode,
		[]byte("data"), 0, zk.WorldACL(zk.PermAll))
	if err != nil {
		test.error.Printf("leaderResignationTest: Error creating the election node (%s): %v",
			test.testSetup.electionNode, err)
		return nil, err
	}

	return []time.Duration{
		time.Second * 1,
		time.Second * 1,
		time.Second * 1,
		time.Second * 1,
		time.Second * 1,
	}, nil
}
开发者ID:Comcast,项目名称:go-leaderelection,代码行数:33,代码来源:zk_leaderresignation.go


示例19: initZk

func (c *Client) initZk() {
	zkclient, _, err := zk.Connect(c.zkHosts, time.Second*30)
	if err != nil {
		panic(err)
	}

	c.zkClient = zkclient

	res, _, _, err := c.zkClient.GetW(c.zkRoot + c.zkRootRegionPath)

	if err != nil {
		panic(err)
	}

	c.rootServer = c.decodeMeta(res)
	c.getRegionConnection(c.getServerName(c.rootServer))

	res, _, _, err = c.zkClient.GetW(c.zkRoot + "/master")

	if err != nil {
		panic(err)
	}

	c.masterServer = c.decodeMeta(res)
}
开发者ID:henrylee2cn,项目名称:go-hbase,代码行数:25,代码来源:client.go


示例20: connect

func connect(zksStr string) (*zk.Conn, <-chan zk.Event) {
	//	zksStr := os.Getenv("ZOOKEEPER_SERVERS")
	zks := strings.Split(zksStr, ",")
	conn, connEvtChan, err := zk.Connect(zks, 5*time.Second)
	must(err)
	return conn, connEvtChan
}
开发者ID:youngkin,项目名称:GoZKLeaderElection,代码行数:7,代码来源:main.go



注:本文中的github.com/samuel/go-zookeeper/zk.Connect函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang zk.NewLock函数代码示例发布时间:2022-05-28
下一篇:
Golang nopclient.NewNopClient函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap