naming

package
v0.0.0-...-17aa5c8 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	MetaWeight  = "weight"
	MetaCluster = "cluster"
	MetaZone    = "zone"
	MetaColor   = "color"
)

metadata common key

Variables

View Source
var (

	// ErrDuplication duplication treeid.
	ErrDuplication = errors.New("discovery: instance duplicate registration")
)

Functions

This section is empty.

Types

type Builder

type Builder interface {
	Build(id string) Resolver
	Scheme() string
}

Builder resolver builder.

type Config

type Config struct {
	Nodes  []string
	Region string
	Zone   string
	Env    string
	Host   string
}

Config discovery configures.

type Discovery

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

Discovery is discovery client.

func New

func New(c *Config) (d *Discovery)

New new a discovery client.

func (*Discovery) Build

func (d *Discovery) Build(appid string) Resolver

Build discovery resovler builder.

func (*Discovery) Close

func (d *Discovery) Close() error

Close stop all running process including discovery and register

func (*Discovery) Register

func (d *Discovery) Register(ins *Instance) (cancelFunc context.CancelFunc, err error)

Register Register an instance with discovery and renew automatically

Example

This Example register a server provider into discovery.

package main

import (
	"fmt"
	"time"

	"github.com/claudiuszhang/discovery/naming"
)

func main() {
	conf := &naming.Config{
		Nodes: []string{"127.0.0.1:7171"}, // NOTE: 配置种子节点(1个或多个),client内部可根据/discovery/nodes节点获取全部node(方便后面增减节点)
		Zone:  "sh1",
		Env:   "test",
	}
	dis := naming.New(conf)
	ins := &naming.Instance{
		Zone:  "sh1",
		Env:   "test",
		AppID: "provider",
		// Hostname:"", // NOTE: hostname 不需要,会优先使用discovery new时Config配置的值,如没有则从os.Hostname方法获取!!!
		Addrs:    []string{"http://172.0.0.1:8888", "grpc://172.0.0.1:9999"},
		LastTs:   time.Now().Unix(),
		Metadata: map[string]string{"weight": "10"},
	}
	cancel, _ := dis.Register(ins)
	defer cancel() // NOTE: 注意一般在进程退出的时候执行,会调用discovery的cancel接口,使实例从discovery移除
	fmt.Println("register")
	// Unordered output4
}
Output:

func (*Discovery) Reload

func (d *Discovery) Reload(c *Config)

Reload reload the config

func (*Discovery) Scheme

func (d *Discovery) Scheme() string

Scheme return discovery's scheme

func (*Discovery) Set

func (d *Discovery) Set(ins *Instance) error

Set set ins status and metadata.

type Instance

type Instance struct {
	// Region is region.
	Region string `json:"region"`
	// Zone is IDC.
	Zone string `json:"zone"`
	// Env prod/pre、uat/fat1
	Env string `json:"env"`
	// AppID is mapping servicetree appid.
	AppID string `json:"appid"`
	// Hostname is hostname from docker.
	Hostname string `json:"hostname"`
	// Addrs is the address of app instance
	// format: scheme://host
	Addrs []string `json:"addrs"`
	// Version is publishing version.
	Version string `json:"version"`
	// LastTs is instance latest updated timestamp
	LastTs int64 `json:"latest_timestamp"`
	// Metadata is the information associated with Addr, which may be used
	// to make load balancing decision.
	Metadata map[string]string `json:"metadata"`
}

Instance represents a server the client connects to.

type InstancesInfo

type InstancesInfo struct {
	Instances map[string][]*Instance `json:"instances"`
	LastTs    int64                  `json:"latest_timestamp"`
	Scheduler []Zone                 `json:"scheduler"`
}

InstancesInfo instance info.

func (*InstancesInfo) UseScheduler

func (insInf *InstancesInfo) UseScheduler(zone string) (inss []*Instance)

UseScheduler use scheduler info on instances. if instancesInfo contains scheduler info about zone, return releated zone's instances weighted by scheduler. if not,only zone instances be returned.

type Registry

type Registry interface {
	Register(ins *Instance) (cancel context.CancelFunc, err error)
	Close() error
}

Registry Register an instance and renew automatically.

type Resolve

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

Resolve discveory resolver.

func (*Resolve) Close

func (r *Resolve) Close() error

Close close resolver.

func (*Resolve) Fetch

func (r *Resolve) Fetch() (ins *InstancesInfo, ok bool)

Fetch fetch resolver instance.

func (*Resolve) Watch

func (r *Resolve) Watch() <-chan struct{}

Watch watch instance.

type Resolver

type Resolver interface {
	Fetch() (*InstancesInfo, bool)
	Watch() <-chan struct{}
	Close() error
}

Resolver resolve naming service

type Zone

type Zone struct {
	Src string           `json:"src"`
	Dst map[string]int64 `json:"dst"`
}

Zone zone scheduler info.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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