discovery

package
v0.0.0-...-83adff0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2020 License: GPL-3.0 Imports: 22 Imported by: 0

README

discovery

项目简介

discovery的客户端SDK,包括了服务发现和服务注册功能

编译环境

  • 请只用 Golang v1.8.x 以上版本编译执行

依赖包

1.公共包go-common

Documentation

Overview

Example

this example creates a registry service to register instance info to discovery server. when the program is about to exit,registry.Cancel should be called.

package main

import (
	"context"
	"go-common/library/naming"
	"go-common/library/naming/discovery"
	"time"
)

func main() {
	var c = &discovery.Config{
		Nodes:  []string{"api.bilibili.co"},
		Zone:   "sh001",
		Env:    "pre",
		Key:    "0c4b8fe3ff35a4b6",
		Secret: "b370880d1aca7d3a289b9b9a7f4d6812",
	}
	var ins = &naming.Instance{
		AppID: "main.arch.test2",
		Addrs: []string{
			"grpc://127.0.0.1:8080",
		},
		Version: "1",
		Metadata: map[string]string{
			"weight": "128",
			"color":  "blue",
		},
	}

	d := discovery.New(c)
	cacenl, err := d.Register(context.TODO(), ins)
	if err != nil {
		return
	}
	defer cacenl()
	//start to Serve
	time.Sleep(time.Second * 5)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

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

Functions

func Build

func Build(id string) naming.Resolver

Build register resolver into default discovery.

func Builder

func Builder() naming.Builder

Builder return default discvoery resolver builder.

func Shuffle

func Shuffle(n int, swap func(i, j int))

Shuffle pseudo-randomizes the order of elements. n is the number of elements. Shuffle panics if n < 0. swap swaps the elements with indexes i and j.

Types

type Config

type Config struct {
	Nodes  []string
	Key    string
	Secret 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.

Example

this example creates a discovery client to poll instances from discovery server.

package main

import (
	"context"
	"fmt"
	"go-common/library/naming/discovery"
)

func main() {
	d := discovery.Build("1231234")
	ch := d.Watch()
	for {
		<-ch
		ins, ok := d.Fetch(context.TODO())
		if ok {
			fmt.Println("new instances found:", ins)
		}
	}
}
Output:

func New

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

New new a discovery client.

func (*Discovery) Build

func (d *Discovery) Build(appid string) naming.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(c context.Context, ins *naming.Instance) (cancelFunc context.CancelFunc, err error)

Register Register an instance with discovery and renew automatically

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 *naming.Instance) error

Set set ins status and metadata.

type Resolver

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

Resolver discveory resolver.

func (*Resolver) Close

func (r *Resolver) Close() error

Close close resolver.

func (*Resolver) Fetch

func (r *Resolver) Fetch(c context.Context) (ins map[string][]*naming.Instance, ok bool)

Fetch fetch resolver instance.

func (*Resolver) Watch

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

Watch watch instance.

Jump to

Keyboard shortcuts

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