guard

package module
v0.0.0-...-f330659 Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

README

English|中文

Guard is an ETCD V3 based service registration and discovery component that was stripped from the production project and is currently in iteration

Support service multi-copy registration and discovery, with rotation training, support load balancing

Support for custom trigger events, you can define multiple and chained execution

Install

go get github.com/Forget-C/guard

Usage

Register
Multiple copy registration is allowed
path := "service/test_service"
client,_ := clientv3.New(clientv3.Config{
	Endpoints:   []string{"127.0.0.1:2379"},
	DialTimeout: 2 * time.Second,
})
	r := NewDefaultRegister(client)
	err := r.Append(&RegisterOption{Path: path, Info: DataContainer{HostName: "aaaa"}, Multi: true})
Single-instance service registration
path := "service/test_service"
client,_ = clientv3.New(clientv3.Config{
	Endpoints:   []string{"127.0.0.1:2379"},
	DialTimeout: 2 * time.Second,
})
	r := NewDefaultRegister(client)
	err := r.Append(&RegisterOption{Path: path, Info: DataContainer{HostName: "aaaa"}, Multi: false})
Cancel a heartbeat registration
r.Stop(path)
Cancel all heartbeat registration
r.StopAll()
Discover
Add a listener path, and the listener serves multiple instances
path := ""service/test_service"
d := NewDefaultDiscover(client)
err := d.Append(&DiscoverOption{Path:path, Prefix:true})
Add a listener path and the listener is a single-instance service
path := ""service/test_service"
d := NewDefaultDiscover(client)
err := d.Append(&DiscoverOption{Path:path, Prefix:false})
Stop a listener
d.Stop(path)
Stop all listening
d.StopAll()
Gets the status of a listener
d.PrefixGet(path)

Information returned

type DiscoverState struct {
	Option *DiscoverOption // listener parameters 
	CurrentPath string	// The current listening path。
	IsChild bool        // Is a child state. When the listener is multiple copies, the structures of stateful information are all substates
	Data *DataContainer // Contains registration data. When the listener is multiple copies, the value is empty
}
Gets all the child states of a listening state

This function needs to be used to retrieve data when the listener is multiple copies

d.PrefixGetChildren(path)
Polling gets the substates
d.PrefixRoundRobinChildren(path)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DiscoverExistError = errors.New("The current path already exists ")
View Source
var NotAllowMultiError = errors.New("The current service does not allow multiple instances ")
View Source
var PathError = errors.New("The path cannot be empty ")
View Source
var RegisterExistError = errors.New("The current path already exists ")

Functions

func DefaultOnDelHandler

func DefaultOnDelHandler(key []byte, value []byte, state *DiscoverState)

默认的删除时触发的函数

func DefaultOnDoneHandler

func DefaultOnDoneHandler(key []byte, state State)

默认的结束时触发的函数

func DefaultOnFailedHandler

func DefaultOnFailedHandler(key []byte, value []byte, state State, err error)

默认的失败时触发的函数

func DefaultOnIntervalHandler

func DefaultOnIntervalHandler(key []byte, state *RegisterState, KeepaliveResponse *clientv3.LeaseKeepAliveResponse)

默认的间隔时触发的函数

func DefaultOnPutHandler

func DefaultOnPutHandler(key []byte, value []byte, state *DiscoverState)

默认的增加时触发的函数

func NewMonitor

func NewMonitor()

Types

type ChangedHandler

type ChangedHandler func(key []byte, value []byte, state *DiscoverState)

当增、删时触发的函数接口

type DataContainer

type DataContainer struct {
	ID        int64             // 唯一ID
	HostName  string            // 主机名
	GRPCPort  int               // GRPC端口
	Domain    string            // 主机坐在的域
	ServiceId int               // 服务ID
	HttpPort  int               // http端口
	MetaData  map[string]string // 扩展的信息
}

数据容器

func (*DataContainer) GetGRPCConn

func (d *DataContainer) GetGRPCConn() (*grpc.ClientConn, error)

获取grpc连接

func (*DataContainer) GetHTTPConn

func (d *DataContainer) GetHTTPConn() *resty.Client

获取http连接

type Discover

type Discover struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewDefaultDiscover

func NewDefaultDiscover(client *clientv3.Client) *Discover

func NewDiscover

func NewDiscover(client *clientv3.Client) *Discover

func (*Discover) Append

func (d *Discover) Append(option *DiscoverOption) error

追加监听

func (*Discover) PrefixGet

func (d *Discover) PrefixGet(prefix string) (*DiscoverState, bool)

使用前缀获取状态信息, 当被监听者为单实例时使用此函数

func (*Discover) PrefixGetChildren

func (d *Discover) PrefixGetChildren(prefix string) *DiscoverStateChildren

使用前缀获取状态信息, 当被监听者为多实例时使用此函数

func (*Discover) PrefixRoundRobinChildren

func (d *Discover) PrefixRoundRobinChildren(prefix string) *DiscoverState

使用前缀获取状态信息, 当被监听者为多实例时, 且期望轮询时 使用此函数

type DiscoverOption

type DiscoverOption struct {
	Path            string
	Prefix          bool
	OnPutHandler    ChangedHandler
	OnDelHandler    ChangedHandler
	OnFailedHandler FailedHandler
	OnDoneHandler   DoneHandler
}

添加监听的参数选项

type DiscoverState

type DiscoverState struct {
	Option *DiscoverOption // 监听参数

	CurrentPath string         // 当前的监听路径。
	IsChild     bool           // 是否为子状态
	Data        *DataContainer // 数据
	// contains filtered or unexported fields
}

监听状态

func (*DiscoverState) AddOnDelHandlers

func (h *DiscoverState) AddOnDelHandlers(handler ChangedHandler)

追加删除时触发的函数

func (*DiscoverState) AddOnDoneHandler

func (h *DiscoverState) AddOnDoneHandler(handler DoneHandler)

追加结束时触发的函数

func (*DiscoverState) AddOnFailedHandler

func (h *DiscoverState) AddOnFailedHandler(handler FailedHandler)

追加失败时触发的函数

func (*DiscoverState) AddOnIntervalHandler

func (h *DiscoverState) AddOnIntervalHandler(handler IntervalHandler)

追加间隔时触发的函数

func (*DiscoverState) AddOnPutHandler

func (h *DiscoverState) AddOnPutHandler(handler ChangedHandler)

追加增加时触发的函数

func (*DiscoverState) GetCurrentPath

func (d *DiscoverState) GetCurrentPath() string

获取当前的路径

func (*DiscoverState) Start

func (d *DiscoverState) Start()

启动监听

func (*DiscoverState) Stop

func (d *DiscoverState) Stop()

停止监听

type DiscoverStateChildren

type DiscoverStateChildren struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

子状态集

func (DiscoverStateChildren) All

获取所有子状态

func (DiscoverStateChildren) Count

func (c DiscoverStateChildren) Count() int

当前子状态集的长度计数

func (DiscoverStateChildren) Get

获取子状态

func (DiscoverStateChildren) IndexGet

func (c DiscoverStateChildren) IndexGet(i int) *DiscoverState

通过索引获取子状态

type DoneHandler

type DoneHandler func(key []byte, state State)

当结束时触发的函数接口

type FailedHandler

type FailedHandler func(key []byte, value []byte, state State, err error)

当失败时触发的函数接口

type IntervalHandler

type IntervalHandler func(key []byte, state *RegisterState, KeepaliveResponse *clientv3.LeaseKeepAliveResponse)

当间隔时触发的函数接口

type Monitor

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

type Register

type Register struct {
	sync.Mutex
	// contains filtered or unexported fields
}

服务注册

func NewDefaultRegister

func NewDefaultRegister(client *clientv3.Client) *Register

func NewRegister

func NewRegister(client *clientv3.Client) *Register

func (*Register) Append

func (r *Register) Append(option *RegisterOption) error

添加一个服务

func (*Register) Stop

func (r *Register) Stop(path string)

停止一个服务

func (*Register) StopAll

func (r *Register) StopAll()

停止当前注册的所有服务

type RegisterOption

type RegisterOption struct {
	Path              string
	OnFailedHandler   FailedHandler
	OnDoneHandler     DoneHandler
	OnIntervalHandler IntervalHandler
	Ttl               int64
	Info              interface{}
	Multi             bool
}

注册参数

type RegisterState

type RegisterState struct {
	Option *RegisterOption // 注册时所用的参数

	CurrentPath string // 当前注册的路径。当允许多服务时,当前路径是 Option.Path 加上时间戳的。
	// contains filtered or unexported fields
}

注册状态记录

func (*RegisterState) AddOnDelHandlers

func (h *RegisterState) AddOnDelHandlers(handler ChangedHandler)

追加删除时触发的函数

func (*RegisterState) AddOnDoneHandler

func (h *RegisterState) AddOnDoneHandler(handler DoneHandler)

追加结束时触发的函数

func (*RegisterState) AddOnFailedHandler

func (h *RegisterState) AddOnFailedHandler(handler FailedHandler)

追加失败时触发的函数

func (*RegisterState) AddOnIntervalHandler

func (h *RegisterState) AddOnIntervalHandler(handler IntervalHandler)

追加间隔时触发的函数

func (*RegisterState) AddOnPutHandler

func (h *RegisterState) AddOnPutHandler(handler ChangedHandler)

追加增加时触发的函数

func (*RegisterState) GetCurrentPath

func (r *RegisterState) GetCurrentPath() string

获取当前的路径

func (*RegisterState) Keepalive

func (r *RegisterState) Keepalive()

保持注册心跳

func (*RegisterState) Start

func (r *RegisterState) Start()

启动注册

func (*RegisterState) Stop

func (r *RegisterState) Stop()

停止心跳保持

type State

type State interface {
	AddOnPutHandler(handler ChangedHandler)
	AddOnDelHandlers(handler ChangedHandler)
	AddOnFailedHandler(handler FailedHandler)
	AddOnDoneHandler(handler DoneHandler)
	AddOnIntervalHandler(handler IntervalHandler)
	Start()
	Stop()
}

状态接口

Jump to

Keyboard shortcuts

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