zookeeper

package module
v0.0.0-...-6fc037c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2017 License: BSD-2-Clause Imports: 8 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Introduction

discover-zk借助zookeeper实现服务发现。辅助在无proxy架构下实现自动扩容

Quick-start

# 服务端
package main

import (
	"fmt"
	"github.com/YoungPioneers/discover-zk"
	"time"
)

func main() {
	zkClient, err := zookeeper.NewClient("/example/nodes")
	if nil != err {
		fmt.Printf("zk client init failed. err: %s\n", err.Error())
		return
	}
	defer zkClient.Close()

	zkClient.Register("node1", []byte("value for node1"))

	for {
		// server runing
		fmt.Println("server is running")
		time.Sleep(10 * time.Second)
	}
}

# 客户端
package main

import (
	"fmt"
	"scaling-zk"
)

func main() {
	zkClient, err := zookeeper.NewClient("/example/nodes")
	if nil != err {
		fmt.Printf("zk client init failed. err: %s\n", err.Error())
		return
	}
	defer zkClient.Close()

	nodes, err := zkClient.Nodes()
	if nil != err {
		fmt.Printf("get nodes failed. err: %s\n", err.Error())
		return

	}

	fmt.Printf("nodes: %+v\n", nodes)

	snapshots, errors := zkClient.Mirror()
	for {
		select {
		case snapshot := <-snapshots:
			fmt.Printf("snapshot: %+v\n", snapshot)
		case err := <-errors:
			fmt.Printf("zk error. err: %s\n", err.Error())
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosedInstance 实例已关闭
	ErrClosedInstance = errors.New("Closed instance")
	// ErrNodesNeeded 未指定节点
	ErrNodesNeeded = errors.New("Nodes needed")
)

Functions

func DefaultAddrs

func DefaultAddrs() (nodes []string, err error)

DefaultAddrs 默认ZK地址,通过TXT记录查询获取

func DefaultAddrsTXT

func DefaultAddrsTXT() string

DefaultAddrsTXT 获取默认TXT地址

func SetDefaultAddrsTXT

func SetDefaultAddrsTXT(txt string)

SetDefaultAddrsTXT 设置TXT地址

Types

type ZKClient

type ZKClient struct {
	// contains filtered or unexported fields
}

ZKClient zookeeper client

func NewClient

func NewClient(path string, addrs ...string) (client *ZKClient, err error)

NewClient 初始化, path为server&&client共用路径,addrs为zookeepers ip

func (*ZKClient) Addrs

func (zkClient *ZKClient) Addrs() []string

Addrs .

func (*ZKClient) Close

func (zkClient *ZKClient) Close() (err error)

Close .

func (*ZKClient) Exists

func (zkClient *ZKClient) Exists() (exists bool, err error)

Exists 判断节点自己的存在性

func (*ZKClient) Mirror

func (zkClient *ZKClient) Mirror() (snapshots chan map[string][]byte, errors chan error)

Mirror 指定路径的snapshots chan, 每个snapshot如 map[string][]byte{"ip:port":"value","ip:port":"value"}

func (*ZKClient) Name

func (zkClient *ZKClient) Name() string

Name .

func (*ZKClient) Nodes

func (zkClient *ZKClient) Nodes() (nodeValues map[string][]byte, err error)

Nodes 返回节点列表

func (*ZKClient) Register

func (zkClient *ZKClient) Register(name string, value []byte) (err error)

Register 注册临时节点, value为自定义信息(权重,开关等)

func (*ZKClient) Update

func (zkClient *ZKClient) Update(value []byte) (err error)

Update 更新节点数据

Directories

Path Synopsis
example
client command
server command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL