dmitriyGarden / consul-leader-election

Golang implementation for leader election through Consul API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consul leader election Build Status codecov GoDoc

This package provides leader election through consul

https://www.consul.io/docs/guides/leader-election.html

How to use

package main

   import(
       "github.com/hashicorp/consul/api"
       "github.com/dmitriyGarden/consul-leader-election"
       "fmt"
   )
   type notify struct {
      T  string
   }
   func (n *notify)EventLeader(f bool)  {
       if f {
           fmt.Println(n.T, "I'm the leader!")
       } else {
           fmt.Println(n.T, "I'm no longer the leader!")
       }
   }
   func main(){
       conf := api.DefaultConfig()
   	consul, _ := api.NewClient(conf)
       n := &notify{
       	T: "My service",
       }

   	elconf := &ElectionConfig{
                 	CheckTimeout: 5 * time.Second,
                 	Client: consul,
                 	Checks: []string{"healthID"},
                 	Key: "service/test-election/leader",
                 	LogLevel: election.LogDebug
                 	Event: n,
                }
   	e := election.NewElection(elconf)
   	// start election
   	go  e.Init()
   	if e.IsLeader() {
           fmt.Println("I'm a leader!")
       }
   	// to do something ....
   	if e.IsLeader() {
   		fmt.Println("I'm a leader!")
   	}
   	// re-election
   	e.ReElection()
   	// to do something ....
   	if e.IsLeader() {
   		fmt.Println("I'm a leader!")
   	}
   	e.Stop()
   }

About

Golang implementation for leader election through Consul API

License:MIT License


Languages

Language:Go 99.0%Language:Makefile 1.0%