naming

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2018 License: MIT Imports: 16 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 disovery 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/Bilibili/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 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 map[string][]*Instance, ok bool)

Fetch fetch resolver instance.

func (*Resolve) Watch

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

Watch watch instance.

type Resolver

type Resolver interface {
	Fetch() (map[string][]*Instance, bool)
	Watch() <-chan struct{}
	Close() error
}

Resolver resolve naming service

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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