etcdlib

package
v2.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 9, 2019 License: Apache-2.0 Imports: 0 Imported by: 0

README

Phonebook example

The etcd library is showcased on the phonebook example. Phonebook entry "Contact" is modelled by protofile. For each entry name, company and phone number is stored.

To generate go structs from proto file run:

make generate

To start examples you have to have etcd running, if you don't have it installed locally, you can use the following docker image:

sudo docker run -p 2379:2379 --name etcd --rm \
    quay.io/coreos/etcd:v3.0.16 /usr/local/bin/etcd \
    -advertise-client-urls http://0.0.0.0:2379 \
    -listen-client-urls http://0.0.0.0:2379

In the example, the connection to etcd is configured using --cfg argument. If the file is not specified, the application tries to connect to etcd running on the localhost with the default port 2379.

The example contains three programs:

View

View is a showcase for data retrieval. It prints the content of the phonebook:

$go run view/view.go --cfg etcd.conf
Phonebook:
    John Doe
        Inc.
        4569
    Peter Smith
        Company xy
        +48621896
Revision 22

Editor

Editor allows to add contact:

$go run editor/editor.go --cfg etcd.conf put "Peter Smith" "Company xy" "+48621896"
Saving  /phonebook/PeterSmith

create multiple contacts in one transaction:

$go run editor/editor.go puttxn '[{"name":"John Doe","company":"XY","phonenumber":"465464"}, {"name":"Tom New","company":"Comp","phonenumber":"123456"}]'
Saving  /phonebook/JohnDoe
Saving  /phonebook/TomNew

and to remove contacts from the phonebook:

$go run editor/editor.go --cfg etcd.conf delete "John Doe"
Removing  /phonebook/JohnDoe

Watcher

Watcher monitors and logs the changes in the phonebook.

$go run watcher/watcher.go 
Watching the key:  /phonebook/
Creating  /phonebook/PeterSmith
        Peter Smith
                Company xy
                +48621896
============================================

Election example

Tiny election app showcases the usage of etcd election mechanism in order to select a leader. The primary use case of mechanism is to ensure that. Multiple instances of the app can compete for leadership on prefix /election.

Example usage:

election/election --cfg etcd.conf

Since there is no other competitor in election, the running instance should be elected as leader and exit immediately.

You can run multiple instances concurrently with optional argument that defines number of seconds that elected leader stays in position until it resigns:

election/election --cfg etcd.conf 5

Observe that always only one instance at a time is leader.

Documentation

Overview

Package etcdlib contains a collection of examples demonstrating the usage of the etcd library.

Directories

Path Synopsis
Example program "editor" demonstrates CREATE/UPDATE/DELETE operations with etcd library.
Example program "editor" demonstrates CREATE/UPDATE/DELETE operations with etcd library.
model
Example program "view" shows how to read data from etcd datastore.
Example program "view" shows how to read data from etcd datastore.
Example program *watcher* shows how to watch for changes in etcd datastore.
Example program *watcher* shows how to watch for changes in etcd datastore.

Jump to

Keyboard shortcuts

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