Skip to content

YoungPioneers/discover-zk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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())
		}
	}
}

About

借助zookeeper实现服务发现

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages