cluster

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2021 License: Apache-2.0 Imports: 17 Imported by: 1

README

概述

一个基于ETCD实现的集群控制器,包含集群操作与服务发现操作。

安装

go get github.com/accfactory/cluster

使用

创建

options := cluster.EtcdConnectOptions{
    Endpoints:   []string{"127.0.0.1:2379"},
    Username:    "",
    Password:    "",
    DialTimeout: 0,
    TTL:         10*time.Second, // 注意,尽量使用素数(秒),否则可能会因缓存出现僵尸信息
    TLS:         nil,
}

cm, cmErr := cluster.NewEtcdCluster("集群节点名称", options, "标签A", "标签B")
if cmErr != nil {
    t.Errorf("new cluster failed, %v", cmErr)
    return
}

集群操作

// 加入(加入动作必须先与任何其它操作)
_ = cm.Join()
// 离开
_ = cm.Leave()
// 获取共享的计数器
counter, _ := cm.Counter("计数器名称")
// 获取共享的MAP
sm := cm.GetMap("MAP名称")
// 获取共享的锁
locker := cm.GetLock("锁名", 1 * time.Minute)

服务发现

// 注册服务
registration, publishErr := cm.Publish("组名", "服务名", "网络协议", "地址(IP:PORT)", []string{"标签"}, Meta, TLS)
// 注销服务
unPublishErr := cm.UnPublish(registration)

// 发现服务
registration, has, getErr := cm.Get("组名", "服务名")


Documentation

Index

Constants

View Source
const (
	ServiceStatusRunning = ServiceStatus("RUNNING")
	ServiceStatusClosing = ServiceStatus("CLOSING")

	ServiceEventTypePut = ServiceEventType("PUT")
	ServiceEventTypeRem = ServiceEventType("DELETE")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cluster

type Cluster interface {
	ServiceDiscovery
	Join() (err error)
	Leave() (err error)
	GetMap(name string) (m SharedMap)
	GetLock(name string, timeout time.Duration) (locker SharedLocker)
	Counter(name string) (counter SharedCounter, err error)
	Close()
}

func NewEtcdCluster

func NewEtcdCluster(name string, options EtcdConnectOptions, tags ...string) (cluster Cluster, err error)

type EtcdConnectOptions

type EtcdConnectOptions struct {
	Endpoints   []string      `json:"endpoints"`
	Username    string        `json:"username,omitempty"`
	Password    string        `json:"password,omitempty"`
	DialTimeout time.Duration `json:"dialTimeout,omitempty"`
	TTL         time.Duration `json:"ttl,omitempty"`
	TLS         *tls.Config
}

type EtcdSharedCounter

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

func (*EtcdSharedCounter) AddAndGet

func (counter *EtcdSharedCounter) AddAndGet(d int64) (v int64, err error)

func (*EtcdSharedCounter) Close

func (counter *EtcdSharedCounter) Close()

func (*EtcdSharedCounter) CompareAndSet

func (counter *EtcdSharedCounter) CompareAndSet(expected int64, v int64) (ok bool, err error)

func (*EtcdSharedCounter) DecrementAndGet

func (counter *EtcdSharedCounter) DecrementAndGet() (v int64, err error)

func (*EtcdSharedCounter) Get

func (counter *EtcdSharedCounter) Get() (v int64, err error)

func (*EtcdSharedCounter) IncrementAndGet

func (counter *EtcdSharedCounter) IncrementAndGet() (v int64, err error)

type EtcdSharedLocker

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

func (*EtcdSharedLocker) Lock

func (lock *EtcdSharedLocker) Lock() (err error)

func (*EtcdSharedLocker) UnLock

func (lock *EtcdSharedLocker) UnLock() (err error)

type EtcdSharedMap

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

func (*EtcdSharedMap) Clean

func (m *EtcdSharedMap) Clean() (err error)

func (*EtcdSharedMap) Get

func (m *EtcdSharedMap) Get(key string, value interface{}) (has bool, err error)

func (*EtcdSharedMap) Keys

func (m *EtcdSharedMap) Keys() (keys []string, err error)

func (*EtcdSharedMap) Put

func (m *EtcdSharedMap) Put(key string, value interface{}, ttl time.Duration) (err error)

func (*EtcdSharedMap) Rem

func (m *EtcdSharedMap) Rem(key string) (err error)

type Meta

type Meta interface {
	Put(key string, value string)
	Get(key string) (value string, has bool)
	Rem(key string)
	Keys() (keys []string)
	Empty() (ok bool)
	Merge(o ...Meta)
}

type Node

type Node struct {
	Id     string        `json:"id,omitempty"`
	Name   string        `json:"name,omitempty"`
	Status ServiceStatus `json:"status,omitempty"`
	Tags   []string      `json:"tags,omitempty"`
}

type Registration

type Registration interface {
	NodeId() (nodeId string)
	NodeName() (nodeName string)
	Id() (id string)
	Group() (group string)
	Name() (name string)
	Status() (status Status)
	Protocol() (protocol string)
	Address() (address string)
	Tags() (tags []string)
	Meta() (meta Meta)
	TLS() (registrationTLS RegistrationTLS)
}

type RegistrationEvent

type RegistrationEvent interface {
	Registration() (registration Registration)
	Type() (eventType RegistrationEventType)
}

type RegistrationEventType

type RegistrationEventType interface {
	IsPut() bool
	IsRem() bool
}

type RegistrationTLS

type RegistrationTLS interface {
	Enable() bool
	VerifySSL() bool
	CA() string
	ServerCert() string
	ServerKey() string
	ClientCert() string
	ClientKey() string
	ToServerTLSConfig() (config *tls.Config, err error)
	ToClientTLSConfig() (config *tls.Config, err error)
}

type Service

type Service struct {
	NodeId_   string          `json:"nodeId,omitempty"`
	NodeName_ string          `json:"nodeName,omitempty"`
	Id_       string          `json:"id,omitempty"`
	Group_    string          `json:"group,omitempty"`
	Name_     string          `json:"name,omitempty"`
	Status_   ServiceStatus   `json:"status,omitempty"`
	Protocol_ string          `json:"protocol,omitempty"`
	Address_  string          `json:"address,omitempty"`
	Tags_     []string        `json:"tags,omitempty"`
	Meta_     Meta            `json:"meta,omitempty"`
	TLS_      RegistrationTLS `json:"tls,omitempty"`
	Version   int64           `json:"-"`
	Deleted   bool            `json:"-"`
	Local     bool            `json:"-"`
}

func NewServiceFromJson

func NewServiceFromJson(value []byte) (service Service, err error)

func (Service) Address

func (s Service) Address() (address string)

func (Service) Group

func (s Service) Group() (group string)

func (Service) Id

func (s Service) Id() (id string)

func (Service) Meta

func (s Service) Meta() (meta Meta)

func (Service) Name

func (s Service) Name() (name string)

func (Service) NodeId added in v1.0.1

func (s Service) NodeId() (nodeId string)

func (Service) NodeName added in v1.0.1

func (s Service) NodeName() (nodeName string)

func (Service) Protocol

func (s Service) Protocol() (protocol string)

func (Service) Status

func (s Service) Status() (status Status)

func (Service) TLS

func (s Service) TLS() (registrationTLS RegistrationTLS)

func (Service) Tags

func (s Service) Tags() (tags []string)

type ServiceDiscovery

type ServiceDiscovery interface {
	Publish(group string, name string, protocol string, address string, tags []string, meta Meta, registrationTLS RegistrationTLS) (registration Registration, err error)
	UnPublish(registration Registration) (err error)
	Get(group string, name string, tags ...string) (registration Registration, has bool, err error)
	GetALL(group string, name string, tags ...string) (registrations []Registration, has bool, err error)
}

type ServiceEvent

type ServiceEvent struct {
	Registration_ Registration          `json:"registration,omitempty"`
	Type_         RegistrationEventType `json:"type,omitempty"`
}

func (ServiceEvent) Registration

func (s ServiceEvent) Registration() (registration Registration)

func (ServiceEvent) Type

func (s ServiceEvent) Type() (eventType RegistrationEventType)

type ServiceEventType

type ServiceEventType string

func (ServiceEventType) IsPut

func (s ServiceEventType) IsPut() bool

func (ServiceEventType) IsRem

func (s ServiceEventType) IsRem() bool

type ServiceMeta

type ServiceMeta map[string]string

func NewServiceMeta

func NewServiceMeta() ServiceMeta

func (ServiceMeta) Empty

func (meta ServiceMeta) Empty() bool

func (ServiceMeta) Get

func (meta ServiceMeta) Get(key string) (string, bool)

func (ServiceMeta) Keys

func (meta ServiceMeta) Keys() []string

func (ServiceMeta) Merge

func (meta ServiceMeta) Merge(o ...Meta)

func (ServiceMeta) Put

func (meta ServiceMeta) Put(key string, value string)

func (ServiceMeta) Rem

func (meta ServiceMeta) Rem(key string)

type ServiceStatus

type ServiceStatus string

func (ServiceStatus) Closing

func (s ServiceStatus) Closing() bool

func (ServiceStatus) Ok

func (s ServiceStatus) Ok() bool

type ServiceTLS

type ServiceTLS struct {
	Enable_     bool   `json:"enable,omitempty"`
	VerifySSL_  bool   `json:"verifySsl,omitempty"`
	CA_         string `json:"ca,omitempty"`
	ServerCert_ string `json:"serverCert,omitempty"`
	ServerKey_  string `json:"serverKey,omitempty"`
	ClientCert_ string `json:"clientCert,omitempty"`
	ClientKey_  string `json:"clientKey,omitempty"`
}

func (ServiceTLS) CA

func (s ServiceTLS) CA() string

func (ServiceTLS) ClientCert

func (s ServiceTLS) ClientCert() string

func (ServiceTLS) ClientKey

func (s ServiceTLS) ClientKey() string

func (ServiceTLS) Enable

func (s ServiceTLS) Enable() bool

func (ServiceTLS) ServerCert

func (s ServiceTLS) ServerCert() string

func (ServiceTLS) ServerKey

func (s ServiceTLS) ServerKey() string

func (ServiceTLS) ToClientTLSConfig added in v1.0.4

func (s ServiceTLS) ToClientTLSConfig() (config *tls.Config, err error)

func (ServiceTLS) ToServerTLSConfig added in v1.0.4

func (s ServiceTLS) ToServerTLSConfig() (config *tls.Config, err error)

func (ServiceTLS) VerifySSL

func (s ServiceTLS) VerifySSL() bool

type ServicesCache

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

func NewServicesCache

func NewServicesCache() (c *ServicesCache, err error)

func (*ServicesCache) Close

func (c *ServicesCache) Close()

func (*ServicesCache) Get

func (c *ServicesCache) Get(group string, name string, tags ...string) (service Service, has bool)

func (*ServicesCache) GetALL

func (c *ServicesCache) GetALL(group string, name string, tags ...string) (services []Service, has bool)

func (*ServicesCache) GetALLLocaled

func (c *ServicesCache) GetALLLocaled() (services []Service)

func (*ServicesCache) OnEvict

func (c *ServicesCache) OnEvict(item *ristretto.Item)

func (*ServicesCache) Put

func (c *ServicesCache) Put(services ...Service)

func (*ServicesCache) Rem

func (c *ServicesCache) Rem(services ...Service)

type SharedCounter

type SharedCounter interface {
	Get() (v int64, err error)
	IncrementAndGet() (v int64, err error)
	DecrementAndGet() (v int64, err error)
	AddAndGet(d int64) (v int64, err error)
	CompareAndSet(expected int64, v int64) (ok bool, err error)
	Close()
}

func NewEtcdSharedCounter

func NewEtcdSharedCounter(name string, cli *etcd.Client) (counter SharedCounter, err error)

type SharedLocker

type SharedLocker interface {
	Lock() (err error)
	UnLock() (err error)
}

type SharedMap

type SharedMap interface {
	Put(key string, value interface{}, ttl time.Duration) (err error)
	Get(key string, value interface{}) (has bool, err error)
	Rem(key string) (err error)
	Keys() (keys []string, err error)
	Clean() (err error)
}

type Status

type Status interface {
	Ok() bool
	Closing() bool
}

Jump to

Keyboard shortcuts

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