etcdhashring

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2020 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

etcd-hashring

Golang Lib to monitor and distribute nodes registered in ETCD dir based on a key for sharding capabilities (employ consistent hashing)

This list will connect to an ETCD server and start to watch a certain dir. When some node in this dir is updated, the internal node are updated.

When requested about the node for a arbitrary elementId, it will get the current nodes list in memory, and apply Consistentent Hash ring to select a ingle node for the element.

See a REST API example at /simple-server

Usage - Golang lib

    import (
        hashring "github.com/flaviostutz/etcd-hashring"
    )

    //connect etcd hashring to the source etcd server
	etcdHashring, err := hashring.NewETCDHashring("http://0.0.0.0:2379", "/webservers/account", 30 * time.Second)
	if err != nil {
		return nil, err
	}

	//connect and keep nodes list updated
	go etcdHashring.Run()

    //get Node according to elementID
    node, err := GetNode(elementID)
    if err!=nil {
		return nil, err
    }

    fmt.Printf("Target node for element id %s is %s", elementID, node.Name)

Usage - API Server

There is a simple API server that can be used to expose this library's results out-of-the-box.

  • Create docker-compose.yml
version: '3.7'

services:

  etcd-hashring:
    image: flaviostutz/etcd-hashring
    ports:
      - "3000:3000"
    restart: always
    environment:
      - LOG_LEVEL=debug
      - ETCD_URL=http://etcd0:2379
      - ETCD_SERVICE_PATH=/webservers/test1

  etcd0:
    image: quay.io/coreos/etcd:v3.2.25
    environment:
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_ADVERTISE_CLIENT_URLS=http://etcd0:2379

  etcd3-lucas:
    image: flaviostutz/etcd-browser-lucas:0.0.1
    ports:
      - 8888:8080
    environment:
      - ENDPOINTS=http://etcd0:2379

  sample-registrar:
    depends_on:
      - etcd0
    image: flaviostutz/etcd-registrar
    environment:
      - LOG_LEVEL=debug
      - ETCD_URL=http://etcd0:2379
      - ETCD_BASE=/webservers
      - SERVICE=test1
      - PORT=3000
      - TTL=60
      - INFO=
  • Run docker-compose up -d

  • After initialization, run curl localhost:3000/config/abc123

  • Add more possible target instances: docker-compose scale sample-registrar=10

  • Run again curl localhost:3000/config/abc123

  • Check if returned node is different for the same element identification

  • Run curl localhost:3000/config/xyz111

  • Run curl localhost:3000/config/AAAAA

  • Run curl localhost:3000/config/BBBBB

  • Verify that each element Id will be targeted to a different node, but the same element id will always return to the same node, unless the number of target servers is updated.

ENVs

  • LOG_LEVEL - sets the log level of verbosity. defaults to 'info'
  • ETCD_URL - List of ETCD cluster URLs separated by space. Ex.: "http://etcd0:2379 http://etcd1:2379" . required
  • ETCD_SERVICE_PATH - ETCD dir path to watch for nodes being added or removed. required
  • ETCD_TIMEOUT_SECONDS - ETCD connection timeout. defaults to '30'

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ETCDHashring

type ETCDHashring struct {
	CurrentNodeList []Node
	// contains filtered or unexported fields
}

ETCDHashring main struct for lib ETCD Hashring

func NewETCDHashring

func NewETCDHashring(etcdEndpoints []string, etcdServicePath string, defaultTimeout time.Duration) (*ETCDHashring, error)

NewETCDHashring create a new ETCD Hashring instance. Call Start() to connect and start listening to ETCD changes

func (*ETCDHashring) AddWatcher

func (e *ETCDHashring) AddWatcher(listener Watcher)

AddWatcher add a new update listener

func (*ETCDHashring) GetNode

func (e *ETCDHashring) GetNode(elementID string) (Node, error)

GetNode get the target node according to the elementId. For example, if Nodes in ETCD is a list of servers that accepts requests, you can use GetNode("client-172.23.23.21") to identify to which server the client would send its requests so that all servers will have a distributed set of clients targeting requests to them from origin, according to the idea of Consistent Hashing

func (*ETCDHashring) Run

func (e *ETCDHashring) Run()

Run connect to ETCD and start monitoring service node for changes

type Node

type Node struct {
	Name string
	Info map[string]string
}

Node info on ETCD

type Watcher

type Watcher interface {
	NodesUpdated(nodes []Node)
}

Watcher is the interface for nodes update listeners

Jump to

Keyboard shortcuts

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