1 Star 0 Fork 0

tking / micro-service

Create your Gitee Account
Explore and code with more than 8 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
配置中心-zookeeper.md 2.13 KB
Copy Edit Web IDE Raw Blame History
tking authored 2022-02-26 15:08 . style

简述

ZooKeeper 是一个集中式服务,用于维护配置信息、命名、提供分布式同步和提供组服务。在微服务开发中,为了配置的高可用,今天将学习实践用zk实现配置中心。

部署zookeeper-server

这里选择Docker安装

#  docker run --name zookeeper --restart always -d -p 2181:2181 zookeeper:3.5

zkui (dashboard)

zk的ui支持web/win/mac

web

https://github.com/DeemOpen/zkui

 docker run --rm -p 9090:9090 -e ZK_SERVER=zkserver:2181 juris/zkui

访问 zui-server:9090 cd7001d77451990ffa0c1dd8ba14f93e.png username: admin, pwd: manager 更多操作可参考: https://github.com/DeemOpen/zkui

mac

可参照 https://github.com/vran-dev/PrettyZoo/blob/master/README_CN.md

go-zookeeper

go zookeeper client

https://github.com/go-zookeeper/zk

支持的功能:

  • 支持增删改查等常规功能
  • 支持账密认证
  • 支持watch机制
  • 支持分布锁

Demo(邮件服务的配置管理):

  • 创建节点 mail_service
  • 在mail_service 创建子节点 SIT配置
  • 在SIT 节点里 Add Property(key=port/value=:8000) 0d9f37d3570f0d6cdb88a08ab1ff2fc0.png

接着我们通过go-zookeeper来 CURD

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-zookeeper/zk"
	"time"
)

func main() {
	var (
		c     *zk.Conn
		err   error
		zksvr = "ZKSERVER:2181"
		path  = "/mail_service/sit/port"
	)
    //连接zkserver
	c, _, err = zk.Connect([]string{zksvr}, time.Second*10)
	if err != nil {
		panic(err)
	}
    //获取节点数据
	nodeData, sate, err := c.Get(path)
	if err != nil {
		panic(err)
	}
	port := string(nodeData)
	fmt.Printf("get: %v\n", port)
	fmt.Printf("sate: %v\n", sate)

	r := gin.Default()
	r.GET("/foo", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "hello",
		})
	})
	r.Run(port)
}

输出

[GIN-debug] Listening and serving HTTP on :8000

参考:

Comment ( 0 )

Sign in to post a comment

1
https://gitee.com/lucktk/study-nodes.git
git@gitee.com:lucktk/study-nodes.git
lucktk
study-nodes
micro-service
master

Search