diff --git a/.gitignore b/.gitignore index 66fd13c..bbfd2cb 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +.idea \ No newline at end of file diff --git a/get_float64.go b/get_float64.go new file mode 100644 index 0000000..6691643 --- /dev/null +++ b/get_float64.go @@ -0,0 +1,10 @@ +package zookeeper + +import "strconv" + +// GetFloat64Value ... +func GetFloat64Value(path string) float64 { + s := GetStringValue(path) + v, _ := strconv.ParseFloat(s, 64) + return v +} diff --git a/get_int.go b/get_int.go new file mode 100644 index 0000000..15d14c0 --- /dev/null +++ b/get_int.go @@ -0,0 +1,10 @@ +package zookeeper + +import "strconv" + +// GetIntValue ... +func GetIntValue(path string) int { + s := GetStringValue(path) + v, _ := strconv.Atoi(s) + return v +} \ No newline at end of file diff --git a/get_string.go b/get_string.go new file mode 100644 index 0000000..acf65e2 --- /dev/null +++ b/get_string.go @@ -0,0 +1,12 @@ +package zookeeper + +import "fmt" + +// GetStringValue ... +func GetStringValue(path string) string { + value, _, err := zkClient.Get(path) + if err != nil { + fmt.Printf("Get value from path %s err: %v\n", path, err) + } + return string(value) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9abd4db --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/Selly-Modules/zookeeper + +go 1.16 + +require ( + github.com/logrusorgru/aurora v2.0.3+incompatible + github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a02ec3b --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= +github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= +github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414 h1:AJNDS0kP60X8wwWFvbLPwDuojxubj9pbfK7pjHw0vKg= +github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= diff --git a/zookeeper.go b/zookeeper.go new file mode 100644 index 0000000..f88983e --- /dev/null +++ b/zookeeper.go @@ -0,0 +1,31 @@ +package zookeeper + +import ( + "fmt" + "time" + + "github.com/logrusorgru/aurora" + "github.com/samuel/go-zookeeper/zk" +) + +var zkClient *zk.Conn + +// Connect ... +func Connect(uri string) error { + c, _, err := zk.Connect([]string{uri}, time.Second*30) + if err != nil { + fmt.Println("Error when connect to Zookeeper", uri, err) + return err + } + + fmt.Println(aurora.Green("*** CONNECTED TO ZOOKEEPER: " + uri)) + + // Set client + zkClient = c + + return nil +} + + + +