edclient

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

ed-client

etcd discovery client lib.

Introduce

  1. After the etcd service breaks down and restarts, you can restore the connection status and re-register the service.
  2. Distributed locks are used to ensure that no data loss occurs during service discovery when services are started.
  3. Support Simple LB. (Concurrency security)

Usage guide

download

go get github.com/kiraqjx/ed-client

registrant

package main

import (
	"context"
	"time"

	"github.com/coreos/etcd/clientv3"
	edclient "github.com/kiraqjx/ed-client"
)

func main() {

	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{"127.0.0.1:2379"},
		DialTimeout: time.Second * 5,
	})
	if err != nil {
		panic(err)
	}

	r := edclient.NewRegistrant(cli, "/ed-client", "/b/", &edclient.NodeInfo{
		Server: "127.0.0.1:8081",
		Tag:    make(map[string]string),
	}, 60)

	ctx, cancel := context.WithCancel(context.Background())
	err = r.Register(ctx)
	if err != nil {
		panic(err)
	}

	time.Sleep(30 * time.Second)
	r.Quit()
	cancel()
}

discovery

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/coreos/etcd/clientv3"
	edclient "github.com/kiraqjx/ed-client"
)

func main() {
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{"127.0.0.1:2379"},
		DialTimeout: time.Second * 5,
	})
	if err != nil {
		panic(err)
	}

	watcher := edclient.NewWatcher(cli, "/ed-client", "/b/")

	ctx, cancel := context.WithCancel(context.Background())
	err = watcher.Start(ctx)
	if err != nil {
		panic(err)
	}

	time.Sleep(15 * time.Second)
	fmt.Println("watch node:", watcher.Nodes)
	time.Sleep(15 * time.Second)
	fmt.Println("watch node:", watcher.Nodes)
	cancel()
}

lb

package edclient

import (
	edclient "github.com/kiraqjx/ed-client"
)

func main () {
	nodes := []*edclient.NodeInfo{
		{
			Server: "127.0.0.1:8081",
		},
		{
			Server: "127.0.0.1:8082",
		},
		{
			Server: "127.0.0.1:8083",
		},
	}

	lb := edclient.NewLb(nodes)

	node := lb.Lb()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lb added in v1.0.1

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

func NewLb added in v1.0.1

func NewLb(nodes []*NodeInfo) *Lb

func NewLbFromMap added in v1.0.2

func NewLbFromMap(nodesMap map[string]*NodeInfo) *Lb

func (*Lb) ChangeNodes added in v1.0.2

func (lb *Lb) ChangeNodes(nodes []*NodeInfo)

func (*Lb) ChangeNodesFromMap added in v1.0.2

func (lb *Lb) ChangeNodesFromMap(nodesMap map[string]*NodeInfo)

func (*Lb) Lb added in v1.0.1

func (lb *Lb) Lb() *NodeInfo

type NodeInfo

type NodeInfo struct {
	Server string
	Tag    map[string]string
}

type Registrant

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

func NewRegistrant

func NewRegistrant(client *clientv3.Client, prefix string, serverName string,
	nodeInfo *NodeInfo, ttl int64) *Registrant

func (*Registrant) Quit

func (r *Registrant) Quit() error

func (*Registrant) Register

func (r *Registrant) Register(ctx context.Context) error

type Watcher

type Watcher struct {
	Nodes map[string]*NodeInfo
	// contains filtered or unexported fields
}

func NewWatcher

func NewWatcher(client *clientv3.Client, prefix string, serverName string) *Watcher

func (*Watcher) ChangeEvent added in v1.0.2

func (w *Watcher) ChangeEvent() chan bool

func (*Watcher) Start

func (w *Watcher) Start(ctx context.Context) error

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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