etcd

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

im-etcd

im聊天服务注册(etcd)

安装

go install github.com/wu-xie-888/etcd@v1.0.0

用法

func main() {
	//初始化
	var name = "test"
	r := NewRegister(OptionEtcd{
		Address: []string{"127.0.0.1:2379"},
		Timeout: 3 * time.Second,
		Client:  nil,
		Node:    nil,
		Context: nil,
	})
	//注册
	addr, err := r.Register(Options{
		Name:    name,
		Address: nil,
		Context: nil,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	if addr != nil {
		fmt.Println(*addr)
	}
	//反注册
	err = r.Unregister(Options{
		Name:    name,
		Address: nil,
		Context: nil,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	//获取所有服务注册信息
	list, err := r.DiscoverList()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(list)
	//获取当前服务注册信息
	info, err := r.DiscoverInfo(Options{
		Name:    name,
		Address: nil,
		Context: nil,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(decode(info)))
	//监控
	err = r.Watch(Options{
		Name:    name,
		Address: nil,
		Context: nil,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OptionEtcd

type OptionEtcd struct {
	Address []string
	Timeout time.Duration
	Client  *clientv3.Client
	Node    map[string]Options //服务信息(key=服务名称)
	Context context.Context
}

func (*OptionEtcd) DiscoverInfo

func (o *OptionEtcd) DiscoverInfo(opt Options) (*Options, error)

func (*OptionEtcd) DiscoverList

func (o *OptionEtcd) DiscoverList() (map[string]Options, error)

func (*OptionEtcd) Register

func (o *OptionEtcd) Register(opt Options) (*string, error)

func (*OptionEtcd) Unregister

func (o *OptionEtcd) Unregister(opt Options) error

Unregister 反注册,获取当前服务名称的所有节点,删除当前地址的节点

func (*OptionEtcd) Watch

func (o *OptionEtcd) Watch(opt Options) error

type Options

type Options struct {
	Name    string          `json:"Name"`
	Address []string        `json:"address"`
	Context context.Context `json:"context"`
}

type Registry

type Registry interface {
	Register(Options) (*string, error)         //注册
	Unregister(Options) error                  //反注册
	DiscoverInfo(Options) (*Options, error)    //发现单服务
	DiscoverList() (map[string]Options, error) //发现所有服务
	Watch(Options) error                       //监控
}

func NewRegister

func NewRegister(opt OptionEtcd) Registry

Jump to

Keyboard shortcuts

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