Code Monkey home page Code Monkey logo

consul-checkin's Introduction

consul-checkin

This is a dummy draft implementation of Go/Golang library to register Golang services automatically within Hashicorp Consul. This one works but definitely expected to be unstable as well as provides unclear API which is a subject to change. The library is made public available since it became dependency for already deployed production services.

Rationale

HashiCorp Consul's API allows to register service in a simple manner. Due to the nature the API is limited:

  1. as soon as service is registered on Consul's agent, nothing happens when Consul Agent become unavailable for some time due to any reason; i.e. the service wouldn't be re-registered again (especially when Consul cluster goes down).

  2. when a Golang service starts before Consul Agent is available, it has to watch its availability to register itself using API above provided.

consul-checkin is a Go/Golang library to watch connection to a Consul agent and register service as soon as Consul agent becomes available; or re-register when a connection to a Consul agent is lost. Such strategy allows microservices to be deployed independently of Consul Agent's state.

Usage

import consul_checkin "github.com/thekondor/consul-checkin"
import consul_api "github.com/hashicorp/consul/api"

func main() {
...

// create consul client using default's HashiCorp API
consulClient, err := consul_api.NewClient(consul_api.DefaultConfig())
if nil != err {
  log.Fatalf("Failed to create consul client, err = %+v", err)
}

// create a worker to checkin service automatically as soon as connection with Consul is established or recovered 
selfCheckin := consul_checkin.CheckinAutomatically(
  &consul_checkin.Config{
    Consul: consul_checkin.ConsulOptions{consulclient},
    On: CheckinEvents{
      ServiceRegisterFailed: func(serviceId string, err error) {
        log.Printf("Service registration '%s' failed due to '%v'", serviceId, err)
      },
      ServiceDeregisterFailed: func(serviceId string, err error) {
        log.Printf("Service de-registration '%s' failed due to '%v'", serviceId, err)
      },
    },
  },
)

// Declare services to register
selfCheckin.Add(consul_checkin.Service{
  ConsulService: &consul_api.AgentServiceRegistration{
   ... // to be initialized accordingly to github.com/hashicorp/consul/api
  },
})

// Start services registration in a background. The call returns immediatelly.
selfCheckin.Start()

// In case of graceful shutdown, try to unregister services. Otherwise Consul would mark this application as a one with a critical state.
go func() {
  <- ctx.Done() // or other way to get notification when the application stops
  selfCheckin.Stop() // to unregister added service(s) on application stop
}()
...
}

License

The library is released under the MIT license. See LICENSE file.

consul-checkin's People

Contributors

thekondor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.